Activity main.XML :-
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.kiranapp.MainActivity"> <ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton1"
android:id="@+id/toggleButton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="60dp"
android:layout_marginTop="18dp"
android:textOff="Off"
android:textOn="On"
android:checked="false" /> <ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton2"
android:layout_alignBaseline="@+id/toggleButton"
android:layout_alignBottom="@+id/toggleButton"
android:id="@+id/toggleButton2"
android:layout_marginLeft="44dp"
android:layout_toRightOf="@+id/toggleButton"
android:textOff="Off"
android:textOn="On"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_marginTop="85dp"
android:layout_below="@+id/toggleButton"
android:layout_centerHorizontal="true" /> </RelativeLayout>
MainActivity.java :-
package com.kiranapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends Activity { private ToggleButton toggleButton, toggleButton2; private Button Submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListenerOnButtonClick(); } public void addListenerOnButtonClick(){ toggleButton=(ToggleButton)findViewById(R.id.toggleButton); toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2); Submit=(Button)findViewById(R.id.button); Submit.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View view) { StringBuilder result = new StringBuilder();
result.append("ToggleButton1 : ").append(toggleButton.getText()); result.append("\nToggleButton2 : ").append(toggleButton2.getText()); Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show(); } }); } @Override
public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true;
} }
Output :-
ToggleButton figure |
Thanks for sharing useful information.
ReplyDelete