I have a cutstom DialogFragment to show a message to the user:
public class MensajeDialogFragment extends DialogFragment {
TextView mTvMensaje;
TextView mTvTitulo;
Button mBtnAceptar;
Button mBtnCancelar;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.layout_mensaje_dialog, null);
mTvTitulo = (TextView) dialogView.findViewById(R.id.tvTitulo);
mTvMensaje = (TextView) dialogView.findViewById(R.id.tvMensaje);
mBtnAceptar = (Button) dialogView.findViewById(R.id.btnAceptar);
mBtnCancelar = (Button) dialogView.findViewById(R.id.btnCancelar);
mTvTitulo.setText(getArguments().getString(getString(R.string.bundle_titulo), ""));
mTvMensaje.setText(getArguments().getString(getString(R.string.bundle_mensaje), ""));
mBtnAceptar.setText(getArguments().getString(getString(R.string.bundle_aceptar), ""));
mBtnCancelar.setText(getArguments().getString(getString(R.string.bundle_cancelar), ""));
builder.setView(dialogView);
getDialog().setCanceledOnTouchOutside(true);
return builder.create();
}
}
But when I reach the getDialog().setCanceledOnTouchOutside(true), I am getting NullPointerException beacuse the getDialog() is returning null.
What am I doing wrong? I want to close the dialog when the user clicks outside of it.
Since you are using DialogFragment, dialog won't be initialized on onCreate of DialogFragment.
I believe DialogFragment by default closes when user clicks outside it.You don't have to declare it explicitly. If you still want to call this function, then DialogFragment has a function called
DialogFragment.setCancelable(boolean)
EDIT
If the above code doesn't work, you can try calling
getDialog().setCanceledOnTouchOutside(true);
inside onCreateView as Dialog object will on be initialized on getLayoutInflater during onCreateDialog. So by the time it reaches onCreateView dialog object will be initialized.
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 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'm creating a Dialog Fragment with a custom layout which includes a Number Picker.
To do so, I've created a DialogFragment class which implements NumberPicker.onValueChangeListener and a layout xml file that it will use.
I'm having an issue associating the Number Picker in the layout with a variable in the fragment class because 'findViewById' "Method cannot be resolved"
How can I get around this problem?
Elements of code below:
Dialog Fragment:
public class PlayersDialogueFragment extends DialogFragment implements NumberPicker.OnValueChangeListener {
NumberPicker numberOfPlayersPicker = null;
#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();
builder.setView(inflater.inflate(R.layout.players_fragment_layout, null));
numberOfPlayersPicker = (NumberPicker) findViewById(R.id.numberOfPlayersPicker);
numberOfPlayersPicker.setMaxValue(4);
numberOfPlayersPicker.setMinValue(2);
Layout - "players_fragment_layout":
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/numberOfPlayersPicker"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
I can put getActivity() before findViewById and can run the app, but it gives a null object reference error by doing so.
PS: If it matters, the dialog fragment is being called by Main Activity upon button press.
Assign inflater.inflate(R.layout.players_fragment_layout, null) to a variable v, and then call v.findViewById like so:
public class PlayersDialogueFragment extends DialogFragment implements NumberPicker.OnValueChangeListener {
NumberPicker numberOfPlayersPicker = null;
#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();
View v = inflater.inflate(R.layout.players_fragment_layout, null);
builder.setView(v);
numberOfPlayersPicker = (NumberPicker) v.findViewById(R.id.numberOfPlayersPicker);
numberOfPlayersPicker.setMaxValue(4);
numberOfPlayersPicker.setMinValue(2);
}
}
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");
}