Reputation: 1816
I have two text views whose background image I want to change.
<TextView
android:layout_width="@dimen/margin_0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_margin="@dimen/margin_padding_2dp"
android:padding="@dimen/margin_padding_5dp"
android:text="Trainings"
android:gravity="center"
android:textSize="17sp"
android:background="@drawable/background_selector"
android:clickable="true"
/>
<TextView
android:id="@+id/learning_programs"
android:layout_width="@dimen/margin_0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Learning Programs"
android:padding="@dimen/margin_padding_5dp"
android:textSize="@dimen/text_size_seventeen"
android:background="@drawable/background_selector"
android:gravity="center"
android:clickable="true"
/>
So, what I want is that when I click on a TextView the background image should change. For that, I have a selector in drawable as
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/rounded_corner" android:state_pressed="true"/>
<item android:drawable="@drawable/rounded_corner" android:state_focused="true"/>
<item android:drawable="@drawable/rounded_corner_grey"/>
</selector>
But the problem with the above code is that it retains the white color only when the user taps on the TextView. Once the finger of the user moves, the color changes to grey.
What I want is that the color should become white after the user has clicked on the TextView once.
With state_selected:true
both the TextViews become white and non-clickable.
Upvotes: 0
Views: 594
Reputation: 4132
Add this line of code to both textviews.
android:focusableInTouchMode="true"
This means that the view can get the focus when the phone is in touch mode.
EDIT: Try this xml and check if it works.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="@dimen/margin_0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_padding_2dp"
android:layout_weight="0.5"
android:background="@drawable/background_selector"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:padding="@dimen/margin_padding_5dp"
android:text="Trainings"
android:textSize="17sp" />
<TextView
android:id="@+id/learning_programs"
android:layout_width="@dimen/margin_0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="@drawable/background_selector"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:padding="@dimen/margin_padding_5dp"
android:text="Learning Programs"
android:textSize="@dimen/text_size_seventeen" />
Upvotes: 1
Reputation: 12147
When your finger release from the view, your view is not in selected
or pressed
state. You need to set the pressed
or selected
state manually after click event.
<TextView
android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="@drawable/background_selector"
android:text="Hello World!" />
Then in your code
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(!v.isSelected());
}
});
You can use one OnClickListener
for two TextView
private View.OnClickListener toggleSelectedStateOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(!v.isSelected());
}
};
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setOnClickListener(toggleSelectedStateOnClickListener);
TextView tv1 = new TextView(this);
tv1.setOnClickListener(toggleSelectedStateOnClickListener);
Upvotes: 1
Reputation: 19
In my case, i use this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/bottom_nav_normal" android:state_enabled="true"/>
<item android:color="@color/bottom_nav_checked" android:state_enabled="false"/>
<item android:color="@color/bottom_nav_checked" android:state_pressed="true"/>
</selector>
then, in the code:
TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO
tv.setEnable(false);
}
}
Upvotes: 0