Oct 17, 2013

Add View programmatically, using addView() and addContentView()

This example demonstrate how to add View programmatically:

Add View Programmatically, using addView() and addContentView()
Add View Programmatically, using addView() and addContentView()

newButton ("Hello") is added to the existing layout using Layout.addView(). anotherLayout and anotherButton ("I'm another button") are added using addContentView().

package com.example.androidview;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  LinearLayout mainLayout = 
    (LinearLayout)findViewById(R.id.mainlayout);
  
  //newButton added to the existing layout
  Button newButton = new Button(this);
  newButton.setText("Hello");
  mainLayout.addView(newButton);
  
  //anotherLayout and anotherButton added 
  //using addContentView()
  LinearLayout anotherLayout = new LinearLayout(this);
  LinearLayout.LayoutParams linearLayoutParams = 
    new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT,
      LinearLayout.LayoutParams.WRAP_CONTENT);
  
  Button anotherButton = new Button(this);
  anotherButton.setText("I'm another button");
  anotherLayout.addView(anotherButton);
  
  addContentView(anotherLayout, linearLayoutParams);
 }

}


<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"
    android:background="#000080"
    android:id="@+id/mainlayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textStyle="bold|italic"
        android:textSize="50sp"
        android:text="android-coding.blogspot.com" />

</LinearLayout>


Oct 9, 2013

Example of using HandlerThread

android.os.HandlerThread is a handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.

This is a example to implement our custom HandlerThread class, CustomHandlerThread. Start it in onResume() and stop it in onPause().

Example of using HandlerThread
Example of using HandlerThread

package com.example.androidtesthandlerthread;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements Handler.Callback{

 public class CustomHandlerThread extends HandlerThread implements Handler.Callback{
  
  public static final int MSG_FINISHED = 100;
  public static final int MSG_COUNT_UP = 101;
  public static final int MSG_COUNT_DOWN = 102;

  private Handler handler, callback;

  public CustomHandlerThread(String name) {
   super(name);
   // TODO Auto-generated constructor stub
  }

  public CustomHandlerThread(String name, int priority) {
   super(name, priority);
   // TODO Auto-generated constructor stub
  }
  
  public void setCallback(Handler cb){
   callback = cb;
  }

  @Override
  protected void onLooperPrepared() {
   handler = new Handler(getLooper(), this);
  }

  @Override
  public boolean handleMessage(Message msg) {
   
   int data1 = msg.arg1;
   int data2 = msg.arg2;
   int counter;
   
   switch(msg.what){
   case MSG_COUNT_UP:
    for(counter=data1; counter < data2; counter++){
     //...
    }
    callback.sendMessage(Message.obtain(null, MSG_FINISHED, counter));
    break;
   case MSG_COUNT_DOWN:
    for(counter=data1; counter > data2; counter--){
     //...
    }
    callback.sendMessage(Message.obtain(null, MSG_FINISHED, counter));
    break;
   }
   return true;
  }
  
  public void querySomething(int start, int end){
   if(start > end){
    Message msg = Message.obtain(
      null,   //Handler h 
      MSG_COUNT_DOWN, //int what 
      start,   //int arg1 
      end);   //int arg2
    handler.sendMessage(msg);
   }else if(start < end){
    Message msg = Message.obtain(
      null,   //Handler h 
      MSG_COUNT_UP, //int what 
      start,   //int arg1 
      end);   //int arg2
    handler.sendMessage(msg);
   }
  }
 }
 
 private final static String MyName = "My CustomHandlerThread";
 private CustomHandlerThread myCustomHandlerThread;
 private Handler myHandler;
 
 Button btnUp, btnDown;
 TextView textResult;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btnUp = (Button)findViewById(R.id.buttonup);
  btnDown = (Button)findViewById(R.id.buttondown);
  textResult = (TextView)findViewById(R.id.result);
  
  btnUp.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    myCustomHandlerThread.querySomething(1, 100);
   }});
  
  btnDown.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    myCustomHandlerThread.querySomething(500, 10);
   }});
  
  myHandler = new Handler(this);
 }
 
 @Override
 protected void onResume() {
  super.onResume();
  
  myCustomHandlerThread = new CustomHandlerThread(MyName);
  myCustomHandlerThread.setCallback(myHandler);
  myCustomHandlerThread.start();
 }

 @Override
 protected void onPause() {
  super.onPause();
  
  myCustomHandlerThread.setCallback(null);
  myCustomHandlerThread.quit();
  myCustomHandlerThread = null;
 }

 @Override
 public boolean handleMessage(Message arg0) {
  int result = (Integer)arg0.obj;
  textResult.setText("result: " + result);
  return false;
 }

}


<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" />
    
    <Button
        android:id="@+id/buttonup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Count up from 1 to 100" />
    <Button
        android:id="@+id/buttondown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Count down from 500 to 10" />
    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Oct 5, 2013

ToggleButton and Switch

android.widget.ToggleButton is a button allows the user to change a setting between two states, ON and OFF. Android 4.0 (API level 14) introduces another kind of toggle button, android.widget.Switch, that provides a slider control. The ToggleButton and Switch controls are subclasses of CompoundButton and function in the same manner, so you can implement their behavior the same way. ~ reference: Toggle Buttons guide.

Example:
ToggleButton and Switch
ToggleButton and Switch


<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" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToogleSwitch" />
    <ToggleButton
        android:id="@+id/mytogglebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="ToggleButton On"
        android:textOff="ToggleButton Off" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch" />
    <!-- Switch require API Level 14 -->
    <Switch
        android:id="@+id/myswitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Switch On"
        android:textOff="Switch Off" />
    
    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


package com.example.androidswitch;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.app.Activity;

public class MainActivity extends Activity {
 
 ToggleButton myToggleButton;
 Switch mySwitch;
 TextView info;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  info = (TextView)findViewById(R.id.info);
  myToggleButton = (ToggleButton)findViewById(R.id.mytogglebutton);
  myToggleButton.setOnCheckedChangeListener(myOnCheckedChangeListener);
  
  
  mySwitch = (Switch)findViewById(R.id.myswitch);
  mySwitch.setOnCheckedChangeListener(myOnCheckedChangeListener);
 }
 
 CompoundButton.OnCheckedChangeListener myOnCheckedChangeListener =
   new CompoundButton.OnCheckedChangeListener(){

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     info.setText(
       "Button: " + buttonView.toString() + "\n" +
       "icChecked: " + String.valueOf(isChecked));
    }
  
 };
}


Oct 2, 2013

Change color of View by applying ColorFilter

The example change color of Views (Button, EditText and ImageView) by applying ColorFilter of various PorterDuffColorFilter.

Views with ColorFilter of various PorterDuffColorFilter
Change color of View by applying ColorFilter


package com.example.androidviewcolorfilter;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;

public class MainActivity extends Activity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Button button1 = (Button)findViewById(R.id.button1);
  Button button2 = (Button)findViewById(R.id.button2);
  EditText edittext3 = (EditText)findViewById(R.id.edittext3);
  ImageView imageview4 = (ImageView)findViewById(R.id.imageview4);
  ImageView imageview5 = (ImageView)findViewById(R.id.imageview5);
  ImageView imageview6 = (ImageView)findViewById(R.id.imageview6);
  
  //Solid Blue
  ColorFilter filter1 = new PorterDuffColorFilter(
    Color.BLUE, PorterDuff.Mode.MULTIPLY);
  //Transparency Blue
  ColorFilter filter2 = new PorterDuffColorFilter(
    Color.BLUE & 0x00ffffff | 0x50000000, 
    PorterDuff.Mode.MULTIPLY);
  
  ColorFilter filter3 = new PorterDuffColorFilter(
    0x2000FF00, 
    PorterDuff.Mode.MULTIPLY);
  
  ColorFilter filter4 = new PorterDuffColorFilter(
    Color.RED, 
    PorterDuff.Mode.MULTIPLY);
  ColorFilter filter5 = new PorterDuffColorFilter(
    Color.RED, 
    PorterDuff.Mode.DST_ATOP);
  ColorFilter filter6 = new PorterDuffColorFilter(
    Color.BLACK, 
    PorterDuff.Mode.XOR);
  
  button1.getBackground().setColorFilter(filter1);
  button2.getBackground().setColorFilter(filter2);
  edittext3.getBackground().setColorFilter(filter3);
  imageview4.setColorFilter(filter4);
  imageview5.setColorFilter(filter5);
  imageview6.setColorFilter(filter6);
 }

}


<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" />
    <Button
        android:id="@+id/button0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Default Button" />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />
 <EditText
        android:id="@+id/edittext3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="EditText 3" />
 <ImageView
     android:id="@+id/imageview4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
 <ImageView
     android:id="@+id/imageview5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
 <ImageView
     android:id="@+id/imageview6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
    
</LinearLayout>


Infolinks In Text Ads