Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rootview in fragment?

I am using this library for using an emoji keyboard on my app. https://github.com/ankushsachdeva/emojicon

The Readme states that the topmost view of your activity layout hierarchy has to be used to initialize the popupwindow.

My app is implemented via fragments.

This is the code I am using for testing:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.overview1_layout, container,
            false);

    // Give the topmost view of your activity layout hierarchy. This will be used to measure soft keyboard height
    EmojiconsPopup popup = new EmojiconsPopup(view, getActivity());

    //Will automatically set size according to the soft keyboard size        
    popup.setSizeForSoftKeyboard();

    popup.showAtBottom();


    return view;
}

If I run this code I am getting following error in logcat:

11-02 22:37:16.685: E/AndroidRuntime(30363): java.lang.RuntimeException: Unable to resume activity {com.Testing.full/com.Testing.full.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

EDIT: I am using SherlockFragment

like image 677
tobias Avatar asked Nov 02 '14 21:11

tobias


People also ask

How do I get a root view of a layout?

to obtain the root view of that hierarchy. The "decor view" can also be obtained via getWindow(). getDecorView() . This is the root of the view hierarchy and the point where it attaches to the window, but I'm not sure you want to be messing with it directly.

What is the Life cycle of android fragment?

Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. A fragment can implement a behaviour that has no user interface component.


1 Answers

save view as instance member of fragment and initialize Emojicons Popup in OnViewCreated method. That might solve your problem.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.overview1_layout, container,
        false);

this.view = view

return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

// Give the topmost view of your activity layout hierarchy. This will be used to measure soft keyboard height
EmojiconsPopup popup = new EmojiconsPopup(view, getActivity());




//Will automatically set size according to the soft keyboard size        
popup.setSizeForSoftKeyboard();

popup.showAtBottom();
}

But for the question title - check here

like image 89
Heisenberg Avatar answered Oct 04 '22 19:10

Heisenberg