Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 4 October 2016

BottomSheetDialogsample in android



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purpule_light"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <Button
            android:id="@+id/btnDialog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Show dialog" />
    </LinearLayout>

</LinearLayout>


MainActivity,java

package com.samset.user.bottomsheetsample;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    private Button btnDialog;
    private BottomSheetDialog bottomSheetDialog;


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

        btnDialog = (Button) findViewById(R.id.btnDialog);
        btnDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheetDialog();
            }
        });

    }
    private void showBottomSheetDialog() {

        bottomSheetDialog = new BottomSheetDialog(this);
        View view = getLayoutInflater().inflate(R.layout.bottomsheet, null);
        RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.rg_sortby);

        bottomSheetDialog.setContentView(view);
        bottomSheetDialog.show();
        bottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                // collapse(bottomSheetDialog);
                bottomSheetDialog = null;
            }
        });
    }


}

bottomsheet.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   >


    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginBottom="@dimen/activity_horizontal_margin"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_marginTop="10dp"
            android:text="Sort by"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <View
            android:layout_marginTop="5dp"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#e5e5e5"/>

        <RadioGroup
            android:id="@+id/rg_sortby"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:gravity="center">

            <RadioButton
                android:text="@string/popularity"
                android:gravity="right|center_vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />


            <RadioButton
                android:text="@string/new_arrivals"
                android:gravity="right|center_vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <RadioButton
                android:gravity="right|center_vertical"
                android:text="@string/price_low_to_high"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <RadioButton
                android:gravity="right|center_vertical"
                android:text="@string/price_hight_to_low"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </RadioGroup>
    </LinearLayout>
</LinearLayout>


Thank you

Live Sample