Created
November 11, 2014 21:09
-
-
Save RammusXu/c53465798e47658ada4a to your computer and use it in GitHub Desktop.
showEnterNameDialog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myfunction; | |
import android.app.AlertDialog; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.LinearLayout; | |
public class AlertDialogManager { | |
public void showEnterNameDialog(final Context context) { | |
final AlertDialog alertDialog = new AlertDialog.Builder(context) | |
.create(); | |
alertDialog.setCancelable(false); | |
alertDialog.setTitle("Enter Your Name"); | |
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( | |
LinearLayout.LayoutParams.MATCH_PARENT, | |
LinearLayout.LayoutParams.MATCH_PARENT); | |
final EditText input = new EditText(context); | |
input.setLayoutParams(lp); | |
input.setHint("Type here"); | |
input.setText(context.getSharedPreferences(MainActivity.PREF, | |
Context.MODE_PRIVATE).getString(MainActivity.PREF_NAME, "")); | |
final Button btn = new Button(context); | |
btn.setLayoutParams(lp); | |
btn.setText("Confirm"); | |
btn.setOnClickListener(new Button.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (!input.getText().toString().trim().isEmpty()) { | |
SharedPreferences sp = context.getSharedPreferences( | |
MainActivity.PREF, Context.MODE_PRIVATE); | |
SharedPreferences.Editor se = sp.edit(); | |
se.putString(MainActivity.PREF_NAME, input.getText() | |
.toString().trim()); | |
se.commit(); | |
alertDialog.dismiss(); | |
} | |
} | |
}); | |
final LinearLayout ll = new LinearLayout(context); | |
ll.setLayoutParams(lp); | |
ll.setOrientation(LinearLayout.VERTICAL); | |
ll.addView(input); | |
ll.addView(btn); | |
alertDialog.setView(ll); // uncomment this line | |
// Showing Alert Message | |
alertDialog.show(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myfunction; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
public class MainActivity extends Activity { | |
protected static final String PREF = "PREF"; | |
protected static final String PREF_NAME = "PREF_NAME"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
adm.showEnterNameDialog(MainActivity.this); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
AlertDialogManager adm = new AlertDialogManager(); | |
@Override | |
public void onResume() { | |
if (getSharedPreferences(PREF, Context.MODE_PRIVATE).getString( | |
PREF_NAME, "").equals("")) { | |
adm.showEnterNameDialog(MainActivity.this); | |
} | |
super.onResume(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment