Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android create selector programmatically

Tags:

How can i create selector programmatically?
i have a xml selector that assigned to TabWidget as Tab indicator color

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/bg_tab_unselected" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/bg_tab_selected" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/bg_tab_unselected_focused" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/bg_tab_selected_focused" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/bg_tab_unselected_pressed" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/bg_tab_selected_pressed" />

    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/bg_tab_unselected_pressed" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/bg_tab_selected_pressed" />
</selector>

Is there any way to create above xml code dynamically?

like image 573
KTG Avatar asked Aug 26 '15 12:08

KTG


People also ask

How to create directory programmatically in Android?

Sharad Kumar More Detail Android App Development for Beginners 28 Lectures 5 hours Anu Khanchandani More Detail This example demonstrates how to create directory programmatically in Android Step 1− Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How to use phone selector API in Android?

How to Use Phone Selector API in Android? Phone Selector API is used to detect phone numbers being used in the phone. Using this you can avoid manual input of Phone Number by users and prompt them to choose the desired number. A sample image is given below to get an idea about what we are going to do in this article.

How to create a button state using a selector?

You should define a selector in a xml file under app / res / drawable folder. Such as my_selector.xml You can define several items in the selector definition xml file. Each item include a drawable object (color or image) that will be used for a button state.

How to create custom drawable resources in Android development?

Shape, selector, and layer-list are usually used to create custom drawable resources in android development. Those three XML elements can save a lot of UI resources and time if being used properly. This article will show you how to use them correctly. 1. Custom Drawable File Overview.


2 Answers

you can use it like this:

public static StateListDrawable makeSelector(int color) {
    StateListDrawable res = new StateListDrawable();
    res.setExitFadeDuration(400);
    res.setAlpha(45);
    res.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(color));
    res.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT));
    return res;
}

and then:

view.setBackground(makeSelector(Color.RED));
like image 131
Leebeedev Avatar answered Oct 26 '22 13:10

Leebeedev


You can create the StateListDrawable directly and use addState for adding the states you've defined in XML:

StateListDrawable d = new StateListDrawable();

[...]

int[] sFocusedSelected = { android.R.attr.state_focused, android.R.attr.state_selected };
Drawable dFocusedSelected = getDrawable(R.drawable.bg_tab_selected_focused);
d.addState(sFocusedSelected, dFocusedSelected);

[...]

Same rules apply like for XML:

The selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.

like image 33
tynn Avatar answered Oct 26 '22 11:10

tynn