I want to prompt the user his name in an AlertDialogue, save his name on Shared Prefferences and set his name in a text view so when he returns he can see his name. So far I have the dialogue input, but i'm not sure if it is saving because the text view shows something like "Android Widget EditText(9o179...etc)" What am I doing wrong? Code:
String name; //global
TextView user;
Oncreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonCreate();
preferences();
togglePlay();
}
TogglePlay Method:
public void togglePlay() {
init.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
KeysOn();
} else {
KeysOff();
}
}
});
}
public void KeysOn() {
Toast.makeText(getApplicationContext(), getString(R.string.ToastToggleOn), Toast.LENGTH_SHORT).show();
user.setText(name);
//etc
}
The preferences method called in the onCreate:
public void preferences(){
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
name= settings.getString("name",name);
saveName();
data = settings.getString("stage", "Indoors");
String backGround = settings.getString("stage", "Indoors");
if (backGround.equals("Indoors")) {
Picasso.with(this).load(R.drawable.shocked_crowd).fit().centerCrop().into(palco);
Toast.makeText(getApplicationContext(), getString(R.string.stageIndoors), Toast.LENGTH_LONG).show();
}
if (backGround.equals("Street")) {
Picasso.with(this).load(R.drawable.coins).fit().centerCrop().into(palco);
Toast.makeText(getApplicationContext(), getString(R.string.stageStreet), Toast.LENGTH_LONG).show();
}
}
EDIT: missing code:
public void popUp(){
AlertDialog.Builder questionName = new AlertDialog.Builder(this);
questionName.setTitle(" Enter your name ");
EditText input=new EditText(this);
name=input.toString();
LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
questionName.setView(input);
questionName.setNegativeButton(" cancel ", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
}
});
questionName.setPositiveButton(" done ", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
}
});
AlertDialog dialog = questionName.create();
dialog.show();
}
public void saveName(){
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", name);
editor.commit();
}