You often need to display icons in your Android apps. Instead of
designing your own, the Android system comes with a set of icons that you can
use in your application.
The application icons that come bundled with Android is
accessible through the android.R.drawable
class. For example, if you want to
display a Share icon on your ImageButton, you simple use the setImageResource() method of the view,
like this:
btnShare =
(ImageButton) findViewById(R.id.btnShare);
btnShare.setImageResource(
android.R.drawable.ic_menu_share);
The following code snippet shows how you can use Reflection to go
through the list of constants defined in the android.R.drawable class and
display each icon:
package net.learn2develop.appicons.appicons;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.lang.reflect.Field;
public class MainActivity extends ActionBarActivity {
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---display
all the icons defined in
// android.R.drawable---
DisplayDrawables(android.R.drawable.class);
}
//---use reflection
to examine the fields in a class---
public void DisplayDrawables(Class
_class){
LinearLayout
linearLayout = (LinearLayout)
findViewById(R.id.linearLayout);
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
final Field[]
fields = _class.getDeclaredFields();
for (Field
field : fields) {
final int
drawableId;
try {
drawableId = field.getInt(_class);
ImageButton imageButton = new
ImageButton(this);
imageButton.setImageResource(drawableId);
imageButton.setLayoutParams(lp);
imageButton.setBackgroundColor(
Color.TRANSPARENT);
linearLayout.addView(imageButton);
TextView txtView = new TextView(this);
txtView.setText(field.getName());
imageButton.setLayoutParams(lp);
imageButton.setBackgroundColor(
Color.TRANSPARENT);
linearLayout.addView(txtView);
} catch
(Exception e) {
continue;
}
}
}
In the activity_main.xml,
you use a ScrollView to contain a LinearLayout:
<ScrollView xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft=
"@dimen/activity_horizontal_margin"
android:paddingRight=
"@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom=
"@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=
"net.learn2develop.appicons.appicons.MainActivity">
</LinearLayout>
</ScrollView>
The following screenshot shows the application icons and their corresponding
names: