android : how to align message in alertDialog? - android

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

Related

Custom Alert dialog is not getting dismissed on click of a button and showing a white background below the custom dialog

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

How to change the text gravity on alertdialog

I've been searching for a while and I haven't found the solution to this problem of mine yet, I have a alertdialog with a few items (Here it is)
But as you can see, the text is aligned to the left. Is there a way to align the items text to the center?
Here's a bit of my code
chosenItem.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose a category");
// add a list
String[] items = {"Hey", "stack", "overflow", "please", "help", "Medical", "Ammo"};
// String[] items = {"Weapons", "Tools", "Items", "Clothing", "Components", "Medical", "Ammo"};
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
reciclador(3, rSlotFrame, rSlotText, rSlotProbabilidade, 30, 50, 1, 0, 100, 100, 100, 0, "cloth_icon", "metalfrags_icon", "sewingkit_icon", "");
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getWindow().setLayout(800, 700);
}
});
}
Thanks in advance.
you can use custom dialog for that like this create a custom layout like this
<?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:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text " />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="description" />
</LinearLayout>
now create custom dailog llike below code
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.setContentView(R.layout.custom_dialog_layout);
Textview tvTitle;
tvTitle = (Textview) dialog.findViewById(R.id.tvTitle);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
tvTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
dialog.show();
You can use custom view in AlertDialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(YourActivity.this);
View alertView = getLayoutInflater().inflate(R.layout.fragment_dialog, null, false);
alertDialog.setView(alertView);
Add your custom requirement of your text in your layout file.
From documenation:
If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.
you need to apply theme for the dialog on the initial state
AlertDialog dialog = new AlertDialog(context, R.theme.DialogTheme);
and declare theme in your theme.xml or styles.xml
<style name="DialogTheme" parent="Animation.AppCompat.Dialog">
<item name="android:itemTextAppearance">#style/DialogTextAppear</item>
</style>
<style name="DialogTextAppear" parent="TextAppearance.AppCompat">
<item name="android:gravity">center</item>
</style>
You can find AlertDialog Title view by this code and set gravity manual but first you must create a AlertDialogBuilder and one Alert dialog like in code
MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(this);
dialogBuilder
.
.
.
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
LinearLayout titleTemplate = alertDialog.findViewById(R.id.title_template);
titleTemplate.setGravity(Gravity.RIGHT);
You can find all view in alert_dialog.xml and find in code and use it.

How controll alertdialog button text size on android?

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;

How to show textview on Alert dialogue

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

Create dialog box with 2 radio buttons inside

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

Categories

Resources