how to use alertdialog - android

package com.progme.wallkon;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class NextActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
ImageView im1;
im1 = (ImageView)findViewById(R.id.a_01_b);
im1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im2;
im2 = (ImageView)findViewById(R.id.a_02_b);
im2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im3;
im3 = (ImageView)findViewById(R.id.a_03_b);
im3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(1);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Gmelon");
builder.setMessage("setting?");
builder.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
});
builder.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
});
return builder.create();
}
}
I wrote code in activity.java like this..
I want to use dialog in im1, im2, im3, and each have to get another event.
Then, I have to write 3 dialog?
and how I can set [//TODO Auto...] here that I use is like..
first dialog for im1,
second dialog for im2,
third dialog for im3..
Please help..

You can write a private variable for the alert dialog and reuse it, but not at the same time
private AlertDialog mDialog = new AlertDialog.Builder(this)
.setTitle("Gmelon")
.setMessage("setting?")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
})
.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
}).create();
now you can show the dialog where ever you want in your code.

It looks like you could just use showDialog(x) to me unless there is more to this question.

Related

App stopped working android

I don't understand why my "App" has stopped working?
This is my MainActivity.class code:-
package com.apps.nishant.iwillguessyournumber;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnplay = (Button)findViewById(R.id.cmdPlay);
private Button btnexit = (Button)findViewById(R.id.cmdExit);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnButtonClickListener();
}
public void OnButtonClickListener() {
btnplay.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
btnexit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setMessage("Do you really wanna exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
}
);
}
}
and my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.apps.nishant.iwillguessyournumber.MainActivity">
<Button
android:id="#+id/cmdPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="219dp" />
<Button
android:id="#+id/cmdExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="328dp" />
</android.support.constraint.ConstraintLayout>
Whenever I am running this app on Genymotion Android:6.0.0 and API 23, it is showing "Unfortunately, (app name) is not working"
What is the problem in my code/platform?
I've tried restarting my apps, looked up a few questions but couldn't yet get my answer when I open a new Activity using Intent, the second activity can't parse the xml file.
The source of your app crash is the following 2 lines,
private Button btnplay = (Button)findViewById(R.id.cmdPlay);
private Button btnexit = (Button)findViewById(R.id.cmdExit);
The method findViewById() should be called inside the onCreate() after the setContentView(). The following code should work,
package com.apps.nishant.iwillguessyournumber;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnplay;
private Button btnexit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnplay = (Button)findViewById(R.id.cmdPlay);
btnexit = (Button)findViewById(R.id.cmdExit);
OnButtonClickListener();
}
public void OnButtonClickListener() {
btnplay.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
btnexit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setMessage("Do you really wanna exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
}
);
}
}

Android : Evaluate Dialog

I want to evaluate a Dialog under Android.
My problem is that the Dialog does not wait
with returning a value until I pressed a
button. And when I use a Semaphore the
program hangs at Semaphore.acquire().
Why does it hang at Semaphore.acquire()?
Can you see where I go wrong?
Here is the main activity
package com.example.modaldialog;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDialog meinDialog = new mDialog(this);
if (meinDialog.ShowMyModalDialog() == 1)
Toast.makeText(this, "Pressed Button 1",
Toast.LENGTH_LONG).show();
}
}
and here the dialog class
package com.example.modaldialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.Toast;
import java.util.concurrent.Semaphore;
import android.app.Activity;
import java.lang.Runnable;
public class mDialog
{
int pressedButtonID;
Activity act;
private final Semaphore dialogSemaphore;
mDialog(Activity act_in)
{
act = act_in;
dialogSemaphore = new Semaphore(0, true);
};
final Runnable mMyDialog = new Runnable()
{
public void run()
{
AlertDialog errorDialog = new
AlertDialog.Builder(act).create();
errorDialog.setMessage("Press a Button!");
errorDialog.setButton("Button2", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pressedButtonID = 2;
dialogSemaphore.release();
}
});
errorDialog.setButton2("Button1", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pressedButtonID = 1;
dialogSemaphore.release();
}
});
errorDialog.setCancelable(false);
errorDialog.show();
}
};
public int ShowMyModalDialog() //should be called from non-UI thread
{
pressedButtonID = 0;
act.runOnUiThread(mMyDialog);
try
{
dialogSemaphore.acquire();
}
catch (InterruptedException e)
{
}
return(pressedButtonID);
}
}
I am not much aware about the Semaphore but you can achieve the same with Interface. I have updated your code please find it below,
package com.example.modaldialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.Toast;
public class mDialog
{
int pressedButtonID=0;
private HandleDialogEvent dialogEvent;
mDialog(HandleDialogEvent dialogEvent)
{
this.dialogEvent = dialogEvent;
}
public void ShowMyModalDialog() //should be called from non-UI thread
{
pressedButtonID = 0;
AlertDialog errorDialog = new
AlertDialog.Builder(act).create();
errorDialog.setMessage("Press a Button!");
errorDialog.setButton("Button2", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pressedButtonID = 2;
dialogEvent.getDialogId(pressedButtonID);
}
});
errorDialog.setButton2("Button1", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pressedButtonID = 1;
dialogEvent.getDialogId(pressedButtonID);
}
});
errorDialog.setCancelable(false);
errorDialog.show();
}
public interface HandleDialogEvent{
public void getDialogId(int Id);
}
}

Button issue eclipse

I want to create a simple app in eclipse with a button. I want to make it so that after the button is pushed 10 times a message will pop up. The problem is that when I start the app and push the button 10 times , nothing happens. Could you please tell me what I've done wrong?
Here's my activity file:
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button gamebutton = (Button) findViewById(R.id.gamebutton);
}
#Override
public void onClick(View v){
//TODO Auto-generated method stub
int clicked = 0;
clicked++;
if( clicked==10){
AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
gamebuild.setMessage("Good");
gamebuild.setCancelable(false);
gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Game.this.finish();
}
});
gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
}
}
Thanks for response! I've edited the activity file this way:
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{
int clicked = 0;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button gamebutton = (Button) findViewById(R.id.gamebutton);
}
#Override
public void onClick(View v){
//TODO Auto-generated method stub
clicked++;
if( clicked==10){
AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
gamebuild.setMessage("Good");
gamebuild.setCancelable(false);
gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Game.this.finish();
}
});
gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
}
}
Still it's not working in a proper way. Sorry for dumb questions: I'm new to android.
new edit:
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{
int clicked = 0;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button gamebutton = (Button) findViewById(R.id.gamebutton);
gamebutton.setOnClickListener(this);
}
#Override
public void onClick(View v){
//TODO Auto-generated method stub
clicked++;
if( clicked==10){
AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
gamebuild.setMessage("Good");
gamebuild.setCancelable(false);
gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Game.this.finish();
}
});
gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
}
}
You put the counter inside the onClick function, causing it to reset every time. So, move
int clicked = 0;
from inside the onClick to before your onCreate such as:
int clicked = 0;
#Override
protected void onCreate....
This will make it set to 0 once, then stay equal to whatever it's last value was as long as the app is open and not killed.
Problem is that variable clicked is declared inside onClick. So it's always zero. You have to declare it globally, inside class Game.

AlertDialog has not come out because cannot getText of a button

My Alert dialog message doesn't come out because I cannot get the text of the button. After that the program will directly go to my EventActivity. How can settle this problem?
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == insertButton.getId()) {
if(colorButton.getText().toString().equals("Color")){
colorAlert.show();
}
}
}
This is variable
AlertDialog colorAlert;
AlertDialog in the OnCreate()
AlertDialog.Builder CA = new AlertDialog.Builder(this);
CA.setTitle("Alert Message!");
CA.setMessage("Please insert the level of important for the event.");
CA.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
colorAlert.dismiss();
startActivity(new Intent(this, EventActivity.class));
}
});
colorAlert = CA.create();
When you compare a String in Java, use YOUR_STRING.equals("Color");
Update:
change your while into if , because your are running it once.
The move your
startActivity(new Intent(this, EventActivity.class));
in your alertbox positive button handler.
Comparing Strings in Java.
Ok look like you need more help, here is one solution... i let you deal with the EventActivity ;)
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
final Context context=this;
public static String EXTRA_MESSAGE;
Button colorButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
colorButton = (Button) findViewById(R.id.button1);
colorButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(colorButton.getText().toString().equals("Color")){
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setMessage("myDialog").setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent intent = new Intent(MainActivity.this, EventActivity.class);
String message = colorButton.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}).setNegativeButton("cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog myAlert = alertBuilder.create();
myAlert.show();
}
}
});
}
Adding to wtsang02 answer, you shouldn't put your alert.show in a while like this one... I see coming the infinite loop ;)

Android, what can i do with that listener i click on the button and doesnt responce

i don't know what is wrong with that code ..the emulator works but the button does not work with the listener.i did everything i cleaned the project and also i made a listener and it supposed to work.
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
public class MainActivity extends Activity {
CharSequence[] items = { "Goolge", "Apple", "MaC Os" };
boolean[] checkedItems = new boolean[items.length];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button btn= (Button)findViewById(R.id.dialog);
// btn.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
showDialog(0);
}
protected Dialog onCreatDialog(int id) {
switch (id) {
case 0 :
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("this is a List of items..");
builder.setNegativeButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getBaseContext(), "Ok clicked",
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "cancel cliked",
Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(
getBaseContext(),
items[which]
+ (isChecked ? " checked"
: " unchecked"),
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
return null;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="#string/click_me" />
</RelativeLayout>
uncomment this code:
Button btn = (Button)findViewById(R.id.dialog);
btn.setOnClickListener(new View.onClickListener(){
#Override
public void onClick(View v) {
//Do your stuff
}
});
If you want to use your Activity as onClickListener, keep "this" on setOnClickListener, but your activity must implements OnClickListener interface, and implements its methods to work.

Categories

Resources