简体   繁体   中英

Changing TextView Background color on touch

I want to change Background color of a textview when click or touch it. this is textview

<com.example.shuvo.KeyboardButton
                android:id="@+id/showMenuButton"
                style="@drawable/color"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:alpha="0.8"
                android:text="Menu"
                android:textColor="@drawable/text_link_selector"
                android:background="@drawable/keyboard"
                 />

In keyboard.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">  <color android:color="#00ff00" /> </item>

And in keyboardButton.java

public class KeyboardButton extends TextView {

public KeyboardButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public KeyboardButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public KeyboardButton(Context context) {
    super(context);
}

public void setLayoutParams(int width, int height) {
    LayoutParams params = new LayoutParams(width, 60);
    this.setLayoutParams(params);
    setBackgroundResource(R.drawable.keyboard);
    setPadding(10, 10, 10, 10);
}

}

This not working. Where is the problem?

Create .xml file in drawable folder and put this code inside

<item android:drawable="@color/red" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@color/red" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@color/red" android:state_focused="true"/>
<item android:drawable="@color/green" android:state_focused="false" android:state_pressed="false"/>

After this set this xml as a background in textview

<TextView android:id="@+id/txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:background="@drawable/yourxmlfile" />

Best way is using a selector xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true" android:drawable="@color/red"/>
<item android:drawable="@color/white"/>

</selector>

In the Activity also we can change it like

view.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
      switch(event.getAction())
      {
          case MotionEvent.ACTION_DOWN:
              v.setBackgroundResource(R.drawable.pressed);
              lastY = event.getY();
              break;
          case MotionEvent.ACTION_UP:
              v.setBackgroundResource(R.drawable.normal);
              break;
          case MotionEvent.ACTION_MOVE:
              float abs = Math.abs(lastY - event.getY());

              if(abs > TIME_DELAY) // TIME_DELAY=2
                  v.setBackgroundResource(R.drawable.normal);
              break;
      }
      return true;
  }
});

You can change the time delay as low as possible so that the background changes quickly.

Actually i solved this Problem just now. Need to add android:clickable="true" in custom textview. Now this is working fine. But i keep this question to help others.

The best way to do this is to use onTouchListener or onClickListener:

    final com.example.shuvo.KeyboardButton text = findViewById(R.id.showMenuButton);
    text.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // change the background color
            text.setBackgroundColor(color);
            text.setBackgroundResource(drawable);
            text.setTextColor(color);
            return false;
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM