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());
});
Related
I just want to know if it is possible to create another edit text below my existing one and how to create it. The new one should also have a subtitle above the text line so users can differentiate which one is which. Thanks and have a great day.
private void showAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
alertDialog.setTitle("Requests:");
alertDialog.setMessage("(Condiments,napkins, take-out orders or such can also be requested here)");
final EditText edtAddress = new EditText(Cart.this);
final EditText edtRef = new EditText(Cart.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
edtAddress.setLayoutParams(lp);
alertDialog.setView(edtAddress);// Request Edit Txt
alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);
alertDialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Create new Request
Request request = new Request(
Common.currentTable.getTablet(),
edtAddress.getText().toString(),
txtTotalPrice.getText().toString(),
cart
);
// Submit to Firebase
requests.child(String.valueOf(System.currentTimeMillis()))
.setValue(request);
//Delete cart
new Database(getBaseContext()).cleanCart();
Toast.makeText(Cart.this, "Order Placed Thank You and Kindly Wait for your Order.", Toast.LENGTH_LONG).show();
finish();
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
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!
I try to get dynamically an identifier of some checkbox in a AlertDialog.
But I do not find the right code for this.
Every time I get return 0.
I have a test to get id of alert title ("android:id/alertTitle")
And this works fine. But I'am not able to reach my inflater-ID's
For me it seems like the context is wrong?
AlertDialog.Builder builder = new AlertDialog.Builder(Claim.this);
builder.setView(optionmenu);
final CheckBox[] myCheckBox = new CheckBox[checkedItems.length];
builder.setTitle("Select pages for claim");
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
for(int i=0; i<checkedItems.length; i++) {
String checkboxID = "android:id/optionspage"+i+"CheckBox";
int resID = builder.getContext().getResources().getIdentifier(checkboxID, null, null);
myCheckBox[i] = ((CheckBox) findViewById(resID));
myCheckBox[i].setChecked(checkedItems[i]);
}
What am I doing wrong here? Thanks.
Code
Button mButton1;
String mDefaultFont1;
SharedPreferences mSharedPreferences1;
SharedPreferences.Editor editor1;
mButton1 = (Button)findViewById(R.id.buttontextfontsent);
mSharedPreferences1 = PreferenceManager.getDefaultSharedPreferences(this);
mDefaultFont1 = mSharedPreferences1.getString("Default_Font1","Normal");
mButton1.setTypeface(mButton1.getTypeface(),Typeface.NORMAL);
mButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String[] list = new String[]{"Normal", "Bold", "Italic", "Bold ITalic"};
AlertDialog.Builder builder = new AlertDialog.Builder(CustomizeFont.this);
builder.setTitle("Make your selection");
builder.setItems(list, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item==0) mButton1.setTypeface(mButton1.getTypeface(),Typeface.NORMAL);
if (item==1) mButton1.setTypeface(mButton1.getTypeface(),Typeface.BOLD);
if (item==2) mButton1.setTypeface(mButton1.getTypeface(),Typeface.ITALIC);
if (item==3) mButton1.setTypeface(mButton1.getTypeface(),Typeface.BOLD_ITALIC);
editor1 = PreferenceManager.getDefaultSharedPreferences(CustomizeFont.this).edit();
editor1.putString("Default_Font1", String.valueOf(item));
editor1.apply();
}
});
builder.show();
}
});
I can change the font but when i restart the activity it goes back to the original normal font... the problem is because of this line mButton1.setTypeface(mButton1.getTypeface(),Typeface.NORMAL);
I need to set the TypeFace.NORMAL to Default_Font1 But not accepting... what should I do?
I need to set the textview to Default_Font1 But not accepting... what should I do?
You need to setTypeface in your button based on you values getting from SharedPreferences
SAMPLE CODE
mDefaultFont1 = mSharedPreferences1.getString("Default_Font1","0");
if (mDefaultFont1.equals("0")) {
mButton1.setTypeface(mButton1.getTypeface(), Typeface.NORMAL);
} else if (mDefaultFont1.equals("1")) {
mButton1.setTypeface(mButton1.getTypeface(), Typeface.BOLD);
} else if (mDefaultFont1.equals("2")) {
mButton1.setTypeface(mButton1.getTypeface(), Typeface.ITALIC);
} else if (mDefaultFont1.equals("3")) {
mButton1.setTypeface(mButton1.getTypeface(), Typeface.BOLD_ITALIC);
}
mButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String[] list = new String[]{"Normal", "Bold", "Italic", "Bold ITalic"};
AlertDialog.Builder builder = new AlertDialog.Builder(CustomizeFont.this);
builder.setTitle("Make your selection");
builder.setItems(list, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item==0) mButton1.setTypeface(null);
if (item==1) mButton1.setTypeface(mButton1.getTypeface(),Typeface.BOLD);
if (item==2) mButton1.setTypeface(mButton1.getTypeface(),Typeface.ITALIC);
if (item==3) mButton1.setTypeface(mButton1.getTypeface(),Typeface.BOLD_ITALIC);
editor1 = PreferenceManager.getDefaultSharedPreferences(CustomizeFont.this).edit();
editor1.putString("Default_Font1", String.valueOf(item));
editor1.apply();
}
});
builder.show();
}
});
try this .
editor.putInt("fontCode",mButton1.getTypeFace().getStyle());
and then use it as normal shared prefrence
mDefaultFont1 = mSharedPreferences1.getString("Default_Font1","Normal");
mButton1.setTypeface(mButton1.getTypeface(),Typeface.NORMAL);
This logic is completely wrong, keep in mind that you're saving int values in preferences (0, 1, 2, 3), but first you load it as if it was string with default value "normal".
In the second line you're setting the typeface for the button, but taking current typeface from same button, so what you get? No change.
Instead of that, I suggest something like this:
mDefaultFont1 = mSharedPreferences1.getInt("Default_Font1",0);
mButton1.setTypeface(mDefaultFont1);
And save the preference like this:
public void onClick(DialogInterface dialog, int item) {
int defaultTypeface = 0;
if (item==0) defaultTypeface = Typeface.NORMAL;
if (item==1) defaultTypeface = Typeface.BOLD;
if (item==2) defaultTypeface = Typeface.ITALIC;
if (item==3) defaultTypeface = Typeface.BOLD_ITALIC;
mButton1.setTypeface(defaultTypeface);
editor1 = PreferenceManager.getDefaultSharedPreferences(CustomizeFont.this).edit();
editor1.putInt("Default_Font1", defaultTypeface);
editor1.apply();
}
Note, my code might be bit wrong, since I did write it from memory and didn't check if it's really fine.
I have this code for showing list of languages for download:
public void onCreateDialog(ArrayList<String>fullLangArray, final ArrayList<String>codeLangArray) {
final String[] items = fullLangArray.toArray(new String[fullLangArray.size()]);
final ArrayList mSelectedItems = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// Set the dialog title
builder.setTitle("Updates...")
.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
if (isChecked) {
mSelectedItems.add(Utils.SERVER_ADDRESS + "/" + codeLangArray.get(indexSelected) + ".zip");
} else if (mSelectedItems.contains(indexSelected)) {
mSelectedItems.remove(Integer.valueOf(indexSelected));
}
}
})
.setPositiveButton("Download", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
DownloadTask downloadTask = new DownloadTask(MainActivity.this);
downloadTask.execute(mSelectedItems.toString());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
I want to make a one item of a checkbox is checked and "disabled" like in a photo (Option 3) when AlertDialog is loaded.
Can you help me how to do it?
You can check the checkbox by using the setChecked() method which boolean value as parameter.
Example:
option1.setChecked(true);
and also uncheck it using
option2.setChecked(false);
If you want to set it to checked and disabled you have use setEnabled() which takes boolean as it's parameters.
Example.
option3.setChecked(true);
option3.setEnabled(false);
This will disable your checkbox and even check it. I hope this was helpful. ThankYou.
For setting Opacity
mSelectedItems.getBackground().setAlpha(128);
Where the INT ranges from 0 (fully transparent) to 255 (fully opaque).
For setChecked item
mSelectedItems.setChecked(true);
Disable checking
mSelectedItems.setEnabled(false)