iphone uisegmentedcontrol  like on android

Tutorial_UI_Cumtom_SwitchButtonActivity.java

package com.powenko.Tutorial_UI_Cumtom_SwitchButton;

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

public class Tutorial_UI_Cumtom_SwitchButtonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button t_Buttonswitch1 = (Button)findViewById(R.id.buttonswitch01);
        Button t_Buttonswitch2 = (Button)findViewById(R.id.buttonswitch02);
        Button t_Buttonswitch3 = (Button)findViewById(R.id.buttonswitch03);

        t_Buttonswitch1.setOnClickListener(mOnClickListener);
        t_Buttonswitch2.setOnClickListener(mOnClickListener);
        t_Buttonswitch3.setOnClickListener(mOnClickListener);

        t_Buttonswitch1.setOnTouchListener(mOnTouchListener);
        t_Buttonswitch2.setOnTouchListener(mOnTouchListener);
        t_Buttonswitch3.setOnTouchListener(mOnTouchListener);

        t_Buttonswitch1.setBackgroundResource(Fun_Btn_image(t_Buttonswitch1,0));
        t_Buttonswitch2.setBackgroundResource(Fun_Btn_image(t_Buttonswitch2,0));
        t_Buttonswitch3.setBackgroundResource(Fun_Btn_image(t_Buttonswitch3,0));

    }

    private OnTouchListener mOnTouchListener = new OnTouchListener() {
  	  @Override
  		 public boolean onTouch(View v, MotionEvent event) {
  			 switch ( event.getAction() ) {
  			 case MotionEvent.ACTION_DOWN:
  				 v.setBackgroundResource(Fun_Btn_image(v,1));
  			 break;
  			 case MotionEvent.ACTION_UP:
  				 v.setBackgroundResource(Fun_Btn_image(v,0));
  			 break;
  			 }

  		 	return false;

  		 }
    };

    public int Fun_Btn_image(View v,int i_kind)
    {
  	  int t_icon=R.drawable.icon;
  	  String t_str=(String)v.getTag();

  	  if(t_str.endsWith("switchleft")  && i_kind==0){
  		  t_icon=R.drawable.btm_switch_left;
  	  }else if(t_str.endsWith("switchleft")  && i_kind==1){
  		  t_icon=R.drawable.btm_switch_left_press;
  	  }else if(t_str.endsWith("switchmid")  && i_kind==0){
  		  t_icon=R.drawable.btm_switch_mid;
  	  }else if(t_str.endsWith("switchmid")  && i_kind==1){
  		  t_icon=R.drawable.btm_switch_mid_press;
  	  }else if(t_str.endsWith("switchright")  && i_kind==0){
  		  t_icon=R.drawable.btm_switch_right;
  	  }else if(t_str.endsWith("switchright")  && i_kind==1){
  		  t_icon=R.drawable.btm_switch_right_press;
  	  }

  	//  t_icon=R.drawable.btn_h01_a;
  	  return t_icon;
    }
  //Create an anonymous implementation of OnClickListener
    private OnClickListener mOnClickListener = new OnClickListener() {
  	  @Override
  	  public void onClick(View v) {
  		  String t_str=(String)v.getTag();
  		  if(t_str.endsWith("switchleft")){
  		  }else  if(t_str.endsWith("switchmid")){

  		  }else  if(t_str.endsWith("switchright")){
  		  }

  	}
    };

}

res\layout\main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<RelativeLayout android:layout_height="wrap_content" android:id="@+id/relativeLayout1" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true">

<Button android:text="Button"
android:layout_width="wrap_content"
android:id="@+id/buttonswitch01"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:background="@drawable/btm_switch_left"
 android:tag="switchleft"
android:textColor="#FFFFFF"   />

<Button android:text="Button"
android:layout_width="wrap_content"
android:id="@+id/buttonswitch02"
android:layout_height="wrap_content"
android:background="@drawable/btm_switch_mid"

android:layout_toRightOf="@+id/buttonswitch01"
 android:tag="switchmid"
android:textColor="#FFFFFF"   />

<Button android:text="Button"
android:layout_width="wrap_content"

android:layout_toRightOf="@+id/buttonswitch02"
android:id="@+id/buttonswitch03"
android:layout_height="wrap_content"
android:background="@drawable/btm_switch_right"
 android:tag="switchright"
android:textColor="#FFFFFF"   />
</RelativeLayout>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:layout_below="@+id/relativeLayout1"
    android:id="@+id/textView1"/>

</RelativeLayout>

res\drawable\btm_switch_mid_press

res\drawable\btm_switch_right_press

res\drawable\btm_switch_left_press


res\drawable\btm_switch_mid

res\drawable\btm_switch_mid

res\drawable\btm_switch_mid

sample code:

“Tutorial_UI_Cumtom_SwitchButton”

By admin-powenko

Dr. Powen Ko is a teacher and CEO on LoopTek LLC, and like to teaching. if you need to class, please let PowenKo know, he will love to service and sharing. LoopTek web site is www.looptek.com

Leave a Reply