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

C++ programming language

                c++ is a general programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features.C++ runs on lots of platforms like Windows, Linux, Unix, Mac etc.   ❖❖  C++ with oops ❖❖

7. Switch Statement in c++

 ❖ C++ switch Statement :-                Consider a situation in which, only one block of code needs to be executed among many blocks. This type of situation can be handled using nested if...else statement but, the better way of handling this type of problem is using switch...case statement.  Syntax of switch:-      Switch(expression) {            Case value1 : statement1;                       Break;             Case value2 : statement2;                      Break;             Default : default statements;  }               The expression is either an integer or a character in above syntax. If the expression matches constant in case, the relevant codes a...

3. Input /output in c++

  ➢   Input/ Output in C++ :-        C++ comes with libraries which provides us many ways for performing input and output. In C++.        input and output is performed in the form of sequence of bytes or more commonly known as  streams. ● Input Stream: If the direction of flow of bytes is from device(for example: Keyboard) to the main memory then this process is called input. ● Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device(display screen ) then this process is called output. ● Header files available in C++ for Input – Output operation are: • iostream: iostream stands for standard input output stream. This header file contains definitions to objects like cin, cout, cerr etc. • fstream: This header file mainly describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.       ...