Monday, 21 January 2013

Android Simple ToggleButton Saving the ON/OFF Example


I use two Activity ,in first used  button in second activity ToggleButton.

........................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" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginRight="75dp"
        android:layout_marginTop="120dp"
        android:text="Button" />

</RelativeLayout>

...........................Second.xml............


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

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" />

</LinearLayout>


..............................MainActivity.java.....................


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), second.class));
}
});
}
}


.............................second.java........................................






import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class second extends Activity {

ToggleButton button;
SharedPreferences app_preferences;
SharedPreferences.Editor editor;
private static final String TOGGLE = "abc";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
button = (ToggleButton) findViewById(R.id.toggleButton1);
app_preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editor = app_preferences.edit();

if (app_preferences.getBoolean(TOGGLE, true)) {
button.setChecked(true);
} else {
button.setChecked(false);
}
// TODO Auto-generated method stub

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (button.isChecked()) {
button.setChecked(true);
editor.putBoolean(TOGGLE, true);
} else {
button.setChecked(false);
editor.putBoolean(TOGGLE, false);
}
editor.commit();
}
});

}

}