-1

i'm working on a listview in an activity and i want when ever i longpressed any listitem the Button on the action bar changes its color like as get highlihted as suppose " i select list item the button on the action bar changes from white to gray" .

   listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // changes the color button ;
            return false;
        }
    });

or is there any other way that i can do this.

I dont have enough reputation to post a pictures Firstly it looks like Before selecting any item and its looks like thisWhen item is long pressed or selected hope you understand my probleum , Thanks in advance

androidcodes
  • 141
  • 1
  • 3
  • 11

2 Answers2

0

Edit: as misunderstood original question:

int mPosition = -1;   

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {

        if(mPosition!=-1) {
            //reset color of previously pressed item to white
            lv.getChildAt(mPosition).setBackgroundColor(Color.WHITE);
        }
        //set current item to fray.
        lv.getChildAt(pos).setBackgroundColor(Color.GRAY);
        //put value of currently selected item to mPosition.
        mPosition = pos;

        return true;
    }
});

Old answer below:

Add this into your method.

Button button1 = (Button)findViewById(R.id.button1);
if(this condition){
    button1.setBackgroundColor(Color.GRAY);
}else{
    //whatever.. 
}

This answer covers the same principle, but in more detail using the resources.

Community
  • 1
  • 1
  • bro it is needed to be changed dynamically, like we do in tab bar that if "if item is selected " – androidcodes Feb 02 '15 at 05:49
  • i'm using a listview . and whenever any list item is long pressed the button on the **action bar** changes its color – androidcodes Feb 02 '15 at 05:58
  • https://www.dropbox.com/s/glmqrb4l26vbj6c/Screenshot_2015-02-02-11-31-11.png?dl=0 you can lookupon this image in this item is selected and things on action bar changes – androidcodes Feb 02 '15 at 06:05
  • Half of the part is done "that is changing the color of listitem " but i also want actionbar to change its color and buttons" whenanyof item is selected as shown in above picture as happening in **gmail app** – androidcodes Feb 02 '15 at 10:52
0

you can give color to list item when selected with

android:listSelector="@color/AnyColor" in .xml file .. see coding

<ListView
    android:id="@+id/nameOfYourListview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:background="AnyYouWant"
   <!--code for color the selected item --> android:listSelector="ColorHereWhichYouWant" >
</ListView>
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83