Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Views from DialogFragment

Tags:

java

android

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

like image 757
Nosakhare Belvi Avatar asked Sep 04 '15 08:09

Nosakhare Belvi


People also ask

How do you show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

What is DialogFragment used for?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do I destroy DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.


2 Answers

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();
like image 191
Elltz Avatar answered Sep 24 '22 10:09

Elltz


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");
}
}
like image 34
Nosakhare Belvi Avatar answered Sep 25 '22 10:09

Nosakhare Belvi