Find a View of an AlertDialog - android

I want to get a View from an AlertDialog:
Button R = (Button) findViewById(R.id.toggleButtonA);
But if I do this, it always is null. What can I do to get and edit the view?
Here are the relevant parts of code:
What creates the Dialog:
public void openSelection(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialayout, null));
AlertDialog dialog = builder.create();
dialog.show();
}
The dialayout.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/toggleButtonA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:onClick="select"
android:text="All"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toggleButtonM" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>

You need to assign your inflated view to a variable first. Example:
public void openSelection(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.activity_main, null); // this line
builder.setView(v);
AlertDialog dialog = builder.create();
dialog.show();
Button RR = (Button) v.findViewById(R.id.toggleButtonA); // this is how you get a view of the button
}
Please reneame your button variable name R to another name perhaps because R is a reserved word.

Inflate the parent layout first,
LayoutInflater inflater = this.getLayoutInflater();
View parent = inflater.inflate(R.layout.activity_main, null);
then set your builder to that parent view.builder.setView(parent);
you can then perform any view finding with that parent inflated view. Or you can call AlertDialog diag = builder.create(); and perform a find with the diag.findViewById(R.id.name);
so Button btR = (Button) diag.findViewById(R.id.toggleButtonA);
or Button btR = (Button)parent.findViewById(R.id.toggleButtonA);

Related

Change the positive button text of an alert dialog after the dialog is shown

I have an alert dialog with a spinner in it. I like to change the text of the positive button depending on the spinner position.
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if (position == 0) {
/////
// I have access to the dialog object here and need to change the positive button text
////
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Here's how i did it.
Declared buttons My Class
MaterialButton positiveBtn,negativeBtn;
Created my function and initialized my buttons
void alertDialog(){
AlertDialog.Builder dialogBuilder = new
AlertDialog.Builder(requireActivity());
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.test_layout, null);
dialogBuilder.setView(dialogView);
positiveBtn = dialogView.findViewById(R.id.btn_positive);
negativeBtn = dialogView.findViewById(R.id.btn_negative);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
negativeBtn.setOnClickListener(v -> {
positiveBtn.setText("Text 2 Positive ");
});
}
Here is the test_layout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_positive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text 1 Positive "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">
</com.google.android.material.button.MaterialButton>
<com.google.android.material.button.MaterialButton
app:layout_constraintTop_toBottomOf="#id/btn_positive"
android:id="#+id/btn_negative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</com.google.android.material.button.MaterialButton>
</androidx.constraintlayout.widget.ConstraintLayout>
In your scenario you already have the custom layout for Alert Dialog just declare variables , intialize and set Text inside the spinner index wise.
this answer was inspired from here

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 )

Inflate image on top of a popupview object

Currently for inflating images (zoom in animation) we use this library.
while on top of regular activities it looks fine (Simple layout), on top of a popupview the image is "behind" the popup.
Tried to change the elevation parameters, but no luck there either.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="2dp"
tools:context="com.MapActivity">
<com.vatsal.imagezoomer.ImageZoomButton
android:id="#+id/bt_picture"
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_alignTop="#id/tx_description"
android:layout_marginTop="1dp"
android:layout_alignLeft="#+id/bt_date_time"
android:layout_marginLeft="-25dp"
android:elevation="10dp"
android:background="#drawable/ic_picture" />
</RelativeLayout>
And this is how the popupview and windows are inflated:
popupView = getLayoutInflater().inflate(R.layout.job, null);
popupWindow = new PopupWindow(popupView,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
Should we try and use a different approach? Mabye ditch the library and inflate it differently?
Thanks
We've followed mmdreza's advice, here's a snippet of the implementation.
As the documantation states, the alertdialog will be on top of the popupview object.
myHolder.jobImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());
LayoutInflater inflater =
(LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = inflater.inflate(R.layout.inflated_picture_dialog, null);
Picasso.get().load(jobView.getJobImageURL()).fit().into(((ImageButton)mView.findViewById(R.id.inflated_picture)));
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
}
});

ItemizedOverlay issue in google map

ive been trying to pop up a custom dialog box when tapping on a marker of the google map. I use this code to override onTap() method of itemizedOverlay calss.
protected boolean onTap(int index) {
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView textName = (TextView) layout.findViewById(R.id.text);
textName.setText("Here is the Name");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
return super.onTap(index);
}
Here is the custom dialog layout xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
But i get this exception when tapping on a marker
02-07 16:46:51.768: E/AndroidRuntime(315): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
this is the exact example given on http://developer.android.com/guide/topics/ui/dialogs.html
any help please...
Thanks.
Use classname.this instead of getApplicationContext
if its a simple mapactivity
if its in tab then use getParent()
because badtokenexception occur when its not take right class reference.
Change
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
to
LayoutInflater inflater = (LayoutInflater) MyActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);

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.

Categories

Resources