Hell i am trying to make alert dialogue to show highscore point.but i cant add textview on my dialogue , highscore stores in textview. here is my code i need to show textview when i click NO button.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Time is up!")
.setCancelable(false)
.setPositiveButton("Restart!",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
App2Activity.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();
}
}.start();
create xml file dialog_layout_pro.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#dedede"
>
<TextView
android:id="#+id/tv_dialog"
android:layout_margin="30dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textColor="#444444"
android:text="Please wait..."
android:textSize="30sp" />
</RelativeLayout>
and in java class
{
Dialog cusDialog;
cusDialog = new Dialog(MyTestClass.this, R.style.CustomDialog);
cusDialog.setContentView(R.layout.dialog_layout_pro);
TextView text = (TextView) cusDialog.findViewById(R.id.tv_dialog);
text.setText("here your counter value");
cusDialog.setCancelable(false);
cusDialog.show();
}
Related
I created a custom alert dialog to show in my app. It has an imageView, two textViews and a button. I am trying to show this custom alert dialog once my main activity launches.I am calling this custom alert dialog.show() in a separate method according to my requirement.The custom alert dialog is showing up but on click of a button it is not getting dismissed, also the custom alert dialog is showing a extra white background.
showCustomAlertDialog() Method
public void showCustomAlertDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View promptView =layoutInflater.inflate(R.layout.reminder_alert_dialog, null);
final AlertDialog builder = new AlertDialog.Builder(this).create();
Button recordButton = (Button)promptView.findViewById(R.id.record_button);
recordButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Dismiss the dialog");
builder.dismiss();
}
});
builder.setView(promptView);
builder.show();
}
reminder_alert_dialog Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="500dp"
android:background="#drawable/ic_alert" />
<TextView
android:id="#+id/greetings_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GOOD MORNING, KEVIN"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/medicines_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Have you taken your medicines today?"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="normal"
android:layout_below="#id/greetings_text"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/record_button"
android:layout_width="160dp"
android:layout_height="40dp"
android:background="#3BC4B8"
android:text="Record"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_below="#id/medicines_text"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Coming to the extra white background, you have to apply the Window Background colour to transparent for the AlertDialog with this code
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
And the issue where dialog isn't showing, is the System.out.println() called? I would also recommend you setting the view for the AlertDialog.Builder before setting the listeners like,
final AlertDialog builder = new AlertDialog.Builder(this).create();
builder.setView(promptView);
Button recordButton = (Button)promptView.findViewById(R.id.record_button);
recordButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Dismiss the dialog");
builder.dismiss();
}
});
Try like this...
//class variables
AlertDialog dialog;
Button dismiss;
//Add your other views if required
//in onCreate() activity method
makeDialog();
//makeDialog() method code
void makeDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
View root = getLayoutInflater().inflate(R.layout.your_layout,null);
dismiss = root.findViewById(R.id.your_button_id);
//another views initialization goes here...
dismiss.setOnClickListener(new View.OnClickListener(){
dialog.dismiss();
//your logic or job...
});
builder.setView(root);
dialog = builder.create();
}
finally show the dialog whenever you want
dialog.show();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
This image took from Deep Relax app from Play Store:
How do I create a popup menu? I am creating an app I want to ask the user to provide feedback?
Try this use custom dialog for this purpose:
Create layout like this custom_dialog_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="title"
android:textColor="#000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting," />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="description" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:id="#+id/btnNotNOw"
android:text="not now"
android:textColor="#040d28" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#null"
android:text="Never"
android:id="#+id/btnNever"
android:textColor="#040d28" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#null"
android:text="sure"
android:id="#+id/btnSure"
android:textColor="#040d28" />
</LinearLayout>
</LinearLayout>
Now create dialog using below code:
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.setContentView(R.layout.custom_dialog_layout);
Button btnNotNOw, btnNever, btnSure;
btnNotNOw = (Button) dialog.findViewById(R.id.btnNotNOw);
btnNever = (Button) dialog.findViewById(R.id.btnNever);
btnSure = (Button) dialog.findViewById(R.id.btnSure);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
dialog.show();
btnNever.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
btnNotNOw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
btnSure.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
You can create it by Custom Dialog :
Custom Dialog :
For this you have to create one xml file and have to attached.
final Dialog dialog1 = new Dialog(LabCheckOutActivity.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setCancelable(true);
dialog1.setContentView(R.layout.dialog_patient_details);
dialog1.show();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Top Heading");
// Setting Dialog Message
alertDialog.setMessage("Message");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Sure",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Not Now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Not Now", Toast.LENGTH_SHORT).show();
}
});
// Setting Netural "Cancel" Button
alertDialog.setNeutralButton("Never", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Never",
Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
Check this
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this, AlertDialog.THEME_TRADITIONAL);
builder.setMessage("Positive,Negative and Neutral Button")
builder.setCancelable(false);
builder.setTitle("Custom Dialog");
// Set the positive/yes button click listener
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Set the negative/no button click listener
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Set the neutral/cancel button click listener
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
// Display the alert dialog on interface
dialog.show();
Hello senior developer.
recently I create alertdialog.
I know title size, message size controll
but I don't know controll button text size
How controll button text size ?
I must this format please
thanks.
View view = getLayoutInflater().inflate(R.layout.connecting_error_dialog, null);
TextView txtTitle = (TextView) view.findViewById(R.id.title);
txtTitle.setTextSize(40);
txtTitle.setTextColor(Color.RED);
txtTitle.setText("fail");
TextView message = (TextView) view.findViewById(R.id.message);
message.setTextSize(30);
message.setText("dddddd");
AlertDialog.Builder builder = new AlertDialog.Builder(Connectingreceiver.this);
builder.setView(view)
.setCancelable(false)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
moveTaskToBack(true);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="HelloAlert!"/>
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip"/>
</LinearLayout>
The alert dialog needs to be created first and then it's elements become accessible to be changed.
final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button btnPositive = alert.getButton(Dialog.BUTTON_POSITIVE);
btnPositive.setTextSize("desired font size");
Button btnNegative = alert.getButton(Dialog.BUTTON_NEGATIVE);
btnNegative.setTextSize("desired font size");
}
});
return alert;
I want to create a dialog box when a button is clicked inside the dialog box I need 2 radio buttons which are linked with mobile default music and other is external memory of the mobile. Anyone can help me out this here is my code as follows?
buttonSound = (Button) findViewById(R.id.btn_sound);
buttonSound.setOnClickListener (new OnClickListener(){
public void onClick(View v){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
Log.d("TAG","button inside mobile");
alertDialogBuilder
//.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Sound",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
AlarmActivity.this.finish();
}
});
// builder.setPositiveButton("Sound", );
alertDialogBuilder
//.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("SdCard",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
Log.d("TAG","button inside sdCard");
AlarmActivity.this.finish();
}
});
alertDialogBuilder.show();
And it is showing error as follows given below
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Use AlarmActivity.this or this intead of getApplicationContext().
Create in layout in res/layout
radio_dialog_layout.xml
<RadioButton
android:id="#+id/rd_!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<RadioButton
android:id="#+id/rd_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/rd_!"
android:text="B" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rd_2"
android:layout_centerInParent="true"
android:text="OK" />
</RelativeLayout>
create dialog in activity by using this code:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.radio_dialog_layout);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
// there are a lot of settings, for dialog, check them all out!
// set up radiobutton
RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_);
RadioButton rd2 = (RadioButton) dialog.findViewById(R.id.rd_2);
// now that the dialog is set up, it's time to show it
dialog.show();
You can also refer this tutorials: How to add lists, Checkboxes and Radio buttons to an AlertDialog – Android
Try this..
Change this..
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
to
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AlarmActivity.this);
EDIT
radio_btn.xml
<?xml version="1.0" encoding="UTF-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/status_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >
<RadioButton
android:id="#+id/default_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default music" />
<RadioButton
android:id="#+id/external_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="External card music" />
</RadioGroup>
JAVa
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.radio_btn, (ViewGroup) getCurrentFocus());
RadioButton default_btn = (RadioButton) dialoglayout.findViewById(R.id.default_btn);
RadioButton external_btn = (RadioButton) dialoglayout.findViewById(R.id.external_btn);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AlarmActivity.this);
Log.d("TAG","button inside mobile");
alertDialogBuilder
//.setMessage("Click yes to exit!")
.setCancelable(false)
.setView(dialoglayout);
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
AlarmActivity.this.finish();
}
});
alertDialogBuilder.show();
Create one xml file,
<RadioButton
android:id="#+id/radiobutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi" />
<RadioButton
android:id="#+id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radiobutton1"
android:text="Bye" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/radiobutton2"
android:layout_centerInParent="true"
android:text="Click" />
and create Dialog in activity,
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_main);
dialog.setTitle("My Dialog Box");
dialog.setCancelable(true);
// set up radiobutton inside dialog box
RadioButton rb1 = (RadioButton) dialog.findViewById(R.id.radiobutton1);
RadioButton rb2 = (RadioButton) dialog.findViewById(R.id.radiobutton2);
// Show the dialog
dialog.show();
i have to align text by middle in android alertdialog.
but i cannot find way...
anyone knows how to this?
try this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My Title");
builder.setMessage("your message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.show();
I know this thread is old but might help some people :D
TextView title = new TextView(this);
title.setText("Client details not saved!");
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
// title.setTextColor(getResources().getColor(R.color.greenBG));
title.setTextSize(23);
TextView msg = new TextView(this);
msg.setText("You're going to lose all the information if you continue!");
msg.setPadding(10, 10, 10, 10);
msg.setGravity(Gravity.CENTER);
msg.setTextSize(18);
DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
finish();
}
}
};
Builder builder = new AlertDialog.Builder(this);
builder.setCustomTitle(title);
builder.setView(msg);
builder.setCancelable(true);
builder.setPositiveButton("Yes", onClick);
builder.setNegativeButton("No", onClick);
AlertDialog dialog = builder.create();
dialog.show();
Best way is to design Custom Dialog box.
view_dialog_box.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#A9E2F3">
<TextView
android:id="#+id/txtDiaTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connection Alart"
android:textColor="#color/Black"
android:textStyle="bold"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#2E9AFE"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:id="#+id/txtDiaMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="No Internet Connection"
android:textColor="#color/Black" />
<Button
android:id="#+id/btnOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="OK"
android:textColor="#color/Black"
android:textStyle="bold"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#color/White"/>
Then it use in java file
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.view_dialog_box);
// set the custom dialog components - text and button
TextView text = (TextView) dialog.findViewById(R.id.txtDiaTitle);
TextView image = (TextView) dialog.findViewById(R.id.txtDiaMsg);
Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Just use that method and your dialog title and message will appear at center:
public static void openDialog(Context context, String message) {
TextView title = new TextView(context);
// You Can Customise your Title here
title.setText("Information Message");
title.setBackgroundColor(Color.BLACK);
title.setPadding(10, 15, 15, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(22);
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setCustomTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
// You Can Customise your Message here
TextView messageView = (TextView) alertDialog
.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
}
You can use your custom layout for alert dialog layout. To align default alert dialog layout message center you can do
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("hello world");
alertDialog = builder.show();
TextView messageText = (TextView) alertDialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
Be careful, if you set messageText with findViewById before you call builder.show() you'll get a null pointer exception.
Have your TextView fill the parent and give it a center gravity.
<TextView ... android:layout_width="fill_parent" android:gravity="center" />
You would have to use one of the constructors provided for AlertDialog in Android, while creating one.
AlertDialog(Context context, int theme)
Construct an AlertDialog that uses an explicit theme.
This link will help you. Since you want the text to be centered, you would want to give the gravity attribute, the value 'center'.
Try this - it will do the trick.
AlertDialog.Builder completeDialog = new AlertDialog.Builder(Main.this);
TextView resultMessage = new TextView(Main.this);
resultMessage.setTextSize(22);
resultMessage.setText("Upload completed!");
resultMessage.setGravity(Gravity.CENTER);
completeDialog.setView(resultMessage);
completeDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#SuppressLint("DefaultLocale")
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
completeDialog.show();