简体   繁体   中英

textview background color is not changing on click in popupwindow

I am using PopUpwindow with textviews in it. Problem is When i click any of the textvies the background color is not changing though it is changing when the textview is focused but not on click.

After clicking i am dismissing the popupwindow , and if i don't dismiss the popupwindow then the background color changes according to the selector :

This is my textview background selector:

<item android:state_focused="true" android:drawable="@drawable/focused" />    
<item android:state_pressed="true" android:drawable="@drawable/pressed" />
<item android:drawable="@drawable/priornone" /> <!-- default --> </selector> 

in my popupwindow all i am doing is this :

TextView manage_list = (TextView)popupView.findViewById(R.id.manage_lists);
manage_list.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) 
{

  Intent myIntent = new Intent(v.getContext(),ManageList.class);
      popupWindow.dismiss();
  startActivity(myIntent);

 }});

layout file for popupwindow:

<?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
  android:background="@drawable/pop_menu_bg"
 android:orientation="vertical"
    >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/manage_lists"
    android:text="Manage lists"
    android:background="@drawable/my_drawable"
 >
 </TextView>


</LinearLayout>

Its quite strange behaviour everything works well if i don't dismiss the popupwindow but if i dismiss the popupwindow on click textview background doesn't changes.

What am i doing Wrong? Any help will be appreciated.

I believe if you use the above code, you will be ok:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/focused" />
<item android:state_pressed="true" android:drawable="@drawable/activated" /> 
<item android:drawable="@drawable/priornone" />
</selector>

You cannot define two different states in an item.

Hope that helps.

//you need to remove android:state_pressed="true" when android:state_focused="true" is also true.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

refer here:

EDIT: you need to give your Linearlayout attribute as android:clickable="false"

You will use your TextView like Checkbox, isn't it?

Use a boolean flag to try this.

private boolean clicked = false;

// ...

mytextView.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){
        clicked = !clicked;

        if(clicked){
            mytextView.setBackgroundColor(yourcolorclicked);
        }else{
            mytextView.setBackgroundColor(yourcolorunclicked);
        }
        mytextView.invalidate();
    }
});

Check if you have a naming conflict. In case none of your changes are showing up, the possibility of it not working because of some kind of a naming issue conflicting with an imported library could be your main problem.

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