I've read a lot regarding this issue, but I don't get to get my case working.
I have defined a TextView element with an initial text in the XML layout file.
If I try to modify it from the main activity class (FragmentActivity) by doing next:
TextView domainText = (TextView) view.findViewById(R.id.domainText);
domainText.setText("test");
it works perfectly.
But if I try to modify this very TextView element from a DialogFragment launched from my main activty class (FragmentActivity):
public class QueryDomainDialogFragment extends DialogFragment{
...
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the dialog title
builder.setTitle(R.string.queryTypeDialogTitle)
.setSingleChoiceItems(R.array.queryDomains, 0, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,int which){
ListView lw = ((AlertDialog)dialog).getListView();
Object selectedDomain = lw.getAdapter().getItem(lw.getCheckedItemPosition());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.activity_search, null);
TextView domainText = (TextView) view.findViewById(R.id.domainText);
domainText.setText("test2");
}
})
nothing happens.
How should I do it? Am I doing it the wrong way? Isn't possible to modify elements from a DialogFragment? Should I implement a sort of callback?
I would like to know why isn't possible to modify elements such as TextView from a DialogFragment, if that's the case.
Thanks in advance.
By doing inflate you are not refering to the same text view any more, you need to set the exact same view, not a new inflated one..
You cannot refere to the textview from the dialog as it's not a part of it's layout..
View view = inflater.inflate(R.layout.activity_search, null);
TextView domainText = (TextView) view.findViewById(R.id.domainText);
You need to create a listener, make your activity implement this listener, and pass it to the dialog..
#Override
public void onClick(DialogInterface dialog,int which){
listener.setText("test2");
}
Related
I use this code to create a custom AlertDialog:
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
The problem is I cannot get the inflated view. dialog.findViewById(R.id.a_view_in_the_layout) returns null.
Alternatively, I can use .setView(View.inflate(context, R.layout.layout, null) but this sometimes makes the dialog fill the screen and take more space than setView(int layoutResId).
If I remember correctly, create sets up the Dialog, but its layout is not inflated until it needs to be shown. Try calling show first then, then finding the view you're looking for.
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
dialog.show() // Cause internal layout to inflate views
dialog.findViewById(...)
Just inflate the layout yourself (its Java code but I think you know what to do):
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.layout, null );
dialog.setView(view);
dialog.create().show();
Your inflated view is now view and you can use it to find other views in it like:
EditText editText = view.findViewById(R.id.myEdittext);
Instead of using alert dialog use simple Dialog its Easy and very simple
final Dialog dialog = new Dialog(context);
dialog.setContentView((R.layout.layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView tvTitle = (TextView) dialog.findViewById(R.id.tvTitle);
tvTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
You don't have to need to inflate the View.
Try this;
View dialogView; //define this as a gobal field
dialogView = LayoutInflater.from(context).inflate(R.layout.your_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setView(dialogView);
View yourView = dialogView.findViewById(R.id.a_view_in_the_layout);
TextView yourTextView = dialogView.findViewById(R.id.a_textView_in_the_layout);
Button yourButton = dialogView.findViewById(R.id.a_button_in_the_layout);
I'm trying to use onClickListener but I keep getting Null exceptions.
This is the call to onClickListener -
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// code here
}
}
};
button1 = findViewById(R.id.button1);
button1.setOnClickListener(onClickListener);
'button1' is found on a different layout which opens (as a Dialog) when the user press on some other button.
What can I do to in this case?
Thanks! :)
You would have to inflate the layout with the dialogue's buttons.
for example, this is my method for displaying a popup.
I have named the method showPopup;
Dialogue ViewSview_student_dialogue;
view_student_dialogue = new Dialogue(getApplicationContext());
private void showStudentPopup(String name,String id_gender) {
TextView cancelpopup,st_name,id_and_gender;
view_student_dialogue.setContentView(R.layout.custom_student_popup);
cancelpopup = (TextView)view_student_dialogue.findViewById(R.id.cancel);
st_name = (TextView)view_student_dialogue.findViewById(R.id.st_name);
id_and_gender = (TextView)view_student_dialogue.findViewById(R.id.id_and_gender);
st_name.setText(name);
id_and_gender.setText(id_gender);
cancelpopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view_student_dialogue.dismiss();
}
});
view_student_dialogue.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));
view_student_dialogue.show();
}
Please take note that custom_student_popup is a layout file that i have created in the res layout folder.
Use layout inflator to inflate that view first then set listener.
The idea here is to get a reference to the view for your Dialog.
First, you inflate the xml layout for your Dialog:
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_layout, null);
You then set that as the view for your new Dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(dialogLayout);
You can then get a reference to the Button from the inflated View and set you OnClickListener:
Button yourButton = (Button)dialogLayout.findViewById(R.id.yourbutton);
/// do the click listener assignment
Show your Dialog to the user:
builder.show();
i am trying to display a pop up when i click on an item view.
when the pop up is open the buttons doesn't work.
here is my onItemClick function :
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
this.id = position;
alert.setTitle("Alert");
alert.setMessage("Souhaitez-vous modifier ou supprimer?");
alert.setView(R.layout.alert_view);
LayoutInflater inflater= LayoutInflater.from(this);
layoutAlert=(LinearLayout)inflater.inflate(R.layout.alert_view,null);
btn_supp = (Button)layoutAlert.findViewById(R.id.btn_supp_alert);
btn_modif = (Button)layoutAlert.findViewById(R.id.btn_modifier_alert);
ed_nom = (EditText)layoutAlert.findViewById(R.id.ed_nom_alert);
ed_prenom = (EditText)layoutAlert.findViewById(R.id.ed_prenom_alert);
ed_tel = (EditText)layoutAlert.findViewById(R.id.ed_tel_alert);
System.out.println(Principal.mesContacts.get(position).getNom().toString());
ed_nom.setText(Principal.mesContacts.get(position).getNom().toString());
btn_supp.setOnClickListener(this);
btn_modif.setOnClickListener(this);
alert.show();
}
and this is my onClick function:
public void onClick(View v) {
if(v==btn_supp)
{
Principal.mesContacts.remove(id);
listView.invalidateViews();
}
if(v==btn_modif)
{
Principal.mesContacts.set(id,new Contact(ed_nom.getText().toString(),ed_prenom.getText().toString(),ed_tel
.getText().toString()));
listView.invalidateViews();
}
}
but that doesn't work, I don't know why
Okay what you basically did is the following:
alert.setView(R.layout.alert_view);:
With this line of code, you instruct the AlertDialog.Builder class to inflate a new layout from the specified resource R.layout.alert_view and set it as the layout of the AlertDialog.
With the following lines of code you inflated a new layout of the same resource.
For this instance you set the OnClickListener. But your constructed layout never got set to the AlertDialog.Builder object.
So the AlertDialog.Builder wasn't aware of your layout and used the layout specified by alert.setView(R.layout.alert_view);. Does that make it clear to you?
I want to set the text in a TextView contained in a custom dialog programmatically, so that I can use Html.fromHtml. In what function should I call setText? I've tried doing it in onCreateDialog, but this does not actually change the text.
public class InfoDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.infodialog, null));
TextView textView = (TextView) getActivity().findViewById(R.id.info);
textView.setText(Html.fromHtml("<h1>Text has been correctly set</h1>"));
...
Try this:
View content = inflater.inflate(R.layout.infodialog, null);
builder.setView(content);
TextView textView = (TextView) content.findViewById(R.id.info);
I am trying to add custom views(chekbox and two radio buttons) as shown in image below in alertdialog but not succeded.
Please suggest me a way to get views as shown in the image.
Thanks in advance!!
Use DialogFragment instead of AlertDialog.
public class CustomDialogFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View viewRoot = inflater.inflate(R.layout.dialog_view, null);
//do something with your view
builder.setView(viewRoot);
return builder.create();
}
}
R.layout.dialog_view - it's your view, which you want to display
You can build dialog with custom layout. Here's some tutorial how to do that:
http://www.mkyong.com/android/android-custom-dialog-example/