ToggleButton allows to select either states of the action. See below code.
activity_practice.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:orientation="vertical"
android:background="#d0d0d0"
tools:context=".PracticeActivity" >
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
/>
<ToggleButton
android:id="@+id/toggle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Turn On"
android:textOff="Turn Off"
android:layout_margin="20dp"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"
android:layout_margin="20dp"
/>
</LinearLayout>
activity_practice.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:orientation="vertical"
android:background="#d0d0d0"
tools:context=".PracticeActivity" >
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
/>
<ToggleButton
android:id="@+id/toggle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Turn On"
android:textOff="Turn Off"
android:layout_margin="20dp"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"
android:layout_margin="20dp"
/>
</LinearLayout>
PracticeActivity.java
package com.agilissystems.practicework;
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 PracticeActivity extends Activity implements OnClickListener {
ToggleButton oToggleB1, oToggleB2 ;
Button oBtn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice);
oToggleB1 = (ToggleButton) findViewById(R.id.toggle1);
oToggleB2 = (ToggleButton) findViewById(R.id.toggle2);
oBtn1 = (Button) findViewById(R.id.btn1);
oBtn1.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.practice, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0.getId() == R.id.btn1){
StringBuffer oStr1 = new StringBuffer();
oStr1.append("\nToggle 1 : " + oToggleB1.getText().toString());
oStr1.append("\nToggle 2 : "+ oToggleB2.getText().toString());
Toast.makeText(this, "State : " + oStr1.toString(), Toast.LENGTH_LONG).show();
}
}
}
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Practicework</string>
<string name="action_settings">Settings</string>
<string name="btn1">Check Status</string>
</resources>
When we add toggle button in the code, it will give ON of OFF state by default which you can see in the first toggle button. But in most of the cases, we need to display different text for ON or OFF state, which we can achieve through textOn and textOff attributes of ToggleButton which are highlighted in above code. getText() method will return the current state of ToggleButton.
No comments:
Post a Comment