EditText on touch - android

I have a DateTimePicker with a little "amount" editext that I save the Sharedpref
but for some reason if I "touch" the EditText and enter the amount in manually, the text is black and doesn't transfer to the TextView, if using the imagebuttons increase and decrease the editText will work as intended. If I enter the dates and times in this manner though it does work, what am I missing?
ImageButton increase = (ImageButton)mDateTimeDialogView.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener(){
public void onClick(View v){
amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
editor.putString("numbers", amountText);
editor.commit();
}
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){
public void onClick(View v){
amountt--;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
editor.putString("numbers", amountText);
editor.commit();
}
});
The relevant EditText info
amountedit=(EditText)mDateTimeDialogView.findViewById(R.id.amountedit);
The TextView it displays to
amnt = (TextView)findViewById(R.id.amountText);
The XML of EditText
<EditText
android:id="#+id/amountedit"
android:layout_width="50dp"
android:key = "amntkey"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="62dp"
android:ems="10"
android:inputType="number"
android:persistent="true" />
The complete java activity it takes place:
import java.util.Calendar;
import java.util.HashMap;
import org.iimed.www.DateTimePicker.DateWatcher;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.inputmethodservice.Keyboard.Key;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class CaAdd extends Activity implements DateWatcher,OnClickListener{
Button button_click;
int amountt = 0;
String amountText;
String number = "numbers";
TextView amnt;
TextView dateResult, date2;
int hrs,min;
SharedPreferences pref;
String time = "time";
String get;
String sTime;
String amount ;
EditText amountedit;
String result_string;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetimeactivity);
dateResult = (TextView)findViewById(R.id.textview1);
date2 = (TextView)findViewById(R.id.textView2);
button_click = (Button)findViewById(R.id.setbtn);
pref = getSharedPreferences("prefs", Context.MODE_PRIVATE);
button_click.setOnClickListener(this);
amnt = (TextView)findViewById(R.id.amountText);
pref = getSharedPreferences("prefs", Context.MODE_PRIVATE);
amountText = Integer.toString(amountt);
amount = amnt.toString();
get = pref.getString("numbers",amountText);
sTime = pref.getString("time", result_string);
amnt.setText(get);
date2.setText(sTime) ;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.setbtn:
final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);
mDateTimePicker.initData();
amountedit=(EditText)mDateTimeDialogView.findViewById(R.id.amountedit);
ImageButton increase = (ImageButton)mDateTimeDialogView.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Editor editor = pref.edit();
amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
String amount =amnt.toString();
editor.putString("numbers", amountText);
editor.commit();
}
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Editor editor = pref.edit();
amountt--;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
String edi = amountedit.toString();
editor.putString("numbers", amountText);
editor.commit();
}
});
Button setDateBtn = (Button)mDateTimeDialogView.findViewById(R.id.SetDateTime);
setDateBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(CaAdd.this,AlarmReceiver.class);
PendingIntent pi=PendingIntent.getActivity(CaAdd.this, 2, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alm=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
Editor editor = pref.edit();
hrs=mDateTimePicker.getHour();
min=mDateTimePicker.getMinute();
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hrs);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi);
mDateTimePicker.clearFocus();
result_string =
String.valueOf(mDateTimePicker.getHour()) + ":" + String.valueOf(mDateTimePicker.getMinute());
if(mDateTimePicker.getHour() > 12) result_string = result_string + "PM";
else result_string = result_string + "AM";
date2.setText(result_string);
editor.putString("time",result_string);
editor.commit();
mDateTimeDialog.dismiss();
}
});
Button cancelBtn = (Button)mDateTimeDialogView.findViewById(R.id.CancelDialog);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
mDateTimeDialog.cancel();
}
});
Button resetBtn = (Button)mDateTimeDialogView.findViewById(R.id.ResetDateTime);
resetBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
}
});
mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDateTimeDialog.setContentView(mDateTimeDialogView);
mDateTimeDialog.show();}}
public void onDateChanged(Calendar c) {
Log.e("","" + c.get(Calendar.MONTH) + " " + c.get(Calendar.DAY_OF_MONTH)+ " " + c.get(Calendar.YEAR));
String result_string =String.valueOf(c.get(Calendar.MONTH)+1) + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(c.get(Calendar.YEAR));
dateResult.setText(null);
}}

Your code isn't clear, you should post code about that EditText you're talking about.
BTW, to get what you would like, you can try to do Sharedpref's edit & commit
when your EditText's text changed, although it's a bad design...
You need a Text change listener
EditText.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{}
#Override
public void afterTextChanged(Editable s)
{}
});

Related

Sharedprefrences not saving?

When i try to save a variable to shared preferences then call it in an editext it is not being saved? I've been looking around for hours but i cannot find anything on it. I know that sharedPrefrences acts like a dictionary in a way but other than that i don't understand why this is not working :(
package com.example.gatorblocks;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class block1Settings extends WearableActivity{
private TextView mTextView;
Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_block1_settings);
configureBackButton();
configureColor();
Button addTextButton = (Button) findViewById(R.id.Apply);
TextView simpleEditText = findViewById(R.id.simpleEditText);
SharedPreferences prefs = getSharedPreferences("classes.txt", 0);
simpleEditText.setText(prefs.getString("classes1","1-1")); //set textbox to equal current class
final EditText vEditText = (EditText) findViewById(R.id.simpleEditText);
mTextView = (TextView) findViewById(R.id.text);
// Enables Always-on
setAmbientEnabled();
addTextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String enteredText = vEditText.getText().toString(); //sets the array value of block to the editText
test(enteredText);
}
});
}
private void configureColor() {
Button Block1 = (Button) findViewById(R.id.colorButton);
Block1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(block1Settings.this, colorBlock1.class));
overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
}
});
}
private void configureBackButton(){
Button backbutton = (Button) findViewById(R.id.backButton);
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(block1Settings.this, classes.class));
overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
finish();
}
});
}
public void test(String enteredText){
SharedPreferences pref = getSharedPreferences("classes.txt", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("class1", enteredText);
editor.apply();
}
}
How did you get data from SharedPreferences which is not saved? You saved data using key class1 and want to get it by classes1 which is not correct way. You have to use same key. Try using
SharedPreferences prefs = getSharedPreferences("classes.txt", 0);
simpleEditText.setText(prefs.getString("class1","1-1"));

Clicker App: Saves Clicks not Correctly

I made a Clicker App (like cookie clicker). But when i Restart the App, the app saves the clicks, but only visible, so i mean when i click on the button, the clicks will be set to 0 again. So my question: Is my SharedPreference correct, or did i have done an mistake?
My complete MainActivity.java:
package com.rage.clicker;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import static com.rage.clicker.R.id.highscore_text;
import static com.rage.clicker.R.id.textView88;
public class MainActivity extends AppCompatActivity {
SoundPool mySound;
int playClick;
private final String TAG = this.getClass().getName();
ImageView hi;
TextView tv_clicks;
TextView ht;
ImageView b_click;
Button save;
int clicks;
SharedPreferences sf;
public static final String preference = "pref";
public static final String saveIt = "saveKey";
private TextView highScoreView;
private TextView currentScoreView;
private int currentScore;
private int highScore;
private String highScoreString;
private String currentScoreString;
private SharedPreferences msharedPreferences;
private static final String FILENAME = "PreferencesFilename";
private static final String VAL_KEY = "ValueKey";
private EditText editText;
private TextView TEV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Speichern laden
TEV = (TextView) findViewById(R.id.textView88);
SharedPreferences sharedPrefs = getSharedPreferences(FILENAME, 0);
TEV.setText(sharedPrefs.getString(VAL_KEY, ""));
//Speichern laden
msharedPreferences = getPreferences(MODE_PRIVATE);
mySound = new SoundPool(100, AudioManager.STREAM_MUSIC, 0);
playClick = mySound.load(this, R.raw.click, 1);
tv_clicks = (TextView) findViewById(textView88);
b_click = (ImageView) findViewById(R.id.imageView);
hi = (ImageView) findViewById(R.id.imageView);
b_click = (ImageView) findViewById(R.id.imageView);
ht = (TextView) findViewById(highscore_text);
final TextView TV = (TextView) findViewById(R.id.highscore_text);
currentScoreString = getString(R.string.current_score);
highScoreString = getString(R.string.high_score);
currentScore = clicks;
highScore = msharedPreferences.getInt(highScoreString, 0);
save = (Button) findViewById(R.id.button);
save.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast dumplings= Toast.makeText(MainActivity.this, "Adolf4", Toast.LENGTH_LONG);
dumplings.show();
}
});
b_click.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final ImageView zoom = (ImageView) findViewById(R.id.imageView);
final Animation zoomAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.size);
zoom.startAnimation(zoomAnimation);
mySound.play(playClick, 1, 1, 1, 0, 1);
clicks++;
tv_clicks.setText("Klicks: " + clicks);
ht.setText(currentScoreString + ": Test" + currentScore);
}
});
}
protected void init() {
currentScoreView.setText(currentScoreString + ": asd" + currentScore);
highScoreView.setText(highScoreString + ":34 " + highScore);
}
#Override
protected void onStop() {
super.onStop();
//Speichern
SharedPreferences sharedPrefs = getSharedPreferences(FILENAME, 0);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(VAL_KEY, TEV.getText().toString());
editor.commit();
//Speichern
}
}
When you save data, you take click's count value from TEV TextView, but inside onClick event you update tv_clicks and TEV always remains zero by default. You should save clicks value like this
editor.putInt(VAL_KEY, clicks);
Then you should load clicks value and set it in the TextView:
clicks = sharedPrefs.getInt(VAL_KEY, 0)
TEV.setText(String.valueOf(clicks));

TextView to display sharedpreference EditText

I'm trying to Save an editText to Sharedprefrences so that it displays in the TextView amnt. It's displaying it but not saving it? what am I doing wrong? I am new to this, just a little weekend hobby so I'm sorry if this is a stupid question, thanks for your time.
package org.iimed.www;
import java.util.Calendar;
import org.iimed.www.DateTimePicker.DateWatcher;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.inputmethodservice.Keyboard.Key;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class CaAdd extends Activity implements DateWatcher,OnClickListener{
Button button_click,amount;
int amountt = 0;
Key at;
EditText amountedit;
String amountText;
String amountString;
TextView dateResult, date2,amnt;
int hrs,min;
SharedPreferences.Editor editor ;
SharedPreferences prefs;
String amntprefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetimeactivity);
dateResult = (TextView)findViewById(R.id.textview1);
date2 = (TextView)findViewById(R.id.textView2);
button_click = (Button)findViewById(R.id.setbtn);
button_click.setOnClickListener(this);
amnt = (TextView)findViewById(R.id.amountText);
}
public final String AT = "Amount";
public void loadDefaultSharedPreferences() {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs != null) {
String loadUsername= prefs.getString(
AT , null);
if (loadUsername != null && !loadUsername.isEmpty()) {
amnt.setText(loadUsername);
}}}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.setbtn:
final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);
mDateTimePicker.initData();
amountedit = (EditText)mDateTimeDialogView.findViewById(R.id.amountedit);
ImageButton increase = (ImageButton)mDateTimeDialogView.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener(){
public void onClick(View v){
amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
}
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){
public void onClick(View v){
amountt--;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
}
});
Button setDateBtn = (Button)mDateTimeDialogView.findViewById(R.id.SetDateTime);
// Update demo TextViews when the "OK" button is clicked
setDateBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(CaAdd.this,AlarmReceiver.class);
PendingIntent pi=PendingIntent.getActivity(CaAdd.this, 2, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alm=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
hrs=mDateTimePicker.getHour();
min=mDateTimePicker.getMinute();
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hrs);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi);
mDateTimePicker.clearFocus();
String result_string = mDateTimePicker.getMonth() + "/" + String.valueOf(mDateTimePicker.getDay()) + "/" + String.valueOf(mDateTimePicker.getYear())
+ " " + String.valueOf(mDateTimePicker.getHour()) + ":" + String.valueOf(mDateTimePicker.getMinute());
if(mDateTimePicker.getHour() > 12) result_string = result_string + "PM";
else result_string = result_string + "AM";
date2.setText(result_string);
mDateTimeDialog.dismiss();
}
});
Button cancelBtn = (Button)mDateTimeDialogView.findViewById(R.id.CancelDialog);
// Cancel the dialog when the "Cancel" button is clicked
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
mDateTimeDialog.cancel();
}
});
// Reset Date and Time pickers when the "Reset" button is clicked
Button resetBtn = (Button)mDateTimeDialogView.findViewById(R.id.ResetDateTime);
resetBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
}
});
mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
//Display the dialog
mDateTimeDialog.show();
}}
public void onDateChanged(Calendar c) {
Log.e("","" + c.get(Calendar.MONTH) + " " + c.get(Calendar.DAY_OF_MONTH)+ " " + c.get(Calendar.YEAR));
String result_string =String.valueOf(c.get(Calendar.MONTH)+1) + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(c.get(Calendar.YEAR));
// _tvUserDOB.setTypeface(CGlobalVariables.tfFoEnglish);
dateResult.setText(result_string+" (mm/dd/yyyy)");
}
public void saveDefaultPreferences() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);{
SharedPreferences.Editor editor = prefs.edit();
final String AT = "Amount";
editor.putString(AT,amountText);
editor.commit();}
}}
Shared preferences allows you to save and retrieve key, value pair data.
Initialization:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
// 0 - for private mode
Editor editor = pref.edit();
Storing Data:
editor.putString("key_name", "string value"); // Storing string
editor.commit(); // commit changes
Retrieving Data:
pref.getString("key_name", null); // getting String
To know more about SharedPreferences or to try out a simple example, check this
You can only save key-value pairs for some of the primitives and String (String set also) only. Android views can never be saved anywhere.
SharedPreferences manager =PreferenceManager.getDefaultSharedPreferences(context);
Use it for saving and retrieving data as follows
// for saving data
manager.edit().putString(context.getString(AT), YOUR_TEXT).commit()
// for retrieving data
manager.getString(context.getString(AT), "");

Trouble inflating second layout

I'm trying to set a second inflated layout in my activity, the one that works inflates to set an alarm from a timepicker, the second one inflates a number picker to set an amount. So far the second button will not respond or, based on another set of code (one shown) I get a runtime error. Can anyone tell me what I am doing wrong?
package org.iimed.www;
import java.util.Calendar;
import org.iimed.www.DateTimePicker.DateWatcher;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class CaAdd extends Activity implements OnClickListener {
Button button_click, amount;
TextView dateResult, date2;
int hrs, min;
int amountt = 0;
EditText amountedit;
String amountText;
Dialog amountdialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetimeactivity);
dateResult = (TextView) findViewById(R.id.textview1);
date2 = (TextView) findViewById(R.id.textView2);
button_click = (Button) findViewById(R.id.setbtn);
amount = (Button) findViewById(R.id.amountbtn);
amount.setOnClickListener(this);
button_click.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.setbtn:
amountdialog = new Dialog(this);
final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater()
.inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView
.findViewById(R.id.DateTimePicker);
mDateTimePicker.initData();
Button setDateBtn = (Button) mDateTimeDialogView
.findViewById(R.id.SetDateTime);
// Update demo TextViews when the "OK" button is clicked
setDateBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(CaAdd.this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getActivity(CaAdd.this, 2,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
hrs = mDateTimePicker.getHour();
min = mDateTimePicker.getMinute();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hrs);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alm.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pi);
mDateTimePicker.clearFocus();
String result_string = mDateTimePicker.getMonth() + "/"
+ String.valueOf(mDateTimePicker.getDay()) + "/"
+ String.valueOf(mDateTimePicker.getYear()) + " "
+ String.valueOf(mDateTimePicker.getHour()) + ":"
+ String.valueOf(mDateTimePicker.getMinute());
if (mDateTimePicker.getHour() > 12)
result_string = result_string + "PM";
else
result_string = result_string + "AM";
date2.setText(result_string);
mDateTimeDialog.dismiss();
}
});
Button cancelBtn = (Button) mDateTimeDialogView
.findViewById(R.id.CancelDialog);
// Cancel the dialog when the "Cancel" button is clicked
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
mDateTimeDialog.cancel();
}
});
// Reset Date and Time pickers when the "Reset" button is clicked
Button resetBtn = (Button) mDateTimeDialogView
.findViewById(R.id.ResetDateTime);
resetBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
}
});
Button amountbtn = (Button) findViewById(R.id.amountbtn);
amountbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Inflate the root layout
final RelativeLayout amountView = (RelativeLayout) getLayoutInflater()
.inflate(R.layout.amount, null);
// Grab widget instance
amountdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
amountdialog.setContentView(amountView);
amountdialog.show();
amountedit = (EditText) amountView
.findViewById(R.id.amount);
ImageButton increase = (ImageButton) amountView
.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
}
});
ImageButton decrease = (ImageButton) amountView
.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
amountt--;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
}
});
amountdialog.dismiss();
// No title on the dialog window
mDateTimeDialog
.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
// Display the dialog
mDateTimeDialog.show();
}
});
}
}
}

Value stored in shared preferences is not being reflected

package com.example.shared;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button bshared;
TextView tv;
String x;
SharedPreferences pref;
Editor edit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bshared = (Button) findViewById(R.id.bshared);
tv = (TextView) findViewById(R.id.tv);
tv.setText(x);
bshared.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
pref=getApplicationContext().getSharedPreferences("details",1);
edit = pref.edit();
edit.clear();
edit.putString("name", "These are new values");
edit.commit();
x = pref.getString("name", "");
}
});
}
}
When i click on the button the value of textview must change to 'These are new values', but that is not happening. Can someone help me find my mistake ?
In on Click Method you have to set value to textview like below code.
pref=getApplicationContext().getSharedPreferences("details",1);
x = pref.getString("name", "");
tv.setText(x);
bshared.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
edit = pref.edit();
edit.clear();
edit.putString("name", "These are new values");
edit.commit();
x = pref.getString("name", "");
tv.setText(x);
}
});
The textview is setting the text before you change your text. So it will not be reflected in the textview. add this line to last of onclick method after you change value of the string.
tv.setText(x);
EDIT:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bshared = (Button) findViewById(R.id.bshared);
tv = (TextView) findViewById(R.id.tv);
pref=getApplicationContext().getSharedPreferences("details",1);
x = pref.getString("name", "");
tv.setText(x);
bshared.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
edit = pref.edit();
edit.clear();
edit.putString("name", "These are new values");
edit.commit();
x = pref.getString("name", "");
tv.setText(x);
}
});
}
}

Categories

Resources