1

How can I achieve to show a Dialog automatically when an activity starts. If the activity is started a Dialog must be shown, where you must type a password. This password will be checked with the password stored in sharedpreferences, and if it is correct this activity will be shown,if not, a message will be shown on the dialog that the password is wrong and he must type it again.. I looked for some tutorials but all of them used a button to start the AlertDialog, but in my case It mus be shown when a specific activity is called.

How can I achieve it?

Janusz
  • 187,060
  • 113
  • 301
  • 369
androidBeginner
  • 197
  • 1
  • 2
  • 10

3 Answers3

2

Add this in your manifest, in the activity you want to look like dialog, declaration:

<activity android:theme="@android:style/Theme.Dialog">

for more information and themes: http://developer.android.com/guide/topics/ui/themes.html

furthermore, to this proggramatically you can use the following code:

public class ShowDialogActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    //
    //Log.d("DEBUG", "showing dialog!");

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.select_dialog_singlechoice);
    dialog.setTitle("Your Widget Name");
    dialog.setCancelable(true);
    dialog.setCanceledOnTouchOutside(true);
    TextView text = (TextView) dialog.findViewById(R.id.text1);
    text.setText("Message");

    dialog.show();
    //
   dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

    public void onCancel(DialogInterface arg0) {
        finish();
    }

   });
}

}

You can choose whatever layout you wish for the dialog and design it as you want.

In addition you would need to set this activity declaration in the manifest for the following:

<activity android:name=".ShowDialogActivity"
          android:theme="@android:style/Theme.Translucent.NoTitleBar">
</activity>

Hope this is what you was looking for.

nitesh goel
  • 6,338
  • 2
  • 29
  • 38
  • +1 You could probably have implemented this with just an `AlertDialog`, but your solution still gets the job done – John Dec 18 '13 at 17:22
  • thanks, so if I want to show this activity or activity-dialog when my mainactivity is called, I will just use in onCreate-Method of my MainActivity an Intent to start this Activity right? Because my main aim of the application is if the mainactivity is called a dialog must be shown with an EditText-Field so the user can give a password...So I want to show my Main-Activity only if the passwort is correct – androidBeginner Dec 18 '13 at 17:27
  • first start this activity ... and if user successfully login then start your main activity. There is no reason to start your main activity first i guess. – nitesh goel Dec 18 '13 at 17:30
1

in your oncreate method add this alert dialog code,and validate the input from edittext

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutname);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Title");
    alert.setMessage("Message");

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText();
      // Do something with value!
      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
      }
    });

    alert.show();
}
Hamad
  • 5,096
  • 13
  • 37
  • 65
  • Thx this is also another nice solution of my problem. How can I manage to show the same Dialog again if for example the cancel button is clicked (in my case if the password is wrong - but that doesn't matter, only want to know how can I show the same dialog again after doing sth.) Only want to show the activity after password is correct – androidBeginner Dec 18 '13 at 17:37
  • extract this alert dialog code in method and run it as many times as you want! @androidBeginner – Hamad Dec 18 '13 at 19:28
0

Another solution is: do not show dialog,create an activity which looks like dialog,create activity and give it a dialog look:

<activity
    android:label="@string/app_name"
    android:name=".DialogActivityDemoActivity"
    android:theme="@android:style/Theme.Dialog" >
    </activity>

now make this activity a launcher activity,validate user's input then start your main activity.

Hamad
  • 5,096
  • 13
  • 37
  • 65