Hyperlink in string resource in AlertDialog - android

I have a large string and I need to add 2 hyperlinks. I did it, but it`s not clickable.
String:
<string name="freeasa"><![CDATA[<b><font color=#cc0022>text<font color=#2266bb> text </font></b> <br> text <a href=\'http://google.com\'>navigate to google.com</a><br><b><font color=#2266bb> text</font><font color=#cc0022> text </font></b><br> text <a href=\'http://yahoo.com\'> yahoo link<\a> ]]></string>
AlertDialog:
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(getResources().getString(R.string.add_info));
builder.setMessage(Html.fromHtml(getResources().getString(R.string.freeasa)))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
There are hyperlinks, but they are not clickable. How to fix it ?

try custom alert dialog below way
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.prompts, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(promptsView);
final TextView userInput = (TextView) promptsView
.findViewById(R.id.textView);
userInput.setMovementMethod(LinkMovementMethod.getInstance());
userInput.setText(Html.fromHtml(getResources().getString(R.string.about_body)));
builder.setTitle(getResources().getString(R.string.app_name));
// builder.setMessage(Html.fromHtml(getResources().getString(R.string.about_body)))
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
prompts.xml
<?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"
android:padding="10dip">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
make sure your string is correct and links are working

Use this to set an heperlink to a TextView:
TextView website = new TextView(this);
website.setText("http://www.google.com");
Linkify.addLinks(website, Linkify.ALL);
Hope it helps

Related

Android - Alert Dialog with changeable image

I want to display an AlertDialog with an image. But the image may change depending on some circumstances.
This is the code I'm using for the AlertDialog:
AlertDialog.Builder alertadd = new AlertDialog.Builder(wheel.this);
LayoutInflater factory = LayoutInflater.from(wheel.this);
final View view = factory.inflate(R.layout.alert, null);
alertadd.setView(view);
alertadd.setTitle("Alert Dialog Title");
alertadd.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
And this is the alert.xml:
<?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">
<ImageView
android:id="#+id/dialog_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>
</LinearLayout>
Is there any way to programmatically access the imageView within the XML file so I can change the image? Or is there a better way to do that?
You can get a reference of ImageView by calling findViewById on your view and then call setImageResource on it :
LayoutInflater factory = LayoutInflater.from(wheel.this);
final View view = factory.inflate(R.layout.alert, null);
// change the ImageView image source
final ImageView dialogImageView = (ImageView) view.findViewById(R.id.dialog_imageview);
dialogImageView.setImageResource(R.drawable.your_image);
alertadd.setView(view);
alertadd.setTitle("Alert Dialog Title");
alertadd.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();

Android Studio Alertdialog show image full size

I want to the show Full size of the image in the alert dialog, When I click on the imagebutton in the android project. How can I do?
In xml file -
<?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">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/my_image"/>
</LinearLayout>
In activity show your custom dialog box using below code -
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogView)
.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
transaction to new fragment with a imageview in match parent.

AlertDialog with ImageView and TextView

I want to make an AlertDialog with ImageView and TextView.
I wrote this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="#+id/imgCustomToast"
android:layout_width="170dp"
android:layout_height="220dp"
android:background="#drawable/ycp"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/txtCustomToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C#"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:textSize="20sp"/>
</LinearLayout>
MainActivity:
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate
{
AlertDialog.Builder alertadd = new AlertDialog.Builder(this);
LayoutInflater factory = LayoutInflater.From(this);
View view = factory.Inflate(Resource.Layout.sample, null);
alertadd.SetView(view);
alertadd.SetPositiveButton("To Close", (senderAlert, args) =>
{
Toast.MakeText(this, "Closed", ToastLength.Short).Show();
});
alertadd.Show();
};
}
}
I want to change size, font and text of the textview with in MainActivity:
TextView textView = FindViewById<TextView>(Resource.Id.txtCustomToast);
string str = "sample text";
textView.Text = str;
Typeface typeP = Typeface.CreateFromAsset(this.Assets, "fonts/BLOTUS.TTF");
textView.SetTypeface(typeP, TypefaceStyle.Normal);
textView.SetTextSize(Android.Util.ComplexUnitType.Sp, 18);
But I see this error:
System.NullReferenceException
How can I change size, font and text of the textview programmatically?
You are doing wrong way you can access the TextView below way,
Change this line
TextView textView = FindViewById<TextView>(Resource.Id.txtCustomToast);
to this
TextView textView = view.FindViewById<TextView>(Resource.Id.txtCustomToast);
You need to find your view using instance of Alert Dialog like below.
TextView textView = view.FindViewById<TextView>(Resource.Id.txtCustomToast);
View view = factory.Inflate(Resource.Layout.sample, null);
TextView textView = view.findViewById<TextView>(Resource.Id.txtCustomToast);
string str = "sample text";
textView.setText(str);
Call this method,That display an AlertDialog with ImageView and TextView
private void showDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("Message");
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView imageView = new ImageView(context);
TextView textView = new TextView(context);
linearLayout.addView(imageView);
linearLayout.addView(textView);
builder.setCancelable(false);
builder.setView(linearLayout);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//ok
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// negative button logic
}
});
AlertDialog dialog = builder.create();
// display dialog
dialog.show();
}

How can can I add custom buttons into an AlertDialog's layout?

I have AlertDialog with Positive and Negative buttons. In AlertDialog layout I have EditText and two Buttons (btnAdd1, btnAdd2). I want when user click at the Button btnAdd1 or btnAdd2 add same text to EditText in AlertDialog (but no close AlertDialog). Is this possible do in AlertDialog or I have to use only Dialog?
This is layout (R.layout.prompt) of AlertDialog:
<LinearLayout>
<EditText
android:id="#+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnAdd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla" />
<Button
android:id="#+id/btnAdd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla" />
</LinearLayout>
And this is source code:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//...
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
I want get acces to the btnAdd1 and btnAdd2 from the layout. Set the OnClickListener() to these two buttons.
The following code will inflate a view from R.layout.prompt and set it to the AlertDialog. The positive and negative buttons will not be used. You can set the onClick behaviors for btnAdd1 and btnAdd2:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
EditText userInput = (EditText) promptView.findViewById(R.id.userInput);
Button btnAdd1 = (Button) promptView.findViewById(R.id.btnAdd1);
Button btnAdd2 = (Button) promptView.findViewById(R.id.btnAdd2);
btnAdd1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// btnAdd1 has been clicked
}
});
btnAdd2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// btnAdd2 has been clicked
}
});
alertD.setView(promptView);
alertD.show();
what you want to do is;
alertD.show();
Button button = (Button)promptView.findViewById(R.id.buttonId);
button.setOnClickListener(....)
using the view to call findViewById, rather than the activity, which will look for the id in the layout that is being displayed.
According to this approach i am able to create the image button but if i want to dismiss or cancel dialog on Cancel button then what i have to do..
public static void alertDialogShow(final Context context,
final String resultMobile) {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompt,
null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
userInput.setText(resultMobile);
userInput.setEnabled(false);
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
My solution for your question.
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
Button btn_1= (Button)promptView.findViewById(R.id.btnAdd1);
btn_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do required function
// don't forget to call alertD.dismiss()
}
});
Button btn_2 = (Button)promptView.findViewById(R.id.btnAdd2);
btn_2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do required function
}
});
alertDialogBuilder
.setCancelable(false)
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
This is the way I did.
custom_alert_dialog.xml file created
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text2"
app:layout_constraintTop_toBottomOf="#+id/text1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK"
app:layout_constraintTop_toBottomOf="#+id/text2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
In activity file
LayoutInflater layoutInflater = getLayoutInflater();
View alertLayout = layoutInflater.inflate(R.layout.custom_alert_dialog, null);
Button button = alertLayout.findViewById(R.id.button1);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setCancelable(false);
alertDialog.setView(alertLayout);
AlertDialog dialog = alertDialog.create();
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
You could try something like this :
dialog.setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.show();
}
});

Android: Alert Dialog Box crashing

I have an Alert Dialog Box that pops up on the click of a ListView item. The Alert Dialog has a custom layout containing two EditTexts and a TextView. However on calling EditText.getText() on the click of the OK button on the dialog, the application crashes with java.lang.NullPointerException. Please help me in debugging it.
The listview onClickListener code :
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
String cn = cursor.getString(cursor.getColumnIndex("CourseName"));
//Toast.makeText(getApplicationContext(), "Selected: "+cn, Toast.LENGTH_SHORT).show();
LayoutInflater lf = LayoutInflater.from(List_of_Courses.this);
final View DialogView = lf.inflate(R.layout.dialog, null);
final EditText input1 = (EditText) findViewById(R.id.attendanceet);
final EditText input2 = (EditText) findViewById(R.id.totalclasseset);
final AlertDialog.Builder alert = new AlertDialog.Builder(List_of_Courses.this);
alert.setTitle(cn).setView(DialogView).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichbutton) {
Log.v("Test","We're checking");
input1.getText();
input2.getText();
Log.v("Test","We're good");
Log.v("Dialog", input1.getText().toString());
Log.v("Dialog", input2.getText().toString());
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichbutton) {
//User clicked cancel so doing nothing.
Log.v("CancelDialog", "User clicked Calcel");
}
});
alert.show();
}
});
The dialog.xml code :
<?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="match_parent">
<LinearLayout
android:orientation="horizontal"
android:id="#+id/DialogLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="25sp"
android:paddingRight="25sp" >
<EditText
android:id="#+id/attendanceet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_weight="1"
android:inputType="number" >
</EditText>
<LinearLayout
android:orientation="vertical"
android:id="#+id/tvLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="6sp" >
<TextView
android:id="#+id/outof"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textSize="20sp"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:textColor="#FFFFFF" >
</TextView>
</LinearLayout>
<EditText
android:id="#+id/totalclasseset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_weight="1"
android:inputType="number" >
</EditText>
</LinearLayout>
</RelativeLayout>
You should use the dialog object to intialize input1 an input 2.
final View DialogView = lf.inflate(R.layout.dialog, null);
final EditText input1 = (EditText) DialogView.findViewById(R.id.attendanceet);
You can findViewById of the current view hierarchy set to the activity. In your case you inflate a dialog and you current view is the dialog on listview item click. So you should use the dialog object to initialize the views.
You can remove the final modiifier for the below
AlertDialog.Builder alert = new AlertDialog.Builder(List_of_Courses.this);
final EditText input1 = (EditText) findViewById(R.id.attendanceet);
final EditText input2 = (EditText) findViewById(R.id.totalclasseset);
should be
final EditText input1 = (EditText) DialogView.findViewById(R.id.attendanceet);
final EditText input2 = (EditText) DialogView.findViewById(R.id.totalclasseset);
You have to create an AlertDialog before calling show() method. And call show() method on created AlertDialog not on AlertDialogBuilder.
// create alert dialog
AlertDialog alertDialog = alert.create();
Look at this example for reference.

Categories

Resources