Create /res/values/arrays.xml to define drawable and int, used to load drawable and background color in our ImageViews.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="resicon">
<item>@android:drawable/ic_dialog_alert</item>
<item>@android:drawable/ic_menu_camera</item>
<item>@android:drawable/ic_menu_compass</item>
<item>@android:drawable/ic_menu_directions</item>
<item>@android:drawable/ic_menu_gallery</item>
</array>
<array name="rescolor">
<item>#FF101010</item>
<item>#FF202020</item>
<item>#FF303030</item>
<item>#FF404040</item>
<item>#FF505050</item>
</array>
</resources>
package com.example.androidxmlresources;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.res.TypedArray;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TypedArray arrayResources = getResources().obtainTypedArray(R.array.resicon);
TypedArray arrayColors = getResources().obtainTypedArray(R.array.rescolor);
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
TextView textView = new TextView(this);
textView.setText("android-er.blogspot.com");
layout.addView(textView);
for(int i=0; i<arrayResources.length(); i++){
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(arrayResources.getDrawable(i));
imageView.setBackgroundColor(arrayColors.getColor(i, 0xFF000000));
layout.addView(imageView);
}
setContentView(layout);
arrayResources.recycle();
arrayColors.recycle();
}
}