Wednesday, August 19, 2015

List all available drawable and its value

Example to list all available Drawable and its value:


package com.example.androidlistdrawable;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView info = (TextView)findViewById(R.id.info);

        Field[] fieldDrawables = android.R.drawable.class.getFields();
        for(int i=0; i<fieldDrawables.length; i++){
            Field field = fieldDrawables[i];
            info.append("R.drawable." + field.getName() + " :\n");
            info.append(field.toString() + "\n");

            try {
                int value = (int) field.get(fieldDrawables);
                info.append("value = " + value + "\n");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            info.append("\n");

        }
    }

}


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
</LinearLayout>



Next:
- RecyclerView + CardView example - list available Drawable

No comments: