\how use sharedpreferences on all field?
Layout XML:
<LinearLayout android:layout_width="match_parent" android:layout_height="75dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1." /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00:00" android:layout_marginRight="10dp"/> <TextView android:id="@+id/textView11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00:00" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="www.google.co.in" android:ems="10" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2." /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:text="00:00" /> <TextView android:id="@+id/textView12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00:00" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="www.yahoo.co.in" android:ems="10" /> </LinearLayout>
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:layout_weight="0.11" android:text="WebView" /> </LinearLayout>
main activicty
package com.example.myproject5;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private TextView tv6, tv7, tv11, tv12;
private EditText et1, et2;
private CheckBox cb1, cb2;
private Button submit, webview;
private int hour;
private int minute;
public static final String PREFS_NAME = "MyPrefsFile";
private static final int TIME_DIALOG_ID = 1;
private static final int TIME_DIALOG_ID1 = 2;
private static final int TIME_DIALOG_ID2 = 3;
private static final int TIME_DIALOG_ID3 = 4;
int cur = 0;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private int savelogin;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv6 = (TextView) findViewById(R.id.textView6);
tv7 = (TextView) findViewById(R.id.textView7);
tv11 = (TextView) findViewById(R.id.textView11);
tv12 = (TextView) findViewById(R.id.textView12);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb2 = (CheckBox) findViewById(R.id.checkBox2);
submit = (Button) findViewById(R.id.button1);
webview = (Button) findViewById(R.id.button2);
tv6.setEnabled(false);
tv7.setEnabled(false);
tv11.setEnabled(false);
tv12.setEnabled(false);
et1.setEnabled(false);
et2.setEnabled(false);
loginPreferences = getSharedPreferences(PREFS_NAME, 0);
savelogin = loginPreferences.getInt("abc", 0); // save data
if (savelogin == 1) {
tv6.setText(savelogin);
} else if (savelogin == 2) {
tv7.setText(savelogin);
} else if (savelogin == 3) {
tv11.setText(0);
} else if (savelogin == 4) {
tv12.setText(0);
}
cb1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (cb1.isChecked()) {
tv6.setEnabled(true);
tv11.setEnabled(true);
et1.setEnabled(true);
} else {
tv6.setEnabled(false);
tv11.setEnabled(false);
et1.setEnabled(false);
}
}
});
cb2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (cb2.isChecked()) {
tv7.setEnabled(true);
tv12.setEnabled(true);
et2.setEnabled(true);
} else {
tv7.setEnabled(false);
tv12.setEnabled(false);
et2.setEnabled(false);
}
}
});
tv6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
loadpreferences1("abc", 1);
}
});
tv7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID1);
loadpreferences1("abc", 2);
}
});
tv11.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID2);
loadpreferences1("abc", 3);
}
});
tv12.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID3);
loadpreferences1("abc", 4);
}
});
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
webview.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, second.class);
startActivity(intent);
loadpreferences1("abc", 5);
}
});
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
cur = TIME_DIALOG_ID;
return new TimePickerDialog(this, timePickerListener, hour, minute, true);
case TIME_DIALOG_ID1:
cur = TIME_DIALOG_ID1;
return new TimePickerDialog(this, timePickerListener, hour, minute, true);
case TIME_DIALOG_ID2:
cur = TIME_DIALOG_ID2;
return new TimePickerDialog(this, timePickerListener, hour, minute, true);
case TIME_DIALOG_ID3:
cur = TIME_DIALOG_ID3;
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
if (cur == TIME_DIALOG_ID) {
tv6.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
} else if (cur == TIME_DIALOG_ID1) {
tv7.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
} else if (cur == TIME_DIALOG_ID2) {
tv11.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
} else if (cur == TIME_DIALOG_ID3) {
tv12.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
}
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
private void loadpreferences1(String str, int in) {
loginPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
loginPrefsEditor.putInt(str, in);
loginPrefsEditor.commit();
}
}
Use these method to save and get values from any where in your app change parameter according to your requirment
this method to save value in shared prefrences
this method to get value from shared prefrences