1
    package alonso.radiobuttonquiz;

    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.TextView;

    public class HomePage extends AppCompatActivity {
      TextView tV1,tV2;
    SharedPreferences sp;
    ImageButton btn;
    Button button;
    //alert dialog box
    final Context c = this;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        tV1 = (TextView) findViewById(R.id.totalcoin);
        tV2 = (TextView)findViewById(R.id.nameDisplay);
        btn = (ImageButton) findViewById(R.id.startQuiz);


        SharedPreferences preferences = 
    PreferenceManager.getDefaultSharedPreferences(this);
        Integer coin = preferences.getInt("coin", 0);
        tV1.setText("COIN: " + coin);


        final SharedPreferences prefs = 
    PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = prefs.edit();
        final String username = prefs.getString("USERNAME", "");
        prefs.getString("USERNAME","");
        tV2.setText(username);



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomePage.this, LvlMenu.class);
                startActivity(intent);
                finish();


            }
        });


        LayoutInflater layoutInflaterAndroid = LayoutInflater.from(c);
        View mView = 
     layoutInflaterAndroid.inflate(R.layout.activity_login2,null);
        final AlertDialog.Builder alertDialogBuilderUserInput = new 
    AlertDialog.Builder(c,R.style.AlertDialogTheme);

        alertDialogBuilderUserInput.setView(mView);

        final EditText userInputDialogEditText = (EditText) 
    mView.findViewById(R.id.userInputDialog);
        alertDialogBuilderUserInput
                .setCancelable(false)
                .setPositiveButton("LOGIN", new 
    DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogBox, int id) {
                        String username = 
    userInputDialogEditText.getText().toString();
                        editor.putString("USERNAME",username);
                        editor.apply();
                        tV2.setText(username);
                }});

        AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
        alertDialogAndroid.show();
     }
}

Where to add setError when editText in alertDialog is empty? Please edit my code if possible to add setError when user never type in any letters in editText.. Thanks! This is the login screen in a quiz app... Really hope you all can lend a helping hand as I had worked on this for several hours already...

Akram Hussain
  • 472
  • 8
  • 23
Alonso
  • 11
  • 5
  • Possible duplicate of [Validation on EditText in alertDialog](https://stackoverflow.com/questions/40261250/validation-on-edittext-in-alertdialog) – tomerpacific Dec 10 '18 at 12:05

1 Answers1

4
 LayoutInflater layoutInflaterAndroid = LayoutInflater.from(this);
    View mView =
            layoutInflaterAndroid.inflate(R.layout.activity_login2,null);
    final AlertDialog.Builder alertDialogBuilderUserInput = new
            AlertDialog.Builder(this,R.style.AlertDialogTheme);



    final EditText userInputDialogEditText = (EditText)
            mView.findViewById(R.id.userInputDialog);
    alertDialogBuilderUserInput.setView(mView);
    AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
    alertDialogAndroid.show();

    alertDialogAndroid.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialogInterface) {

            Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Do something
                    if(userInputDialogEditText.getText().toString().equals("")){
                        userInputDialogEditText.setError("Enter Username");
                    }else {
                        String username =
                                userInputDialogEditText.getText().toString();
                        editor.putString("USERNAME",username);
                        editor.apply();
                        tV2.setText(username);
                        dialog.dismiss();
                    }
                }
            });
        }
    });
alertDialogAndroid.show();

Hope this help you...if you need any help you can ask

piet.t
  • 11,718
  • 21
  • 43
  • 52
Mahesh Vayak
  • 1,056
  • 12
  • 25
  • Thanks for your reply... but when I launch it again, the positive button does not appear at all... Can u help me again? Thanks – Alonso Dec 10 '18 at 14:48
  • Thanks anyway but then again shared preference had failed to work... nvm I reused my codes. Really appreciate your reply – Alonso Dec 11 '18 at 08:47