Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Tuesday, January 6, 2015

Load resources from XML

This example show how to load resources from xml.


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();
 }

}

Tuesday, December 10, 2013

Include custom view in XML

The exercise show how to refer custom view in XML.

Include custom view in XML


Create custom view extending View in separated file, MyView.java.
package com.example.androidpaint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {
 
 Bitmap myBitmap;
 int minWidth, minHeight;

 public MyView(Context context) {
  super(context);
  init();
 }

 public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }

 public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init();
 }
 
 private void init(){
  myBitmap = BitmapFactory.decodeResource(
    getResources(), R.drawable.ic_launcher);
  minWidth = myBitmap.getWidth();
  minHeight = myBitmap.getHeight();
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.drawBitmap(myBitmap, 0, 0, null);
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  setMeasuredDimension(minWidth, minHeight);
 }
 
}


Include <com.example.androidpaint.MyView> in layout XML. where com.example.androidpaint is our package.
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        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" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button"/>
    
    <com.example.androidpaint.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <com.example.androidpaint.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="another button"/>

</LinearLayout>