Reputation: 3255
Implemented a dialog fragement this way:
public class ProgressFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.installation_page, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
titleView = (TextView) v.findViewById(R.id.progressText);
titleView.setText("progress");
builder.setView(v);
return builder.create();
}
TextView titleView;
public TextView getTextView() {
return titleView;
}
and i want to be able to update content of titleView, from whereever it is called. like:
ProgressFragment progressFragment = new ProgressFragment();
progressFragment.show(getSupportFragmentManager(),"");
issue is triers like:
((TextView)progressFragment.getDialog().findViewById(R.id.progressText)).setText("text");
and
progressFragment.getTextView().setText("text");
and
((TextView)progressFragment.getView().findViewById(R.id.progressText)).setText("working in this place");
all returns null. How do i do this. Thanks
Upvotes: 10
Views: 6416
Reputation: 3625
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
// Get the layout inflater
val inflater = requireActivity().layoutInflater;
// get custom layout view
val view = inflater.inflate(R.layout.edit_text_dialog, null)
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(view)
// Add action buttons
.setPositiveButton(R.string.ok,
DialogInterface.OnClickListener { dialog, id ->
// sign in the user ...
val editText = view.findViewById<EditText>(R.id.channelName)
Toast.makeText(activity, editText.text.toString(), Toast.LENGTH_SHORT).show()
})
.setNegativeButton(R.string.cancel,
DialogInterface.OnClickListener { dialog, id ->
getDialog()?.cancel()
})
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
Upvotes: 0
Reputation: 3255
Not so sure if this is a good practice but i fixed it using callbacks.
public interface dialogViewCallBack {
void onViewLoaded(TextView textView);
}
static dialogViewCallBack dialogViewCallBack;
public ProgressFragment progressFragment(dialogViewCallBack callBack) {
dialogViewCallBack = callBack;
return new ProgressFragment();
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.installation_page, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
TextView titleView = (TextView) v.findViewById(R.id.progressText);
titleView.setText("progress");
builder.setView(v);
dialogViewCallBack.onViewLoaded(titleView);
return builder.create();
}
So, on the class that call progressFragment, i just do:
public class TestActivity extends AppCompatActivity implements ProgressFragment.dialogViewCallBack {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProgressFragment progressFragment = new ProgressFragment().progressFragment(this);
progressFragment.show(getSupportFragmentManager(),"");
}
@Override
public void onViewLoaded(TextView textView) {
// can assign the view to another view here.
textView.setText("working here");
}
}
Upvotes: 2
Reputation: 10859
No! No! No!
if you create your DialogFragment with
`Dialog onCreateDialog(Bundle savedInstanceState)`
(your View is dead) as you created it with Dialog but not
public View onCreateView(LayoutInflater inflater,ViewGroup
container,Bundle savedInstanceState) {
Looking at your code i will suggest you use the second method of creating your DialogFrament as you are just return a TextView
cheese
but if you insist on your method, then your solution is, quite longer
progressFragment.getDialog().getWindow().getDecorView()
the decorview might be your TextView
im not sure but overall
progressFragment.getDialog().getWindow().findViewById();
Upvotes: 3
Reputation: 4357
Create a setter method for text view in Dialog Fragment
public void setTextView(String str) {
titleView.setText(str);
}
and call
ProgressFragment progressFragment = new ProgressFragment();
progressFragment.show(getSupportFragmentManager(),"");
progressFragment.setTextView("title");
Upvotes: 1