How controll alertdialog button text size on android? - 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;

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

Android trouble in setting text from Edit TextView from alert dialog to EditText View

I have an Text View on which when user click will open a alert dialog. The dialog consists of Edit Text and a button. In Edit text when user enters it's height and clicks on "OK" button,the entered height will gets displayed to the Edit Text.
MainActivity.java
et_weightAndHeight = (EditText) findViewById(R.id.et_weightAndHeight);
et_weightAndHeight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater li = LayoutInflater.from(AccountActivity.this);
View promptView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountActivity.this);
alertDialog.setView(promptView);
final EditText input = (EditText)findViewById(R.id.editTextDialogUserInput);
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
et_weightAndHeight.setText(input.getText().toString());
}
});
AlertDialog aD = alertDialog.create();
aD.show();
}
});
}
}
prompts.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Weight in lbs : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</LinearLayout>
Without proper explanation and logcat, I am assuming you are getting NullPointerException because you haven't initialized the TextView inside the dialog properly.
Change,
final TextView input =(TextView)findViewById(R.id.editTextDialogUserInput);
to
final TextView input =(TextView)promptView.findViewById(R.id.editTextDialogUserInput);

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

How can I access the EditText field in a DialogBox?

How can I access the EditText field in a DialogBox?
Place your EditText widget into your dialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/myEditText"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
Then inflate the View from within your Dialog and get the content of the EditText.
private Dialog myTextDialog() {
final View layout = View.inflate(this, R.layout.myDialog, null);
final EditText savedText = ((EditText) layout.findViewById(R.id.myEditText));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(0);
builder.setPositiveButton("Save", new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String myTextString = savedText.getText().toString().trim();
}
});
builder.setView(layout);
return builder.create();
}
After pressing the "Save"-button myTextString will hold the content of your EditText. You certainly need to display the dialog of this example first.

android : how to align message in alertDialog?

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

Categories

Resources