Android Alert Dialog on startup - java

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?

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.

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();
}

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.

Related

Check if context is being viewed or in focus

I have an Alert Dialog that I am displaying whenever there is a response from the server or an API. It looks like so:
public void showDeveloperDialog(Context context, String responseMessage) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Response");
alertDialog.setMessage(responseMessage);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setCancelable(false);
alertDialog.show();
}
If the dialog gets called from example Activity A and there is a delay with the response prompting the user goes to another activity (Activity B), the app crashes since the context that called it is not in view.
How can I check to ensure that the dialog only gets shown if the initial context is in view?
You could cast the context to an Activity to check if the Activity is finished or not, and only display if its not finished using isFinishing() method
Activity activity = (Activity) context;
if (!activity.isFinishing()) {
// You can now display the dialog
alertDialog.show();
}

How do I get the correct Context for an OnClickListener?

I'm trying to open a custom dialog when a user clicks on a LinearLayout using the following code:
each_pays = (TextView) findViewById(R.id.each_pays);
each_pays_vert.setOnClickListener(new LinearLayout.OnClickListener() {
#Override
public void onClick(View _v) {
// custom dialog
final Dialog multiples_dialog = new Dialog(this);
multiples_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
multiples_dialog.setContentView(R.layout.multiples_dialog);
Button closeMultiplesDialogButton = (Button) multiples_dialog.findViewById(R.id.close_multiples_button);
// if button is clicked, close the custom dialog
closeMultiplesDialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
multiples_dialog.dismiss();
}
});
multiples_dialog.show();
}
});
The custom dialog code etc works elsewhere (when run from an option menu item click, for example), but when I try it here I get a compile time error Error:(303, 71) error: incompatible types: Intent cannot be converted to Context.
The error is in the line:
final Dialog multiples_dialog = new Dialog(this);
If I replace this with getApplicationContext() I get a run time crash.
I'm confused.
Your declaration is inside of a Object-Declaration (OnClickListener). So this is not your Activity in this case, but the OnClickListener.
Three options to work around:
reference the activity for example with final Dialog multiples_dialog = new Dialog(MainActivity.this)
put the code to show the dialog in a separate function in your Activity and call that function in your OnClickListener.
save a reference to the Context to be used within the OnClickListener, (mark it as final). This option would look something like this:
'
each_pays = (TextView) findViewById(R.id.each_pays);
final Context ctx = this;
each_pays_vert.setOnClickListener(new LinearLayout.OnClickListener() {
#Override
public void onClick(View _v) {
// custom dialog
final Dialog multiples_dialog = new Dialog(ctx);
...
Note: the Application Context can not be used for any UI-actions. This is the reason of the crash when using getApplicationContext().
You can get a Context from a View by using getContext():
final Dialog multiples_dialog = new Dialog(_v.getContext());

java Android - two dialogs, prevent first dialog from closing after second one exits

Initially in my app I am creating an AlertDialog which has three buttons, in which the middle button opens up another AlertDialog. The problem is that when the second AlertDialog closes after a button is pressed, the first one closes with it. I think both AlertDialogs get closed after I press a button on the second AlertDialog.
What I want is for the first AlertDialog to open another AlertDialog that has its own buttons, and when second AlertDialog presses a button, it only closes itself and goes back to the first one. Is there any way to achieve this?
Here is the code for the button used to open the AlertDialog:
final ImageButton fabgroup = (ImageButton) findViewById(R.id.groupButton);
Here's the code for a button that opens an AlertDialog that contains another button that opens another AlertDialog using the middle button (create button) on itself, but closes them both when a button on the second one is pressed (either the yes or no button, which is not what I want as I only want the second one to close itself and go back to the first AlertDialog, and yea this sounds pretty confusing in theory so I can try to clarify if needed):
fabgroup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(CreateNote.this);
helpBuilder.setTitle("Select a group");
helpBuilder.setMessage("Add to group?");
final TextView input = new TextView(mainactiv.this);
input.setSingleLine();
input.setText("");
helpBuilder.setView(input);
helpBuilder.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
Toast.makeText(CreateNote.this, "Page has been added to group", Toast.LENGTH_SHORT).show();
}
});
helpBuilder.setNeutralButton("Create", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//open another alertbox
AlertDialog.Builder helpBuilder2 = new AlertDialog.Builder(CreateNote.this);
helpBuilder2.setTitle("Assign a new group");
helpBuilder2.setMessage("Create group?");
final EditText input = new EditText(CreateNote.this);
input.setSingleLine();
input.setText("");
helpBuilder2.setView(input);
helpBuilder2.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Create Group
Toast.makeText(CreateNote.this, "Group has been created", Toast.LENGTH_SHORT).show();
}
});
helpBuilder2.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog2 = helpBuilder2.create();
helpDialog2.show();
}
});
helpBuilder.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
});
Help would be greatly appreciated.
I eventually managed to solve this problem by creating two separate functions to generate each dialog box, and when one closes it calls the function to create the other one, kinda like recycling (or maybe closer to looping functions). Although, I'm not entirely sure how performance heavy this is, but it seems to do the job without any issues from what I'm testing. If anyone would like to chime in on how this could be an issue, then I'm open to hearing what others have to say about the negative points of using alert dialog boxes this way.
You can show an activity as dialog. Put this in your manifest file.
<activity android:theme="#android:style/Theme.Dialog" android:excludeFromRecents="true"/>
From this answer: Android Activity as a dialog

How to link buttons in android to perform a function?

The Android code consists of two parts Java and the XML. The XML code has helped display the buttons and switches, what code to write so as to get an output of On and Off from it.
Here is an example of basic code that allows a user to access the camera once a button has been pushed in an app:
Button camera = (Button) findViewById(R.id.button1);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
});
button1 corresponds to the android id for button in the xml script. You can then use the object that you've set the button to to set the OnClickListener. Inside of the the camera.setOnclickListener brackets is what you want to happen once the button is clicked. Public void onClick is a function that tells android to do something. I tried to put this in basic terms. If you'd like more specifics, there's really good android studio tutorial videos out there. This one is my favorite:
https://www.youtube.com/watch?v=QAbQgLGKd3Y&list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
or
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
public void selfDestruct(View view) {
// do something
}
if I understand your question you are asking how to react to user click on a button or a switch..
if so, you should create an instance of OnClickListener and implement the onClick() method..
simple example:
Button b = findViewById(R.id.btn_id);
b.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});

Dismissing Alert Dialog with custom interface using custom button

I have a custom alert dialog. I am currently trying to alter the onclicklisteners for my two buttons. Previously I have used the following code.
builder.setNegativeButton("Nope", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
\\code here which is not relevant to question
}
});
However, now since the dialog has a custom view and custom buttons, I use the following approach.
Button confirm = (Button) windowView.findViewById(R.id.confirmbutton);
Button cancel = (Button) windowView.findViewById(R.id.negatebutton);
cancel.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
}
});
My question is how do I dismiss the dialog within the cancel button listener if I can't access the dialog variable. I want to use the AlertDialog I am already using and do not want a solution with a different type of dialog.
What you need to do, is just keep a reference of the Dialog, then you can call the dismiss method. In my example, i keep the reference as a property.
private Dialog dialog;
#Override
public void onResume() {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
LinearLayout llView = new LinearLayout(this);
Button btnDismiss = new Button(this);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
llView.addView(btnDismiss);
adb.setView(llView);
dialog = adb.create();
dialog.show();
super.onResume();
}
It's important keep the reference as a property, cause the reference must be final to be accessible inside the onClick method, and since the dialog it's not created yet, you cant keep the final reference in a method variable, then keep it in a property.

Categories