ToggleButtonActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_toggle_button"
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="com.bhaumik.programmingstudy.ToggleButtonActivity">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate On"
android:textOff="Vibrate Off"
android:textAllCaps="false"
android:layout_gravity="center"
android:id="@+id/btn_toggle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="25sp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textColor="#000"
android:id="@+id/tv_show"/>
</LinearLayout>
ToggleButtonActivity.java
package com.bhaumik.programmingstudy;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
public class ToggleButtonActivity extends AppCompatActivity {
TextView showText;
ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toggle_button);
showText = (TextView) findViewById(R.id.tv_show);
toggleButton = (ToggleButton) findViewById(R.id.btn_toggle);
showText.setVisibility(View.INVISIBLE);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = ((ToggleButton) v).isChecked();
if(checked)
{
showText.setVisibility(View.VISIBLE);
showText.setText("Vibrate On");
}
else
{
showText.setVisibility(View.VISIBLE);
showText.setText("Vibrate Off");
}
}
});
}
}
0 comments:
Post a Comment