Skip to main content

How to implement Bottom sheet dialog in android studio

For Full video click on below link :-

https://youtu.be/RdKznpj3p30?si=41W4Dj0focRWbFln


Working with the activity_main.xml file.


Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity"
    android:background="@color/white">

    <Button
        android:id="@+id/openbtn"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:backgroundTint="@color/black"
        android:text="Open"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/txtadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.219" />
</androidx.constraintlayout.widget.ConstraintLayout>




Working with the MainActivity.java file.


Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. 


package com.example.bottom_dialog;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btn;
    TextView edt;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn= findViewById(R.id.openbtn);
        edt = findViewById(R.id.txtadd);

        Intent intent = getIntent();
        String a = intent.getStringExtra("data");
        edt.setText(a);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bottomsheet bottomsheet = new Bottomsheet();
                bottomsheet.show(getSupportFragmentManager(),"TAG");

            }
        });

    }
}


Create a new Layout file for out bottom sheet dialog.


Go to the row_add_item.xml file and refer to the following code. Below is the code for the row_add_item.xml file. 


<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:orientation="vertical"
    android:background="@drawable/edittext2">
    
    <View
        android:layout_width="100dp"
        android:layout_height="3dp"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:background="#232222"/>

    <EditText
        android:id="@+id/new_add_task_name"
        android:layout_width="350dp"
        android:layout_height="45dp"
        android:background="@drawable/edit_text"
        android:layout_marginTop="50dp"
        android:layout_gravity="center_horizontal"
        android:hint="Task Name"
        android:textColorHint="#7733C1"
        android:paddingStart="20dp"
        tools:ignore="Autofill,HardcodedText,RtlSymmetry,TextFields" />

    <EditText
        android:id="@+id/new_add_task_des"
        android:layout_width="350dp"
        android:layout_height="45dp"
        android:background="@drawable/edit_text"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:hint="Task Description"
        android:textColorHint="#7733C1"
        android:paddingStart="20dp"
        tools:ignore="Autofill,HardcodedText,RtlSymmetry,TextFields" />

    <Button
        android:id="@+id/new_add_task_btn"
        android:layout_width="350dp"
        android:layout_height="50dp"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"
        android:backgroundTint="#7733C1"
        android:text="Done!!"
        android:textColor="@color/white"
        android:textSize="15sp"
        tools:ignore="HardcodedText" />

    <View
        android:layout_width="100dp"
        android:layout_height="3dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="#D5C3EC"/>
</LinearLayout>



Create a java class for  bottom sheet dialog.

Go to the Bottomsheet.java file and refer to the following code. Below is the code for the Bottomsheet.java file.


package com.example.bottom_dialog;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class Bottomsheet extends BottomSheetDialogFragment {
    public Bottomsheet() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.row_add_item,container,false);

        Button btnadd=view.findViewById(R.id.new_add_task_btn);
        EditText ed= view.findViewById(R.id.new_add_task_name);

        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getContext(), MainActivity.class);
                intent.putExtra("data",ed.getText().toString());
                startActivity(intent);
            }
        });


        return view;
    }
}

Comments

Popular posts from this blog

Remove specific item from Recyclerview in android studio

For Full video click on below link :- https://youtu.be/sUz4fqeanjI?si=A8AqBWCgu-5NnBOJ Working with the activity_main.xml file. Navigate to the   app > res > layout > activity_main.xml  and add the below code to that file. Below is the code for the   activity_main.xml   file.  <?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:background="@color/white"> <EditText android:layout_width="350dp" android:layout_height="50dp" android:inputType="text" android:id="@+id/coursenam...

Fingerprint Authentication in Android Studio Project

Implement the implementation : - implementation 'androidx.biometric:biometric:1.0.1' Working with the activity_main.xml file. Navigate to the   app > res > layout > activity_main.xml  and add the below code to that file. Below is the code for the   activity_main.xml   file. <?xml version="1.0" encoding="utf-8"?> <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="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity" android:background="@color/white"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Boxcode!!" android:textSize=...

programming skills imp programs

Full course of c++ programming language Click here >  C++ programming language ===================================== 1. Write a program to display an average of 3 inputted numbers. a=int(input("Enter a : ")) b=int(input("Enter a : ")) c=int(input("Enter a : ")) avg=(a+b+c)/3 print(avg) ====================================== 2. Write a program to calculate simple interest for a given principle amount, rate of interest and no of years. p=float(input("Enter a P : ")) r=float(input("Enter a R : ")) n=float(input("Enter a N : ")) si=(p*r*n)/100 print("simple interest is ",si) ====================================== 3. Write a program to display area of triangle. base=float(input("Enter a base : ")) height=float(input("Enter a height : ")) area=base*height/2 print("Area of triangle is ",area) ====================================== 4. Write a program to calculate area of circle. r=float(input...