-2

i'm a completely noob with java code but i want to learn. Now i set a keypad ,to do this i entered 3 orizzontal Linearlayout and in each one of it i entered 3 Buttons,all this in activity_main.xml like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="286dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:inputType="numberPassword" >

    <requestFocus />
</EditText>
<LinearLayout android:orientation="horizontal" 
             android:layout_width="fill_parent" 
             android:layout_height="54.0dip" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip">

   <Button   android:textStyle="bold" 
             android:id="@+id/one"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />


    <Button  android:textStyle="bold" 
             android:id="@+id/two"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold"
             android:id="@+id/three"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />
 </LinearLayout>
 <LinearLayout android:orientation="horizontal" 
             android:layout_width="fill_parent" 
             android:layout_height="54.0dip" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip">

    <Button  android:textStyle="bold"
             android:id="@+id/four"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold" 
             android:id="@+id/five"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold"
             android:id="@+id/six"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" 
             android:layout_width="fill_parent" 
             android:layout_height="54.0dip" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip">

    <Button  android:textStyle="bold"
             android:id="@+id/seven" 
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold" 
             android:id="@+id/eight"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold"
             android:id="@+id/nine"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />
</LinearLayout>

</LinearLayout>

each background of Button is definite by a xml in drawable like this

<?xml version="1.0" encoding="utf-8"?>
<selector
 xmlns:android="http://schemas.android.com/apk/res/android">   
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/btn_default_normal_disable" />
<item android:state_pressed="true" android:drawable="@drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_default_selected" />
<item android:state_enabled="true" android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true" android:drawable="@drawable/btn_default_normal_disable_focused" />
<item android:drawable="@drawable/btn_default_normal_disable" />

</selector>

and my MainActivity.java is this

package com.provabottone;

import android.os.Bundle;
import android.widget.Button;
import android.app.Activity;


public class MainActivity extends Activity {





private Button mOne;
private Button mTwo;
private Button mThree;
private Button mFour;
private Button mFive;
private Button mSix;
private Button mSeven;
private Button mEight;
private Button mNine;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


          mOne = (Button) findViewById(R.id.one);
          mTwo = (Button) findViewById(R.id.two);
          mThree  = (Button) findViewById(R.id.three);
          mFour = (Button) findViewById(R.id.four);
          mFive = (Button) findViewById(R.id.five);
          mSix = (Button) findViewById(R.id.six);
          mSeven = (Button) findViewById(R.id.seven);
          mEight = (Button) findViewById(R.id.eight);
          mNine = (Button) findViewById(R.id.nine);

           mOne.setText("1");
           mTwo.setText("2");
           mThree.setText("3");
           mFour.setText("4");
           mFive.setText("5");
           mSix.setText("6");
           mSeven.setText("7");
           mEight.setText("8");
           mNine.setText("9");

          mOne.setBackgroundResource(R.drawable.bottone_premuto);
          mTwo.setBackgroundResource(R.drawable.bottone_premuto);
          mThree.setBackgroundResource(R.drawable.bottone_premuto);
              mFour.setBackgroundResource(R.drawable.bottone_premuto);
          mFive.setBackgroundResource(R.drawable.bottone_premuto);
          mSix.setBackgroundResource(R.drawable.bottone_premuto);
          mSeven.setBackgroundResource(R.drawable.bottone_premuto);
          mEight.setBackgroundResource(R.drawable.bottone_premuto);
          mNine.setBackgroundResource(R.drawable.bottone_premuto);

}


}

now what i want is to change the color of the button pressed definite by btn_default_pressed_holo_dark when pressed,what code i need to add in my MainActivity.java? thanks in advance and sorry for long post. Hi Nicola

Nicola
  • 1
  • 1
  • Google it. You can find good tutorials – Rick Feb 12 '14 at 22:01
  • @user2675569 you are right but is a month that I'm looking for and i found nothing to solve my issue – Nicola Feb 13 '14 at 16:59
  • You can find good tutorials here: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList http://stackoverflow.com/questions/4336060/android-how-to-add-a-custom-button-state http://stackoverflow.com/questions/8852420/what-is-the-state-focused-state-for-a-button – Rick Feb 13 '14 at 17:39
  • @user2675569 thanks for the links but they don't solve again my issue – Nicola Feb 13 '14 at 19:40
  • You also have to remove this from your java class – Rick Feb 14 '14 at 14:24
  • _mOne.setBackgroundResource(R.drawable.bottone_premuto); mTwo.setBackgroundResource(R.drawable.bottone_premuto); mThree.setBackgroundResource(R.drawable.bottone_premuto); mFour.setBackgroundResource(R.drawable.bottone_premuto); mFive.setBackgroundResource(R.drawable.bottone_premuto); mSix.setBackgroundResource(R.drawable.bottone_premuto); mSeven.setBackgroundResource(R.drawable.bottone_premuto); mEight.setBackgroundResource(R.drawable.bottone_premuto); mNine.setBackgroundResource(R.drawable.bottone_premuto);_ – Rick Feb 14 '14 at 14:24
  • @user2675569 thanks yes i' ve already remove those lines, but i found the solution adding this code for each button http://tech.chitgoks.com/2011/04/17/android-change-imagebutton-tint-to-simulate-button-pressed/ – Nicola Feb 14 '14 at 15:22

1 Answers1

0

i solved my issue removing this code

mOne.setBackgroundResource(R.drawable.bottone_premuto);
      mTwo.setBackgroundResource(R.drawable.bottone_premuto);
      mThree.setBackgroundResource(R.drawable.bottone_premuto);
          mFour.setBackgroundResource(R.drawable.bottone_premuto);
      mFive.setBackgroundResource(R.drawable.bottone_premuto);
      mSix.setBackgroundResource(R.drawable.bottone_premuto);
      mSeven.setBackgroundResource(R.drawable.bottone_premuto);
      mEight.setBackgroundResource(R.drawable.bottone_premuto);
      mNine.setBackgroundResource(R.drawable.bottone_premuto);

and adding this code

    mOne.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mOne.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mOne.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mTwo.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mTwo.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mTwo.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mThree.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mThree.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mThree.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mFour.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mFour.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mFour.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mFive.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mFive.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mFive.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mSix.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mSix.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mSix.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mSeven.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mSeven.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mSeven.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mEight.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mEight.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mEight.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mNine.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mNine.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mNine.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });

but now i want to make it in another way without the onTouchListener,adding android:onClick="button_pressed" to each button in my .xml and recall it with a new method like this

 public void button_pressed(View v) {

   button_pressed = findViewById(R.drawable.btn_default_pressed_holo_dark);
    button_pressed.setBackgroundColor(Color.argb(33, 243, 55, 123));

}

but the buttons when pressed shows always the color definited by btn_default_pressed_holo_dark. What I do wrong?

Nicola
  • 1
  • 1