How can I change the font of items in alertDialog? [duplicate] - java

This question already has answers here:
Android - Using Custom Font
(21 answers)
Closed 7 years ago.
I have alertDialog with items,
this is the code:
final CharSequence[] options = { "Back","continue"};
AlertDialog.Builder builder = new AlertDialog.Builder(myActivity.this);
builder.setTitle(customTitle);
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Back"))
{
//do something;
}
else if (options[item].equals("continue"))
{
dialog.dismiss();
}
}
});
builder.show();
I have custom font in my assets folder, how can I connect the item's font to this font?
I saw many answers but there is no answer for itmes in dialog, all answers was about the title or text uses by textView,
I need to change the font of my items, someone can help me?

To do this you use alert builder to build your alert. You then get the TextView from this alert and then you set the typeface for the alert.
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/yourfont.ttf");
textView.setTypeface(face);

Related

Android : EditText and TextView are misaligned in alertdialog

In this code, I am making an AlertDialog with attributes title, EditText, TextView, Cancel Button, and Email me Button.
EditText and TextView not aligned/set properly.
// Alert Dialog
private void showForgotpasswdDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Forgot your password?");
// Set linear layout
LinearLayout linearLayout = new LinearLayout(this);
// View to set an dialog
final EditText Email = new EditText(this);
Email.setHint("Email");
Email.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
// Text view
linearLayout.addView(Email);
builder.setView(linearLayout);
// Text view
final TextView tv = new TextView(this);
tv.setText("Unfortunately, if you have never given us your email, we will not be able to reset your password");
linearLayout.addView(tv);
builder.setView(linearLayout);
// Buttons for EMAIL ME
builder.setPositiveButton("EMAIL ME", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// Input email
String email = Email.getText().toString().trim();
beginforgotpasswd(email);
}
});
// Buttons for CANCEL
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int i) {
// Dismiss dialog
dialog.dismiss();
}
});
// Show dialog
builder.create().show();
}
Please see the following screenshot showing the misaligned EditText:
just try it:
linearLayout.setOrientation(LinearLayout.VERTICAL);
to get proper orientation!

App crashes on initialization views in Dialog [duplicate]

This question already has answers here:
findviewbyid returns null in a dialog
(7 answers)
Closed 3 years ago.
I'm trying to open Dialog window while clicking on some text view in recyclerview element.
Opening a window works perfectly if I don't try to initialize the texts view.
If I do, then the app crashes.
in my guestMessage setOnClickListener i'm open a Dialog.
In onClick i'm initialize TextView that should contain the texts (name and message).
#Override
public void onBindViewHolder(#NonNull final GuestViewHolder holder, final int position) {
final Guest guest = guests.get(position);
holder.guestName.setText(guests.get(position).getGuestName());
//holder.guestMessage.setText(guests.get(position).getGuestMessage());
holder.guestPhoneNumber.setText(guests.get(position).getGuestPhoneNumber());
holder.personsCount.setText(guests.get(position).getPersonsCount());
holder.guestMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog messageDialog = new Dialog(context);
TextView name = messageDialog.findViewById(R.id.messageTitlePopUp);
TextView message = messageDialog.findViewById(R.id.messageInPopup);
messageDialog.setContentView(R.layout.message_dialog_popup);
name.setText(guests.get(position).getGuestName());
message.setText(guests.get(position).getGuestMessage());
messageDialog.show();
}
});
}
You are initialising views before setting up content view on dialog, which cause the issue.
To solve issue, just change the initialisation order for your dialog code.
final Dialog messageDialog = new Dialog(context);
messageDialog.setContentView(R.layout.message_dialog_popup);
TextView name = messageDialog.findViewById(R.id.messageTitlePopUp);
TextView message = messageDialog.findViewById(R.id.messageInPopup);

How to add RadioGroup and EditText to AlertDialog?

Hi I want to get user name and user gender using alert dialog so I add:
AlertDialog.Builder user= new AlertDialog.Builder(this);
user.setTitle("New Student");
user.setMessage("What is your Name?");
final RadioGroup genderRG= new RadioGroup(this);
RadioButton radiomr = new RadioButton(this);
radiomr.setText("Mr");
// radiomr.setId(1);
genderRG.addView(radiomr);
RadioButton radiomiss = new RadioButton(this);
radiomiss.setText("Miss");
// radiomiss.setId(2);
genderRG.addView(radiomiss);
user.setView(genderRG);
final EditText input = new EditText(this);
user.setView(input);
user.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int whichButton)
{
// get data
}
});
user.setNegativeButton("CANCEL", null);
user.create().show();
But when I run it, It just gives me an EditText.
Please! How can I get Gender and Name in same AlertDialog using radio button or spinner.
You should create your own layout and set it via setView().
You are instead inflating just the EditText, this is why you have just that in the dialog.
In any case it is better to use DialogFragment to create your own dialog version.
More here https://developer.android.com/reference/android/app/DialogFragment

Android Linkify text in dialog

Hi I've gone through all of the different linkify tutorials I could find but none of them work here is my current code:
final SpannableString s = new SpannableString("Please send any questions to email#fake.com");
Linkify.addLinks(s, Linkify.EMAIL_ADDRESSES);
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setTitle("Warning!")
.setMessage(s)
.setCancelable(false)
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Decline", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Activity.this.finish();
}
}).show();
However when I actually run the app it shows the text like blue and underlined as if it were linked but selecting the text doesn't prompt to open the email app. I've also tried with urls and the browser doesn't work is there something that's missing?
Thanks for any help.
In order to have a clickable area on dialog you need to use TextView (View) and set autoLink=all in layout file or invoke setAutoLinkMask() method from within the code.
final SpannableString s = new SpannableString("Please send any questions to email#fake.com");
//added a TextView
final TextView tx1=new TextView(this);
tx1.setText(s);
tx1.setAutoLinkMask(RESULT_OK);
tx1.setMovementMethod(LinkMovementMethod.getInstance());
Linkify.addLinks(s, Linkify.EMAIL_ADDRESSES);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Warning!")
.setCancelable(false)
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Decline", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setView(tx1)
.show();
Here's Kotlin in case it's helpful:
val s = SpannableString(getString(R.string.actions_list_info_button_body))
val tx1 = TextView(context!!)
tx1.text = s
tx1.autoLinkMask = RESULT_OK
tx1.movementMethod = LinkMovementMethod.getInstance()
The rest is the same.
The result likely will not look great so you probably will want to add some padding as well:
// Adjust Padding to dp
val scale: Float = resources.displayMetrics.density
val dpAsPixels: Int = (25 * scale + 0.5f).toInt()
text.setPadding(dpAsPixels,20,dpAsPixels,0)
Alternatively, you can reuse the TextView created.
AlertDialog.Builder builder;
builder.setMessage(R.string.yourMessage);
Dialog dialog = builder.create();
dialog.setOnShowListener(d -> {
TextView text = dialog.getWindow().findViewById(android.R.id.message);
text.setAutoLinkMask(Linkify.ALL);
text.setMovementMethod(LinkMovementMethod.getInstance());
});

TextView inside an alert dialog won't scroll down

I am taking data from a website and placing inside of a text view so that I can center it using setGravity(Gravity.CENTER). However when i place the text view inside of the alert dialog I can no longer scroll down to see the end of my message. Can someone help me with this problem? Thank You
TextView msg1=new TextView(Welcome.this);
//Takes data from a website
msg1.setText(Html.fromHtml(getText("http://www.cellphonesolutions.net/welcome-en")));
msg1.setGravity(Gravity.CENTER);
LinearLayout dialogLayout = new LinearLayout(Welcome.this);
dialogLayout.addView(msg1);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
AlertDialog welcome = new AlertDialog.Builder(Welcome.this).create();
welcome.setView(dialogLayout);
welcome.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//rest of the code....
You can try changing the code like this:
dialogLayout.addView(msg1);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
AlertDialog welcome = new AlertDialog.Builder(Welcome.this).create();
ScrollView scrollPane = new ScrollView(this);
scrollPane.addView(dialogLayout);
welcome.setView(scrollPane);

Categories

Resources