Search code examples
androiddrawerlayout

How to show a popup TextView inside DrawerLayout for NavigationView


I try to create a TextView that pops up at the bottom just like Google Hangout appen. The Google Hangout app show a popup for a few sec saying signed-in as xxx.

I have this classic Navigation drawer layout

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.port.android.ui.ActivityMain">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar" />

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </FrameLayout>

        <TextView
            android:id="@+id/popup_logged_in"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/PrimaryColor"
            android:layout_alignParentBottom="true"
            android:padding="15dp"
            android:textSize="14sp"
            android:visibility="gone"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textAllCaps="true"
            android:textStyle="bold" />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_view_header"
        app:menu="@menu/menu_navigation">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:padding="20dp"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn_exit"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="exitAddress"
                android:text="Exit Address"/>

        </LinearLayout>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

As you see I put a TextView inside the LinearLayout and it do show up when i do view.animate() but it pushes the layout up and I want it to stay on top. So I try a RelativeLayout but that made the toolbar disappear.

Here´s the AppCompatActivity if it helps:

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (Application.getInstance().isClosing())
        finish();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    popUpThreadPoolFactory = new PopUpThreadPoolFactory(this);
    fragManager = getSupportFragmentManager();
    mMainAdapter = new ActivityMainAdapter(this);
    // Initializing Toolbar and setting it as the actionbar
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    //Initializing NavigationView
    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
    mNavigationView.setNavigationItemSelectedListener(this);
    // Initializing Drawer Layout and ActionBarToggle
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ((Button) findViewById(R.id.btn_exit)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            FirebaseManager.getInstance().exitCurrentAddress();
        }
    });

    mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.openDrawer, R.string.closeDrawer) {
        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
            super.onDrawerOpened(drawerView);
        }
    };
    //Setting the actionbarToggle to drawer layout
    mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);
    //calling sync state is necessay or else your hamburger icon wont show up
    mActionBarDrawerToggle.syncState();
    // Initialize Firebase Analytics
    mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client2 = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

@Override
protected void onStart() {
    super.onStart();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client2.connect();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "ActivityMain Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app URL is correct.
            Uri.parse("android-app://com.port.android/http/host/path")
    );
    AppIndex.AppIndexApi.start(client2, viewAction);
}

@Override
protected void onResume() {
    super.onResume();
    MyLog.i(TAG, "onResume");
    Application.getInstance().addUIListener(OnConnectionStateListener.class, this);
    Application.getInstance().addUIListener(OnExitAddressListener.class, this);
    Application.getInstance().addUIListener(OnUserSignedInListener.class, this);
    // update state
    if (Application.getInstance().isInitialized() && !Application.getInstance().isClosing()) {
        MyLog.i(TAG, "Application isInitialized activating adapter");
        mainAdapterOnChange();
    } else
        MyLog.i(TAG, "Wait for loading or closing");
}

Solution

  • I think you are searching about a SnackBar.. If this is what you are trying then here is an example.

    In you Activity where you want to show this, Bind your parent Layout.Suppose:
    in your activity.xml

    <android.support.design.widget.CoordinatorLayout     
      xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/cd"
    tools:context="com.example.ayyappaboddupalli.dragmaker.MainActivity">
    

    In your activity.java class:

        CoordinatorLayout cd=(CoodinatorLayour)findViewById(R.id.cd);
    

    Now if you want to show that popup on click of a button:

        btn.setOnClickListener(new onClickListener()
    
        public void onClick(View v)
       {
       Snackbar.make(cd,"text you want to show",Snackbar.LENGTH_LONG).show();
        }   
        );