I have multiple linear layouts inside a ScrollView .Each linear layout have a image on click of which i want to set the linear layout background as selected as we 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 like this onclick of the imageView:
layout1.setBackgroundColor(Color.BLUE);
But this is not giving me the desired output .Please help me in this how we can do this
change the selected State :
public void changeState(){
for (int i = 0; i < mainLayout.getChildCount(); i++) {
View child = mainLayout.getChildAt(i);
child.setSelected(false);
}
}
Create a xml drawable in your drawable folder ex: background_linear.xml
write the 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 as background for your LinearLayout in your xml layout.
And on click of image view call this method
layout1.setSelected(true);
To remove the selection you have to call same method by passing false.
If you have multiple LinearLayout then you should remember which one was previously selected layout. To achieve this you can do this:
Define an int to store id of the previously 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 you don't want set ClickListener to every ImageView in java then set onClick property of all ImageView in xml to same method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With