Set linear layout as selected on Click Android -


i have multiple linear layouts inside scrollview .each linear layout have image on click of want set linear layout background selected have in listview .

xml

<scrollview                 android:layout_width="wrap_content"                 android:layout_height="wrap_content">                 <linearlayout                     android:layout_width="fill_parent"                     android:layout_height="wrap_content"                     android:layout_weight="2"                     android:orientation="vertical">                      <linearlayout                         android:id="@+id/layout1"                         android:layout_width="match_parent"                         android:layout_height="70dp"                         android:orientation="vertical">                          <imageview                             android:id="@+id/img1"                             android:layout_width="90dp"                             android:layout_height="50dp"                             android:background="@mipmap/ic_launcher"                             android:layout_gravity="center" />                     </linearlayout>                      <linearlayout                         android:id="@+id/layout2"                         android:layout_width="match_parent"                         android:layout_height="70dp"                         android:orientation="vertical">                          <imageview                             android:id="@+id/img2"                             android:layout_width="90dp"                             android:layout_height="50dp"                             android:background="@mipmap/ic_launcher"                             android:layout_gravity="center" />                     </linearlayout>                      <linearlayout                         android:id="@+id/layout3"                         android:layout_width="match_parent"                         android:layout_height="70dp"                         android:orientation="vertical">                          <imageview                             android:id="@+id/img3"                             android:layout_width="90dp"                             android:layout_height="50dp"                             android:background="@mipmap/ic_launcher"                             android:layout_gravity="center" />                     </linearlayout>                      <linearlayout                         android:id="@+id/layout4"                         android:layout_width="match_parent"                         android:layout_height="70dp"                         android:orientation="vertical">                          <imageview                             android:id="@+id/img4"                             android:layout_width="90dp"                             android:layout_height="50dp"                             android:layout_gravity="center"                             android:background="@mipmap/ic_launcher" />                     </linearlayout>             </scrollview> 

i have done onclick of imageview:

layout1.setbackgroundcolor(color.blue); 

but not giving me desired output .please me in how can this

change selected state :

public void changestate(){         (int = 0; < mainlayout.getchildcount(); i++) {             view child = mainlayout.getchildat(i);             child.setselected(false);         }     } 

create xml drawable in drawable folder ex: background_linear.xml write below code

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">   <item android:state_selected="true" >         <shape android:shape="rectangle">              <solid android:color="required_color_here"/>        </shape> </item> 

then set background_linear background linearlayout in xml layout.

and on click of image view call method
layout1.setselected(true);

to remove selection have call same method passing false. if have multiple linearlayout should remember 1 selected layout. achieve can this: define int store id of selected view.

int previously_selected_layout; 

and in on click of image view

 imageview.setonclicklistener(new view.onclicklistener() {    @override    public void onclick(view view) {      ((view)view.getparent()).setselected(true);      view v1=findviewbyid(previously_selected_layout);      if(v1!=null) v1.setselected(false);      previously_selected_layout=view.getparent().getid();   }  }); 

if don't want set clicklistener every imageview in java set onclick property of imageview in xml same method.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -