AlertDialog has not come out because cannot getText of a button - android

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

Related

How Can i hide the MainActivity while pop up AlertDialog?

I want to hide MainActivity when AlertDialog is shown.
I create new Activity and put my code in it and call it from MainActivity by using Intent.
My Code of AlertDialog :
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder1 = new AlertDialog.Builder(Dialog.this);
builder1.setTitle("RONQ");
builder1.setMessage(MainActivity.mesgList.get(MainActivity.currentMsgNumber));
builder1.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder1.show();
}
}
And Intent code is :
Intent intent = new Intent();
intent.setClass(MainActivity.this, Dialog.class);
startActivity(intent);
You don't need Intent:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("message")
.setPositiveButton(getString(R.string.update), (dialogInterface, i) -> {
//do something
}).setNegativeButton(getString(R.string.cancel), (dialogInterface, i) -> {
//do something
}).setCancelable(false).create().show();

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.

how to add return=true in wireless settings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
im working on a project and im stuck, what i want is after enabling wifi or internet, the back button(hardware button) should bring me back to SplitScreen.xml instead of closing the app please dont give any code to add , as my code is working fine , insted ,correct my code
}}
To achieve the behavior you want, move your call to open Wireless Settings to the MainActivity, and add a boolean variable to its Intent to indicate whether the Settings should be opened in MainActivity.onCreate.
Total Redo Edit
This is your SplashActivity.java. The only problem you should have is the layout name. I don't remember what you named it.
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog.OnClickListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class SplashScreen extends Activity
{
static ConnectivityManager cm;
AlertDialog dailog;
AlertDialog.Builder build;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);// checking
build = new AlertDialog.Builder(this);
setContentView(R.layout.activity_splash);
// if connection is
// there screen goes
// to next screen
// else shows
// message
if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()
|| cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting())
{
Log.e("cm value", "" + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting());
Toast.makeText(this, "Internet is active", 2000).show();
Thread mythread = new Thread() {
public void run()
{
try
{
sleep(5000);
}
catch (Exception e)
{
}
finally
{
Intent intent = new Intent(SplashScreen.this,
MainActivity.class);
startActivity(intent);
finish();
}
}
};
mythread.start();
}
else
{
build.setMessage("This application requires Internet connection. Would you connect to internet ?");
build.setPositiveButton("Yes", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Intent intent = new Intent(SplashScreen.this,
MainActivity.class);
intent.putExtra("showSettings", true);
startActivity(intent);
finish();
//startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
});
build.setNegativeButton("No", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
build.setMessage("Are sure you want to exit?");
build.setPositiveButton("Yes", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
finish();
}
});
build.setNegativeButton("NO", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
finish();
Intent intent = new Intent(SplashScreen.this,
SplashScreen.class);
startActivity(intent);
dialog.dismiss();
}
});
dailog = build.create();
dailog.show();
}
});
dailog = build.create();
dailog.show();
}
}
}
And this is your MainActivity.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.Toast;
public class MainActivity extends Activity
{
ToggleButton toggle;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggle = (ToggleButton)findViewById(R.id.tglbtn1);
toggle.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (toggle.isChecked())
{
Toast.makeText(getApplicationContext(), "Boosting For Next 60 Minutes, Minimize the Application", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Boosting Turned Off", Toast.LENGTH_LONG).show();
}
}
}
);
Intent intent = getIntent();
boolean show = intent.getBooleanExtra("showSettings", false);
if (show)
{
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
}
}
Fix the layout name issue in SplashScreen and you should be good. I just ran this code and it performs as expected.

how to use alertdialog

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.

How do i add a close button to this alert dialog in Android

I need help implementing the code to add a negative or positive button to close my alert dialogue (any help would be much appreciated). I think some of my punctuation in the code needs altering as well so any help would be great :)
package kevin.erica.box;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.Random;
public class TheKevinAndEricaBoxActivity extends Activity {
/** Called when the activity is first created. */
private String[] myString;
private String list;
private String[] myString2;
private String list2;
private static final Random rgenerator = new Random();
private static final Random rgenerator2 = new Random();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
list = myString[rgenerator.nextInt(myString.length)];
myString2 = res.getStringArray(R.array.myArray2);
list2 = myString2[rgenerator.nextInt(myString2.length)];
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton1);
ib.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View erica) {
AlertDialog.Builder b = new AlertDialog.Builder(
TheKevinAndEricaBoxActivity.this);
b.setMessage(myString[rgenerator.nextInt(myString.length)]);
b.setTitle(R.string.title1);
Dialog d = b.create();
d.show();
}
});
}
}
You can use the below code in your app::::
AlertDialog.Builder b = new AlertDialog.Builder(TheKevinAndEricaBoxActivity.this);
b.setMessage(myString[rgenerator.nextInt(myString.length)]);
b.setTitle(R.string.title1);
b.setPositiveButton("Button Text", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which){
//stuff you want the button to do
}
});
b.setNegativeButton("Button Text", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which){
//stuff you want the button to do
}
});
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
Search in Google before you post a question..
Here is a guide.
Take notice of the functions "set___Button".
b.setNegativeButton("Button Text", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which){
//stuff you want the button to do
});
You need make a custom dialog. See the sample:
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
This is the full sample:
http://developer.android.com/guide/topics/ui/dialogs.html

Categories

Resources