Android hyperlinks on TextView in custom AlertDialog not clickable - android

Following my previous question I still have problems managing hyperlinks on my custom AlertDialog but I believe I narrowed down the problem because I think it only not works because it's a custom dialog.
I have followed all the instructions here and here but can't seem to bypass this situation
On strings.xml I have a string defined this way:
<string name="link_text_manual"><b>text2:</b> This is some other
text, with a link specified
via an <a> tag. Use a \"tel:\" URL
to dial a phone number.
</string>
If I create my dialog this way:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Data")
.setIcon(R.drawable.info)
.setMessage(R.string.link_text_manual)
.setCancelable(true)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog welcomeAlert = builder.create();
welcomeAlert.show();
// Make the textview clickable. Must be called after show()
((TextView)welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
it shows like this:
This works as expected and all clicks are clickable.
Now I want the same thing but on a custom layout.
Created a TextView on my info.xml like this:
<TextView
android:id="#+id/infoDetailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/infoVersionTitle"
android:autoLink="all"
android:linksClickable="true"
android:padding="4dp" />
And this is the code:
Context ctx = this;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.info, (ViewGroup) findViewById(R.id.infoLayout));
AlertDialog.Builder about = new AlertDialog.Builder(ctx);
about.setView(layout);
about.setTitle("Data");
about.setIcon(R.drawable.info);
AlertDialog displayInfo = about.create();
displayInfo.show();
// Make the textview clickable. Must be called after show()
TextView textView = (TextView) displayInfo.findViewById(R.id.infoDetailText);
textView.setText(R.string.link_text_manual);
textView.setMovementMethod(LinkMovementMethod.getInstance());
With the above code it shows like this (links not clickable and even not marked as links):
If I remove
textView.setMovementMethod(LinkMovementMethod.getInstance());
it shows like the bellow image (Links marked as links but not clickable):
I've also tried to use Linkify and android:autoLink="web" but the result is always the same.
setMessage() works but with a custom layout with a TextView can't make it to work.
Probably this is simple but can't seem to make it work. Any suggestions?
Thanks

You defined the TextView without android:autoLink="all" , android:clickable="true" & android:linksClickable="false"
For info.xml TextView as below
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/infoDetailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp" />

Anyway, your code works perfectly,...
Code for AlertDialog:
LayoutInflater inflater = LayoutInflater.from(ctx);
View layout = inflater.inflate(R.layout.info, null);
AlertDialog.Builder about = new AlertDialog.Builder(ctx);
about.setTitle("Data");
about.setIcon(R.drawable.info);
// Make the textview clickable. Must be called after show()
TextView textView = (TextView) layout.findViewById(R.id.infoDetailText);
textView.setText(R.string.link_text_manual);
textView.setMovementMethod(LinkMovementMethod.getInstance());
about.setView(layout);
AlertDialog displayInfo = about.create();
displayInfo.show();
Code of TextView XML:
<TextView
android:id="#+id/infoDetailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/infoVersionTitle"
android:padding="4dp" />
Removed two attributes..
android:autoLink="all"
android:linksClickable="true"

Try this
setText(Html.fromHtml(String));

Related

Views not found for Alert Dialog in fragments

I am trying to populate an Alert Dialog (with a layout file having an EditText & Button) within a fragment of Activity.
The components are not being found by calling findViewById() in the fragment's corresponding class. I have written this code inside the class :
AlertDialog.Builder builder=new AlertDialog.Builder(context);
View v=getLayoutInflater().inflate(R.layout.layout_alert_dialog,null);
final EditText txtAddNew=v.findViewById(R.id.txtAddNew); //null
final TextView txtErr = v.findViewById(R.id.txtErr); //null
Button btnAdd=v.findViewById(R.id.btnAdd); //null
I think the problem raises from this line of code:
View v=getLayoutInflater().inflate(R.layout.layout_alert_dialog,null);
The alertDialog XML code is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/txtAddNew"
android:hint="Category name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtErr"
/>
<Button
android:id="#+id/btnAdd"
android:text="Add Category" />
</LinearLayout>
Can anyone find what the actual problem is? Thanks!
Yes, that's correct
Do something like this :
LayoutInflater layout = LayoutInflater.from(this);
final View view = layout.inflate(R.layout.dialog_layout, null);
final AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(view);
dialog.show();
You can do it like below.
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
alertBuilder.setView(getLayoutInflater().inflate(R.layout.layout_alert_dialog,null));
AlertDialog alertDialog = alertBuilder.create();
EditText txtAddNew = alertDialog.findViewById(R.id.txtAddNew);
TextView txtErr = alertDialog.findViewById(R.id.txtErr);
Button btnAdd = alertDialog.findViewById(R.id.btnAdd);
alertDialog.show();
Try using dialog.setContentView(v)
See the difference between setView(v) and setContentView(v) here:
What is difference between Dialog.setContentView( View ) & AlertDialog.setView( View )

how to customize Dialog header in android studio?

I'm using a custom dialog that I've created in a separate xml file in my project, and I'm coloring the main window a blueish tint,
but the main header still remains the default white color.
Is there no way to change the font color, size, background for the header?
Is the only thing I can change in the header the text?
xml:
<?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"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#3edfbc"
android:orientation="vertical">
<TextView
android:id="#+id/textaligmentManager_loader_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="Initlizing Wifi"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.github.ybq.android.spinkit.SpinKitView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/barcodeScanning_spinkit"
style="#style/SpinKitView.Large.FoldingCube"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="#+id/textaligmentManager_loader_textview"
android:padding="20dp"
android:layout_centerHorizontal="true"
app:SpinKit_Color="#ffffff" />
</RelativeLayout>
dialog:
Dialog dialog = new Dialog(Scanning_Barcode_Activity.this);
dialog.setContentView(R.layout.aligment_manager_loader_layout);
dialog.setTitle("Loading");
dialog.setCancelable(false);
//set up text
loaderScreenMainText = (TextView) dialog.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
//progressBar = (ProgressBar) dialog.findViewById(R.id.barcodeScanning_spinkit);
//DoubleBounce doubleBounce = new DoubleBounce();
//progressBar.setIndeterminateDrawable(doubleBounce);
//now that the dialog is set up, it's time to show it
dialog.show();
You can use AlertDialog which is a subclass of Dialog class. Here you can define a custom layout containing everything such as title, body and buttons. No extra title section will appear. Here is a demo:
AlertDialog.Builder builder = new AlertDialog.Builder(Scanning_Barcode_Activity.this);
builder.setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(Scanning_Barcode_Activity.this);
View dialogView = inflater.inflate(R.layout.aligment_manager_loader_layout, null);
TextView loaderScreenMainText = (TextView) dialogView.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
builder.setView(dialogView);
AlertDialog dialog = builder.create();
dialog.show();
Design your custom layout in such a way that the layout itself contains the header part. And then skip this code:
dialog.setTitle("Loading");
Instead add this statement:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
You can also use AlertDialog. In that case requestWindowFeature() method is not required.
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(Scanning_Barcode_Activity.this);
LayoutInflater inflater = LayoutInflater.from(Scanning_Barcode_Activity.this);
View customView = inflater.inflate(your_layout, null);
builder.setCancelable(false);
loaderScreenMainText = (TextView) customView.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
builder.setView(customView);
AlertDialog dialog = builder.create();
dialog.show();
There are plenty of stylish open source libraries that you can use for customizing header parts.
Here I'm sharing my newly developed open source library : PanterDialog
Hope it will help you.

Android TextView Visibility in AlertDialog does not change

I am using an AlertDialog created using android.suport.v7.app.AlertDialog and I am having a custom layout for the Dialog with a few EditTexts and a few TextViews.
The TextViews that are used are initially having their visibility set to GONE.
I am using the TextViews mainly to prompt the user for incorrect inputs in the EditText fields.
So when I try to toggle the visibility of the TextView to VISIBLE for showing an error message, the visibility does not change.
My XML File for layoutS is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/contactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="#string/sampark_naam"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp">
<requestFocus />
</EditText>
<TextView
android:id="#+id/noName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/no_name_error"
android:textColor="#android:color/holo_red_dark"
android:layout_marginEnd="5dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="5dp"
android:visibility="gone"/>
</LinearLayout>
The AlertDialog used is:
final Context context = view.getContext();
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View aDrishya = layoutInflater.inflate(R.layout.layoutS, null, false);
vNaam = (EditText) aDrishya.findViewById(R.id.contactName);
nameError = (TextView) aDrishya.findViewById(R.id.noName);
sSanddok = new AlertDialog.Builder(context)
.setView(aDrishya)
.setTitle("Add A New Contact")
.setPositiveButton("Add +", new PositiveButton(context))
.setNegativeButton("Pick", new null);
sSanddok.show();
In the positive button I check for the contactName entered as:
if(vNaam.getText().toString().equals("")) {
Toast.makeText(this, "No Name", Toast.LENGTH_SHORT).show();
nameError.setVisibility(View.VISIBLE);
} else {
nameError.setVisibility(View.GONE);
}
It is showing the Toast but it's not showing the TextView.
Any idea what's wrong here and how to fix it?
Hi can you try to change and run this code, See if it works..
Alert dialog changes :
sSanddok = new AlertDialog.Builder(context)
.setView(R.layout.layoutS)
.setTitle("Add A New Contact")
.setPositiveButton("Add +", null)
.setNegativeButton("Pick",null);
sSanddok.show();
sSanddok.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//if edit text value is empty do this
TextView tv = (TextView)sSanddok.findViewById(R.id.noName);
tv.setVisibility(View.VISIBLE);
}
});
TextView visibility in xml is set to gone..
Hi this is working for me...Try this code pls

Android AlertDialog produces black text with black background

I am having an issue with a custom view in a dialog on android API 10.
I use AlertDialog.Builder to construct my dialog. I include a custom view panel using the setView command on the builder.
This works on most of the API's I've tested with. The style changes somewhat from device to device, but that is what I want, for the style to match the device default.
My problem is that on API 10, any text that is in my custom view shows up as black on a black background.
Any text I insert using AlertDialog.Builder.setMessage() appears correctly.
What magical attribute/style is the dialog builder using to determine text appearance?
My app's theme is Theme.AppCompat.Light.
Here is my onCreateDialog method:
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.fragment_status_dialog, null);
mStatusTextView = (TextView) view.findViewById(R.id.text_status);
mConnectedDeviceTextView = (TextView) view.findViewById(R.id.text_connected_device);
MainService.ServiceState state = null;
if (getArguments().containsKey(SERVICE_STATUS_ARG_KEY)) {
state = (MainService.ServiceState) getArguments().getSerializable(SERVICE_STATUS_ARG_KEY);
}
setState(state);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setMessage("This will show up just fine.");
builder.setTitle(getString(R.string.status_title));
builder.setNegativeButton(R.string.dialog_back_button_text, null);
builder.setNeutralButton(R.string.dialog_connect_to_text, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mListener.onDialogConnectTo();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
Here's my fragment_status_dialog layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="18dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/status_starting"
android:id="#+id/text_status"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/status_connected_to_unknown"
android:paddingStart="4dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:id="#+id/text_connected_device"
android:layout_toRightOf="#+id/text_status"
android:layout_toEndOf="#+id/text_status"/>
</RelativeLayout>
Note, I've tried https://stackoverflow.com/a/24505312/2350083 but it didn't fix it.
Try calling AlertDialog#setInverseBackgroundForced(true).
What about simply setting the color of the text? Ex:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:text="#string/status_starting"
android:id="#+id/text_status"/>
The TextView is using the default text color of the device (or the app). If you set the color specifically to the TextView it will be overriden on devices irrespective of the API.

Trying to Read Edit box inside Custom Dialog... Getting FC

I am trying to read a EditText box and send its contents to another method from within a Custom dialog box. My current code causes a Force Close. The logcat is very vague... however I know the uncaught exception takes place in this method:
public void getName(){
Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("New Game");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
final EditText inputBox = new EditText(this);
//set up text
final TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Enter Your Name...");
//set up button
final Button button = (Button) dialog.findViewById(R.id.namebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = inputBox.getText().toString();
setName(str);
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
Here is the custom XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/linearLayout1">
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/nameprompt"></TextView>
<EditText android:layout_width="fill_parent" android:id="#+id/editText1" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Player">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="wrap_content" android:id="#+id/namebutton" android:text="Ok" android:layout_width="fill_parent"></Button>
</LinearLayout>
</RelativeLayout>
Any ideas???
You have the EditText in your XML for the Dialog's layout... and you're properly using findViewById() to instantiate your TextView...
You need to do the same for the EditText, also use findViewById() to instantiate it.
final EditText inputBox = (EditText) dialog.findViewById(R.id.editText1);
Something is up with the inputBox object. You create it in this method, but I don't see you actually adding to the layout anywhere. When this method completes, you'll be displaying the dialog box, but the input box won't be displayed anywhere. In fact, I think that inputBox might be garbage collected since there are not references to if around after the getName() method completes. Therefore when you call get input on it, it might be null.
I think what you meant to do was this:
final EditText inputBox = (EditText)dialog.findViewById(R.id.editText1)

Categories

Resources