Monday, 15 July 2019

Android Material Design BottomSheet Example


1) Add this dependency in your gradle file for material design 



    implementation 'com.android.support:design:28.0.0'


1) MainActivity.xml


<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".MainActivity">


    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <Button
                android:id="@+id/btnBottomSheet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Open Bottom Sheet"
                android:textStyle="bold"
                android:textSize="20dp"/>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>


    <include layout="@layout/bottom_layout" />

</android.support.design.widget.CoordinatorLayout>


2) Create bottom_layout.xml in res/layout folder and past below code


<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/mainLayout"
    android:background="@android:color/holo_blue_bright"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
     app:behavior_hideable="true"
     app:behavior_peekHeight="58dp"
    android:padding="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose One"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@android:color/white"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>

    <View
        android:layout_width="wrap_content"
        android:layout_height="5dp"
        android:background="@android:color/black"
        android:layout_marginRight="150dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/whatsapp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Whatsapp"
            android:padding="10dp"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/facebook"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Facebook"
            android:padding="10dp"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/twitter"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Twitter"
            android:padding="10dp"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/instagram"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Instagram"
            android:padding="10dp"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold"/>
    </LinearLayout>

</LinearLayout>


3) MainActivity.java


import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    LinearLayout mainLayout;
    BottomSheetBehavior bottomSheetBehavior;
    Button btnBottomSheet;

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

        mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
        btnBottomSheet = (Button) findViewById(R.id.btnBottomSheet);

        //Todo: Add A Layout For BottomSheetBehavior
        bottomSheetBehavior = BottomSheetBehavior.from(mainLayout);

        //Todo: The bottom sheet is hidden.
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

        //Todo: The bottom sheet is expanded.
        //bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

        //Todo: The bottom sheet is collapsed.
        //bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

        //Todo: The bottom sheet is dragging.
        //bottomSheetBehavior.setState(BottomSheetBehavior.STATE_DRAGGING);

        //Todo: The bottom sheet is settling.
        //bottomSheetBehavior.setState(BottomSheetBehavior.STATE_SETTLING);

        btnBottomSheet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });

    }
}

1 comment:

Anonymous said...

if you add an Screen Shot of the project that will be helpful.

Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...