postheadericon ToggleButton example in Android





MainActivity.java

package com.example.togglebuttondemo;



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


public class MainActivity extends Activity {
ToggleButton tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb = (ToggleButton) findViewById(R.id.toggleButton1);

tb.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Toast.makeText(getApplicationContext(),
tb.getText().toString().equals("Tea") ? "You have Selected Tea" : "You have Selected Coffee",
Toast.LENGTH_LONG).show();
}
});
}

}



activity_main.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:text="www.BipinRupadiiya.com" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="ToggleButton"
        android:textOff="Coffee"
        android:textOn="Tea" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/toggleButton1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="52dp"
        android:layout_marginLeft="24dp"
        android:text="What do you like Tea / Coffee...?"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>



0 comments:

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.