.showAtLocation on Android

0

I found this tutorial in android which allows to show a popup window watch tutorial

But what I do is show my popupwindow without pressing any button, this is what I have:

private PopupWindow popupWindow;
@Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.conductor);
       View rootView = getWindow().getDecorView().getRootView();
       mostrar_popup(rootView);
}

private void mostrar_popup(View rootView) {
        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // inflate the custom popup layout
        final View inflatedView = layoutInflater.inflate(R.layout.lista_alumnos, null,false);
        // find the ListView in the popup layout
        ListView lista_alumnos = (ListView)inflatedView.findViewById(R.id.lista_alumnos);
        // get device size
        Display display = getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);
        // set height depends on the device size
        popupWindow = new PopupWindow(inflatedView, size.x - 50,size.y - 400, true );
        // set a background drawable with rounders corners
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.fondo_lista_alumnos));
        // make it focusable to show the keyboard to enter in 'EditText'
        popupWindow.setFocusable(true);
        // make it outside touchable to dismiss the popup window
        popupWindow.setOutsideTouchable(true);

        // show the popup at bottom of the screen and set some margin at bottom ie,
        popupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0,100); //aqui esta mi error
    }

conductor.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff" >

    <fragment
        android:id="@+id/map_conductor"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

I thank you in advance

    
asked by Dimoreno 05.06.2017 в 21:13
source

1 answer

1

If you do this from onCreate () you will probably have an error, since the root view is not completely created:

   View rootView = getWindow().getDecorView().getRootView();
   mostrar_popup(rootView);

To work properly by calling the method from onCreateView() , add an id in your layout loaded by setContentView() , for example:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"

        android:id="@+id/container"

>

the defined id is container, then you call the method this way:

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findViewById(R.id.container).post(new Runnable() {
            public void run() {
                View rootView = getWindow().getDecorView().getRootView();
                onShowPopup(rootView);
            }
        });


    }

According to your code it would be like this:

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.conductor);


        findViewById(R.id.map_container).post(new Runnable() {
            public void run() {
                View rootView = getWindow().getDecorView().getRootView();
                onShowPopup(rootView);
            }
        });


    }
    
answered by 05.06.2017 / 21:26
source