Saturday, January 28, 2017

Set Dialog position to bottom and remove default margins

Tags

Default position of the dialog is at the middle of the screen. This example show you, how to change the position of the dialog to bottom.

1.layout_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="bottom"
    android:background="@color/black_overlay">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#fff">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="10dp"
            android:text="MENU"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:drawable/ic_menu_edit" />
            <TextView
                android:id="@+id/tv_edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Edit"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="10dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:drawable/ic_delete" />
            <TextView
                android:id="@+id/tv_delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Delete"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>



2.style.xml

Add style to style.xml
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <!-- Customize your theme here. -->   
    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

3.DialogMenu.java

public class DialogMenu implements View.OnClickListener{
    public DialogMenu(Context context) {
        Dialog dialog = new Dialog(context, R.style.DialogTheme);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.layout_menu);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView tv_edit = (TextView) dialog.findViewById(R.id.tv_edit);
        TextView tv_delete = (TextView) dialog.findViewById(R.id.tv_delete);
        tv_edit.setOnClickListener(this);
        tv_delete.setOnClickListener(this);

    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_edit :
                Log.d("Test ", " 1 ");
                break;
            case R.id.tv_delete :
                Log.d("Test ", " 2 ");
                break;
        }
    }
}

4.Usage

new DialogMenu(MainActivity.this);

This Is The Oldest Page


EmoticonEmoticon