Android xml layout selector

I wanted to know if there is a selector for layouts, for example, for drawables.

An example of a selector with the ability to draw:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected --> <item android:drawable="@drawable/tab_library_clicked" android:state_selected="true" /> <!-- When not selected --> <item android:drawable="@drawable/tab_library_not_clicked" /> 

But is something like this possible (this code below does not work for me, but it should give you an idea of ​​what I'm trying to execute):

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected --> <item android:layout="@layout/tab_library_clicked" android:state_selected="true" /> <!-- When not selected --> <item android:layout="@layout/tab_library_not_clicked" /> 

+7
source share
4 answers

I may be mistaken, but I am sure that this does not exist.

The most β€œcomplex” thing you can do with an XML selector is probably using a list of available layers to combine multiple drawings into one state.

Alternatively, you can try using the touch_down and touch_up events to programmatically call setVisibility() on the layouts, but I think the performance will be very poor.

+3
source

Well, what you're looking for is a way to propagate the parent state to its child views. Thus, when the state of the indicator changes (selected, but not), the state of the image changes.

Android already has an xml attribute for this.

 android:duplicateParentState="true" 

use this with your child views in the layout file and combine it with the selector. eg:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#f00" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" android:duplicateParentState="true" /> <ImageView android:id="@+id/imageView1" android:duplicateParentState="true" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:src="@drawable/selector" /> 

+1
source

The default selection mechanism provides only alternatives for properties (e.g. usability, etc.), depending on the state of the view. It does not replace the Views themselves. The requested features are not available.

The selected state for the view is bound to one of the *_STATE_SET field values ​​in the View class. The combined view state is retrieved through getDrawableState() .

You can extend the View class and use these states to fulfill some customs. Or, you can develop your own layout controller that tracks state changes in the view tree and replaces child views according to the xml resource.

But the effort is not worth it because:

  • The presentation / layout structure should remain unchanged during user interaction. You should not change the button in the checkbox when the user selects it, replacing views / layouts, as if it were just confused UX-wise.

  • You only need to change the appearance of the view when interacting with the user, which is already provided using the selection elements and colors.

+1
source

There is no android:layout attribute for item in the documentation . It is assumed to be used with android:drawable .

0
source

All Articles