Question

I have ImageButton and TextView inside RelativeLayout. I want to change their colors to blue when they are pressed and make them white again when released. also I need to do it to both of them no matter which of them I click. and I want to activate the same action to both of them, for example a Toast that will be shown on the screen.

I tried to do it using selectors and duplicateParentState and many other options. Can someone give me a simple example of how to do it?

UPDATE:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/trash_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/trash_pressed" android:state_focused="true"/>
<item android:drawable="@drawable/trash"/>
</selector>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#33B5E5" />
    <item android:state_focused="true" android:color="#33B5E5" />
    <item android:color="#ffffff" />
</selector>

and this is the layout

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scaleType="fitCenter"
        android:gravity="center"
        android:layout_gravity="center">

<ImageButton
        android:id="@+id/button_ResetData"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@android:color/transparent"
        android:clickable="true"
        android:duplicateParentState="true"
        android:scaleType="fitCenter"
        android:src="@drawable/reset_button_selector"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/button_ResetData"
            android:scaleType="fitCenter"
            android:clickable="true"
            android:text="@string/Button_ResetData"
            android:textSize="10dp"
            android:duplicateParentState="true"
            android:textColor="@drawable/main_buttons_text_color_selector"/>

</RelativeLayout>

when I click the layout the selectors of the text and button is activated but not the click event. when I click the button the selectors don't work but the click event works

Was it helpful?

Solution 5

FOUND A SOLUTION:

final ImageButton resetButton = (ImageButton) findViewById(R.id.button_ResetData);
            final TextView resetButton_Title = (TextView) findViewById(R.id.textview_button_ResetData_title);
            View.OnTouchListener onTouchListener_ResetButton = new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            resetButton.setImageResource(R.drawable.trash_pressed);
                            resetButton_Title.setTextColor(getResources().getColor(R.color.Blue_Light));
                            break;
                        case MotionEvent.ACTION_UP:
                            resetButton.setImageResource(R.drawable.trash);
                            resetButton_Title.setTextColor(Color.WHITE);

                            AlertDialog.Builder builder = new Builder(MainActivity.this);
                            builder.setMessage("Are you sure you want to clear the list (except \"In progress\")?")
                                    .setPositiveButton("Yes", resetListDialogClickListener)
                                    .setNegativeButton("No", resetListDialogClickListener);
                            AlertDialog dialog = builder.show();
                            TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
                            messageView.setGravity(Gravity.CENTER);
                    }

                    return false;
                }
            };
            resetButton.setOnTouchListener(onTouchListener_ResetButton);
            resetButton_Title.setOnTouchListener(onTouchListener_ResetButton);

OTHER TIPS

You can set selectors to RelativeLayout directly and implement onClickListener for the same. And make sure you include android:clickable="true" inside your RelativeLayout.

Very Simple

In your activity where u call the on click function... define them separately but use them like this

public class yourclass extends Activity implements OnClickListener
{


    ImageButton IMGBTN1;

      TextView tv1;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_previousentry);


        IMGBTN1 = (ImageButton) findViewById(R.id.imageButton1);

        tv1= (TextView) findViewById(R.id.textview1);       

        IMGBTN1.setOnClickListener(this);
                tv1.setOnClickListener(this);

}
 public void onClick(View v) 

          {



                if (v == IMGBTN1 )
                               {
                IMGBTN1.setBackgroundColor(Color.BLUE);
             tv1.setTextColor(Color.parseColor("#bdbdbd"));
       Toast.makeText(getBaseContext(),"img 1 pressed", Toast.LENGTH_LONG).show();

  delay();

                                }
                                if (v == tv1)
                               {
             IMGBTN1.setBackgroundColor(Color.BLUE);
            tv1.setTextColor(Color.parseColor("#bdbdbd"));
 Toast.makeText(getBaseContext(),"textview 1 pressed", Toast.LENGTH_LONG).show();
                         delay();

                                }


      public void delay()
   {
       final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run()
            {
                  IMGBTN1.setBackgroundColor(Color.RED);
              tv1.setTextColor(Color.parseColor("#ffffff"));

              }

        }, 500);

   }
    }

Use yourView.performClick() to programatically click a button.

You can try this,

Add selectors to both Textview and image drawable. Add that image drawable to textview using attributes like drawableLeft, drawableTop etc in xml. Let me know if it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top