Jan 31, 2014

Add shadow to EditText

To add shadow to EditText, simple add the following to XML inside <EditText>:

        android:shadowColor="#000000"
        android:shadowDx="5.0"
        android:shadowDy="5.0"
        android:shadowRadius="5.0" 


Example to shadow effect to EditText in the example "Implement TextWatcher to EditText".

EditText with shadow
EditText with shadow
Example:
<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:shadowColor="#000000"
        android:shadowDx="5.0"
        android:shadowDy="5.0"
        android:shadowRadius="5.0" 

        android:textSize="28sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:text="afterTextChanged(Editable s)" />
    <TextView
        android:id="@+id/promptAfter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:text="beforeTextChanged(CharSequence s, int start, int count, int after)" />
    <TextView
        android:id="@+id/promptBefore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:text="onTextChanged(CharSequence s, int start, int before, int count)" />
    <TextView
        android:id="@+id/promptOn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
 
</LinearLayout>

Jan 28, 2014

Implement auto scroll marquee TextView in Spinner

Last post demonstrate how to "Implement auto scroll marquee TextView in ListView". It can be applied on Spinner also.

Implement auto scroll marquee TextView in Spinner
Implement auto scroll marquee TextView in Spinner

MainActivity.java
package com.example.androidcustomspinner;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 private class MyObject {
  private int number;
  private String name;

  MyObject(int num, String nam) {
   number = num;
   name = nam;
  }

  public int getNumber() {
   return number;
  }

  public String getName() {
   return name;
  }
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Spinner spinner = (Spinner) findViewById(R.id.spinner);

  // Init ArrayList of MyObject
  ArrayList<MyObject> myArrayList = new ArrayList<MyObject>();
  myArrayList.add(new MyObject(0, "Sunday Sunday Sunday Sunday Sunday"));
  myArrayList.add(new MyObject(1, "Monday Monday Monday Monday Monday"));
  myArrayList.add(new MyObject(2, "Tuesday Tuesday Tuesday Tuesday Tuesday"));
  myArrayList.add(new MyObject(3, "Wednesday Wednesday Wednesday Wednesday Wednesday"));
  myArrayList.add(new MyObject(4, "Thursday Thursday Thursday Thursday Thursday"));
  myArrayList.add(new MyObject(5, "Friday Friday Friday Friday Friday"));
  myArrayList.add(new MyObject(6, "Saturday Saturday Saturday Saturday Saturday"));

  MyAdapter myAdapter = new MyAdapter(this, myArrayList);
  spinner.setAdapter(myAdapter);
  
  spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

   @Override
   public void onItemSelected(AdapterView<?> parent, View view, 
     int position, long id) {
    MyObject clickedObj = (MyObject)parent.getItemAtPosition(position);
       Toast.makeText(MainActivity.this, 
         "Clicked item:\n" +
         clickedObj.getNumber() + ": " +
         clickedObj.getName(), 
         Toast.LENGTH_LONG).show();
   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {
   }});

 }

 private class MyAdapter extends BaseAdapter {

  private ArrayList<MyObject> myList;

  private Activity parentActivity;
  private LayoutInflater inflater;

  public MyAdapter(Activity parent, ArrayList<MyObject> l) {
   parentActivity = parent;
   myList = l;
   inflater = (LayoutInflater) parentActivity
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public int getCount() {
   return myList.size();
  }

  @Override
  public Object getItem(int position) {
   return myList.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   View view = convertView;
   if (convertView == null)
    view = inflater.inflate(R.layout.row, null);

   TextView text1 = (TextView) view.findViewById(R.id.text1);
   TextView text2 = (TextView) view.findViewById(R.id.text2);
   MyObject myObj = myList.get(position);
   text1.setText(String.valueOf(myObj.getNumber()));
   text2.setText(myObj.getName());
   text2.setSelected(true);
   return view;
  }

 }
}

row.xml
<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:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:textStyle="bold"/>
 
</LinearLayout>

activity_main.xml
<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:text="android-coding.blogspot.com" />
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
</LinearLayout>

Implement auto scroll marquee TextView in ListView

This example implement auto scroll TextView in custom ListView to "List System Properties of Android system".

Implement auto scroll marquee TextView in ListView
Implement auto scroll marquee TextView in ListView
Reference:
To make a TextView auto-run, if the text is longer than display area, add the line to <TextView> in layout file:
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"


And call textview.setSelected(true) in Java code.

MainActivity.java
package com.example.androidcustomlistview;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

 private class SystemProperties{
  private String propertiesName;
  private String properties;
  
  SystemProperties(String pName, String p){
   propertiesName = pName;
   properties = p; 
  }
  
  public String getPropertiesName(){
   return propertiesName;
  }
  
  public String getpPoperties(){
   return properties;
  }
 } 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ListView listView = (ListView)findViewById(R.id.listview);
  
  //Init ArrayList of MyObject
  ArrayList<SystemProperties> myPropertyList = new ArrayList<SystemProperties>();
  
  //get System Properties
  Properties properties = System.getProperties();
  Enumeration<String> propEnum = 
                (Enumeration<String>)properties.propertyNames();
         
        while(propEnum.hasMoreElements()){
            String propName = propEnum.nextElement();
            String prop = System.getProperty(propName);
            SystemProperties nexProp = new SystemProperties(propName, prop);
            myPropertyList.add(nexProp);
        }
  
  
  MyAdapter myAdapter = new MyAdapter(this, myPropertyList);
  listView.setAdapter(myAdapter);
  
  listView.setOnItemClickListener(new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, 
     View view, int position, long id) {
    SystemProperties clickedObj = (SystemProperties)parent.getItemAtPosition(position);
    Toast.makeText(MainActivity.this, 
      clickedObj.getPropertiesName() + ":\n" +
      clickedObj.getpPoperties(), 
      Toast.LENGTH_LONG).show();
   }});
  
 }

 private class MyAdapter extends BaseAdapter {
  
  private ArrayList<SystemProperties> myList;
  
  private Activity parentActivity;
     private LayoutInflater inflater; 
  
     public MyAdapter(Activity parent, ArrayList<SystemProperties> l) {
      parentActivity = parent;
         myList=l;
         inflater = (LayoutInflater)parentActivity
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }

  @Override
  public int getCount() {
   return myList.size();
  }

  @Override
  public Object getItem(int position) {
   return myList.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView (int position, View convertView, 
    ViewGroup parent) {
   View view = convertView;
         if(convertView==null)
             view = inflater.inflate(R.layout.row, null);
  
         TextView text1 = (TextView)view.findViewById(R.id.text1);
         TextView text2 = (TextView)view.findViewById(R.id.text2);
         SystemProperties myObj = myList.get(position);
         text1.setText(String.valueOf(myObj.getPropertiesName()));
         text2.setText(myObj.getpPoperties());
         text2.setSelected(true);
         return view;
  }
 }
}

row.xml
<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:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:textStyle="bold"/>

</LinearLayout>

activity_main.xml
<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:text="android-coding.blogspot.com" />
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Also read: Implement auto scroll marquee TextView in Spinner

Jan 25, 2014

Implement TextWatcher to EditText

This example implement TextWatcher, attach to EditText with addTextChangedListener(). Its methods will be called when the text is changed.

Implement TextWatcher to EditText
Implement TextWatcher to EditText

package com.example.androidedittext;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 TextView promptAfter, promptBefore, promptOn;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  promptAfter = (TextView)findViewById(R.id.promptAfter);
  promptBefore = (TextView)findViewById(R.id.promptBefore);
  promptOn = (TextView)findViewById(R.id.promptOn);
  
  EditText input = (EditText)findViewById(R.id.input);
  input.addTextChangedListener(textWatcher);
 }

 TextWatcher textWatcher = new TextWatcher(){

  @Override
  public void afterTextChanged(Editable s) {
   promptAfter.setText(s.toString());
  }

  @Override
  public void beforeTextChanged(CharSequence s, 
    int start, int count, int after) {
   promptBefore.setText(s + "\n" + 
    start + " / " + count + " / " + after);
  }

  @Override
  public void onTextChanged(CharSequence s, 
    int start, int before, int count) {
   promptOn.setText(s + "\n" 
    + start + " / " + before + " / " + count);
  }
  
 };

}

<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <EditText
        android:id="@+id/input"
     android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:text="afterTextChanged(Editable s)" />
    <TextView
        android:id="@+id/promptAfter"
     android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:text="beforeTextChanged(CharSequence s, int start, int count, int after)" />
    <TextView
        android:id="@+id/promptBefore"
     android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:text="onTextChanged(CharSequence s, int start, int before, int count)" />
    <TextView
        android:id="@+id/promptOn"
     android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
 
</LinearLayout>

Jan 24, 2014

Implement custom shape for EditText

Example show how to implement custom shape for EditText.

EditText with custom shape
EditText with custom shape
Create XML files in /res/drawable/ folder to define shape.

/res/drawable/topbuttonshape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid
        android:color="#a0a0a0" >
    </solid>

    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" >
    </corners>

</shape>

/res/drawable/midbuttonshape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid
        android:color="#b0b0b0" >
    </solid>

    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp" >
    </corners>

</shape>

/res/drawable/downbuttonshape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid
        android:color="#c0c0c0" >
    </solid>

    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp" >
    </corners>

</shape>

Use the shapes in layout XML android:background
<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <EditText
     android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28sp"
        android:background="@drawable/topbuttonshape"
        android:hint="topbuttonshape" />
    <EditText
     android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28sp"
        android:background="@drawable/midbuttonshape"
        android:hint="midbuttonshape" />
 <EditText
     android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28sp"
        android:background="@drawable/downbuttonshape"
        android:hint="downbuttonshape" />
 
</LinearLayout>

Jan 19, 2014

Copy drawable between ImageViews

Example to get drawable from one ImageView, then setImageDrawable to another ImageView.

Copy drawable between ImageViews
Copy drawable between ImageViews

package com.example.androidcopyimage;

import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.drawable.Drawable;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  ImageView source = (ImageView)findViewById(R.id.source);
  ImageView target = (ImageView)findViewById(R.id.target);
  
  Drawable drawable = source.getDrawable();
  target.setImageDrawable(drawable);
 }
}

<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:text="android-coding.blogspot.com" />
    <ImageView
        android:id="@+id/source"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
    <ImageView
        android:id="@+id/target"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
        

</LinearLayout>

Jan 13, 2014

ViewStub

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime.
ViewStub
ViewStub

Main layout, to include <ViewStub>.
<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" >

    <ViewStub
        android:id="@+id/mystub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inflatedId="@+id/mytitle"
        android:layout="@layout/titlebar" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <Button
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show ViewStub" />

</LinearLayout>

titlebar.xml, to be inflated in <ViewStub>.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:color/background_dark">
 
    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/ic_launcher" />
     
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
         
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Android-Coding"
            android:textColor="@android:color/white"
            android:textStyle="bold"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="http://android-coding.blogspot.com/"
            android:textColor="@android:color/white"
            android:textStyle="italic"/>
         
    </LinearLayout>
</LinearLayout>

Inflate the ViewStub in java code.
package com.example.androidviewstub;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {
 
 ViewStub myViewStub;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  myViewStub = (ViewStub) findViewById(R.id.mystub);
  
  Button buttonShow = (Button)findViewById(R.id.show);
  buttonShow.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    if(myViewStub.getParent() != null){
     myViewStub.inflate();
    }else{
     Toast.makeText(MainActivity.this, 
       "myViewStub.getParent() == null: replaced", 
       Toast.LENGTH_LONG).show();
    }
    
   }});
 }

}

Jan 12, 2014

Re-use Layouts with include

This example show how to use layout defined in seperated XML, using <include>. Such that you can re-use common view in your layout, or in separated layout.

Re-use Layouts with <include/>
The same layour included twice using <include>
Create the re-use layout, /res/layout/titlebar.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:color/background_dark">

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content" 
               android:src="@drawable/ic_launcher" />
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Android-Coding"
            android:textColor="@android:color/white"
            android:textStyle="bold"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="http://android-coding.blogspot.com/"
            android:textColor="@android:color/white"
            android:textStyle="italic"/>
        
    </LinearLayout>
</LinearLayout>

Include titlebar.xml in our main layout:
<RelativeLayout 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"
    tools:context=".MainActivity" >
    
    <include layout="@layout/titlebar"
        android:id="@+id/topbanner"
        android:layout_alignParentTop="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/topbanner"
        android:text="@string/hello_world" />
    
    <include layout="@layout/titlebar"
        android:id="@+id/topbanner2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>


Jan 6, 2014

Custom Spinner with OnItemSelectedListener

Example to implement custom Spinner with OnItemSelectedListener.

Custom Spinner with OnItemSelectedListener
Custom Spinner with OnItemSelectedListener

package com.example.androidcustomspinner;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 private class MyObject {
  private int number;
  private String name;

  MyObject(int num, String nam) {
   number = num;
   name = nam;
  }

  public int getNumber() {
   return number;
  }

  public String getName() {
   return name;
  }
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Spinner spinner = (Spinner) findViewById(R.id.spinner);

  // Init ArrayList of MyObject
  ArrayList<MyObject> myArrayList = new ArrayList<MyObject>();
  myArrayList.add(new MyObject(0, "Sunday"));
  myArrayList.add(new MyObject(1, "Monday"));
  myArrayList.add(new MyObject(2, "Tuesday"));
  myArrayList.add(new MyObject(3, "Wednesday"));
  myArrayList.add(new MyObject(4, "Thursday"));
  myArrayList.add(new MyObject(5, "Friday"));
  myArrayList.add(new MyObject(6, "Saturday"));

  MyAdapter myAdapter = new MyAdapter(this, myArrayList);
  spinner.setAdapter(myAdapter);
  
  spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

   @Override
   public void onItemSelected(AdapterView<?> parent, View view, 
     int position, long id) {
    MyObject clickedObj = (MyObject)parent.getItemAtPosition(position);
       Toast.makeText(MainActivity.this, 
         "Clicked item:\n" +
         clickedObj.getNumber() + ": " +
         clickedObj.getName(), 
         Toast.LENGTH_LONG).show();
   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {
   }});

 }

 private class MyAdapter extends BaseAdapter {

  private ArrayList<MyObject> myList;

  private Activity parentActivity;
  private LayoutInflater inflater;

  public MyAdapter(Activity parent, ArrayList<MyObject> l) {
   parentActivity = parent;
   myList = l;
   inflater = (LayoutInflater) parentActivity
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public int getCount() {
   return myList.size();
  }

  @Override
  public Object getItem(int position) {
   return myList.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   View view = convertView;
   if (convertView == null)
    view = inflater.inflate(R.layout.row, null);

   TextView text1 = (TextView) view.findViewById(R.id.text1);
   TextView text2 = (TextView) view.findViewById(R.id.text2);
   MyObject myObj = myList.get(position);
   text1.setText(String.valueOf(myObj.getNumber()));
   text2.setText(myObj.getName());
   return view;
  }

 }
}

row.xml, define the layout of individual row in the custom Spinner.
<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:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"/>
 
</LinearLayout>

Main layout
<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:text="android-coding.blogspot.com" />
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
</LinearLayout>

Also read: Implement auto scroll marquee TextView in Spinner

Jan 5, 2014

List System Properties of Android system

The example call getProperties() method of java.lang.System class to get the system properties. It modify the custom ListView from last example "Implement OnItemClickListener for custom ListView" and "Create custom ListView of custom object".

List System Properties
List System Properties
MainActivity.java
package com.example.androidcustomlistview;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

 private class SystemProperties{
  private String propertiesName;
  private String properties;
  
  SystemProperties(String pName, String p){
   propertiesName = pName;
   properties = p; 
  }
  
  public String getPropertiesName(){
   return propertiesName;
  }
  
  public String getpPoperties(){
   return properties;
  }
 } 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ListView listView = (ListView)findViewById(R.id.listview);
  
  //Init ArrayList of MyObject
  ArrayList<SystemProperties> myPropertyList = new ArrayList<SystemProperties>();
  
  //get System Properties
  Properties properties = System.getProperties();
  Enumeration<String> propEnum = 
                (Enumeration<String>)properties.propertyNames();
         
        while(propEnum.hasMoreElements()){
            String propName = propEnum.nextElement();
            String prop = System.getProperty(propName);
            SystemProperties nexProp = new SystemProperties(propName, prop);
            myPropertyList.add(nexProp);
        }
  
  
  MyAdapter myAdapter = new MyAdapter(this, myPropertyList);
  listView.setAdapter(myAdapter);
  
  listView.setOnItemClickListener(new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, 
     View view, int position, long id) {
    SystemProperties clickedObj = (SystemProperties)parent.getItemAtPosition(position);
    Toast.makeText(MainActivity.this, 
      clickedObj.getPropertiesName() + ":\n" +
      clickedObj.getpPoperties(), 
      Toast.LENGTH_LONG).show();
   }});
  
 }

 private class MyAdapter extends BaseAdapter {
  
  private ArrayList<SystemProperties> myList;
  
  private Activity parentActivity;
     private LayoutInflater inflater; 
  
     public MyAdapter(Activity parent, ArrayList<SystemProperties> l) {
      parentActivity = parent;
         myList=l;
         inflater = (LayoutInflater)parentActivity
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }

  @Override
  public int getCount() {
   return myList.size();
  }

  @Override
  public Object getItem(int position) {
   return myList.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView (int position, View convertView, 
    ViewGroup parent) {
   View view = convertView;
         if(convertView==null)
             view = inflater.inflate(R.layout.row, null);
  
         TextView text1 = (TextView)view.findViewById(R.id.text1);
         TextView text2 = (TextView)view.findViewById(R.id.text2);
         SystemProperties myObj = myList.get(position);
         text1.setText(String.valueOf(myObj.getPropertiesName()));
         text2.setText(myObj.getpPoperties());
         return view;
  }
 }
}

For thw XMLs of the main layout and the ListView row layout, refer to the post "Create custom ListView of custom object".

Next: Implement auto scroll marquee TextView in ListView

Implement OnItemClickListener for custom ListView

This example implement OnItemClickListener for custom List in last post "Create custom ListView of custom object" to get and display the clicked item.

Implement OnItemClickListener for custom ListView
Implement OnItemClickListener for custom ListView

package com.example.androidcustomlistview;

import java.util.ArrayList;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

 private class MyObject{
  private int number;
  private String name;
  
  MyObject(int num, String nam){
   number = num;
   name = nam; 
  }
  
  public int getNumber(){
   return number;
  }
  
  public String getName(){
   return name;
  }
 } 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ListView listView = (ListView)findViewById(R.id.listview);
  
  //Init ArrayList of MyObject
  ArrayList<MyObject> myArrayList = new ArrayList<MyObject>();
  myArrayList.add(new MyObject(0, "Sunday"));
  myArrayList.add(new MyObject(1, "Monday"));
  myArrayList.add(new MyObject(2, "Tuesday"));
  myArrayList.add(new MyObject(3, "Wednesday"));
  myArrayList.add(new MyObject(4, "Thursday"));
  myArrayList.add(new MyObject(5, "Friday"));
  myArrayList.add(new MyObject(6, "Saturday"));
  
  MyAdapter myAdapter = new MyAdapter(this, myArrayList);
  listView.setAdapter(myAdapter);
  
  listView.setOnItemClickListener(new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, 
     View view, int position, long id) {
    MyObject clickedObj = (MyObject)parent.getItemAtPosition(position);
    Toast.makeText(MainActivity.this, 
      "Clicked item:\n" +
      clickedObj.getNumber() + ": " +
      clickedObj.getName(), 
      Toast.LENGTH_LONG).show();
   }});
  
 }

 private class MyAdapter extends BaseAdapter {
  
  private ArrayList<MyObject> myList;
  
  private Activity parentActivity;
     private LayoutInflater inflater; 
  
     public MyAdapter(Activity parent, ArrayList<MyObject> l) {
      parentActivity = parent;
         myList=l;
         inflater = (LayoutInflater)parentActivity
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }

  @Override
  public int getCount() {
   return myList.size();
  }

  @Override
  public Object getItem(int position) {
   return myList.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView (int position, View convertView, 
    ViewGroup parent) {
   View view = convertView;
         if(convertView==null)
             view = inflater.inflate(R.layout.row, null);
  
         TextView text1 = (TextView)view.findViewById(R.id.text1);
         TextView text2 = (TextView)view.findViewById(R.id.text2);
         MyObject myObj = myList.get(position);
         text1.setText(String.valueOf(myObj.getNumber()));
         text2.setText(myObj.getName());
         return view;
  }
 }
}

Create custom ListView of custom object

This example create ListView using custom adapter for custom object.

Create custom ListView of custom object
Create custom ListView of custom object

package com.example.androidcustomlistview;

import java.util.ArrayList;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

 private class MyObject{
  private int number;
  private String name;
  
  MyObject(int num, String nam){
   number = num;
   name = nam; 
  }
  
  public int getNumber(){
   return number;
  }
  
  public String getName(){
   return name;
  }
 } 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ListView listView = (ListView)findViewById(R.id.listview);
  
  //Init ArrayList of MyObject
  ArrayList<MyObject> myArrayList = new ArrayList<MyObject>();
  myArrayList.add(new MyObject(0, "Sunday"));
  myArrayList.add(new MyObject(1, "Monday"));
  myArrayList.add(new MyObject(2, "Tuesday"));
  myArrayList.add(new MyObject(3, "Wednesday"));
  myArrayList.add(new MyObject(4, "Thursday"));
  myArrayList.add(new MyObject(5, "Friday"));
  myArrayList.add(new MyObject(6, "Saturday"));
  
  MyAdapter myAdapter = new MyAdapter(this, myArrayList);
  listView.setAdapter(myAdapter);
  
 }

 private class MyAdapter extends BaseAdapter {
  
  private ArrayList<MyObject> myList;
  
  private Activity parentActivity;
     private LayoutInflater inflater; 
  
     public MyAdapter(Activity parent, ArrayList<MyObject> l) {
      parentActivity = parent;
         myList=l;
         inflater = (LayoutInflater)parentActivity
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }

  @Override
  public int getCount() {
   return myList.size();
  }

  @Override
  public Object getItem(int position) {
   return myList.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView (int position, View convertView, 
    ViewGroup parent) {
   View view = convertView;
         if(convertView==null)
             view = inflater.inflate(R.layout.row, null);
  
         TextView text1 = (TextView)view.findViewById(R.id.text1);
         TextView text2 = (TextView)view.findViewById(R.id.text2);
         MyObject myObj = myList.get(position);
         text1.setText(String.valueOf(myObj.getNumber()));
         text2.setText(myObj.getName());
         return view;
  }
 }
}

row.xml, define the layout of individual row in the list.
<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:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"/>

</LinearLayout>

Main layout
<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:text="android-coding.blogspot.com" />
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Next: Implement OnItemClickListener for custom ListView

Jan 4, 2014

DrawerLayout with custom Layout/View

The post "Example of Navigation Drawer" show a android.support.v4.widget.DrawerLayout with ListView inside. We can also place our layout/view inside DrawerLayout.

DrawerLayout with custom Layout/View
DrawerLayout with custom Layout/View

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        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"
        tools:context=".MainActivity" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="android-coding.blogspot.com" />
    </RelativeLayout>

    <LinearLayout 
        android:id="@+id/drawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical"
        android:background="@android:color/background_dark"
        android:padding="5dp" >
        
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="android-coding"/>
        <Button
            android:id="@+id/drawerbutton"
         android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="Hello"/>
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

package com.example.androidnavigationdrawer;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

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

  Button buttonHello = (Button)findViewById(R.id.drawerbutton);
  buttonHello.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Toast.makeText(MainActivity.this, 
      "Hello Android-Coding", 
      Toast.LENGTH_LONG).show();
   }});
 }

}

Infolinks In Text Ads