Bottom Fragment in Android Java

In this tutorial, we will write code to show fragment via bottom sheet in Android application using Java.
You can use any latest version of Android Studio. This tutorial was written using Android Studio 4.1.

Working with Bottom Fragment in Android Java

As usual first create a new project in Android Studio, with min version as 26.
Then we proceed towards writing code for it.

Code

First, we create a bottom_fragment.xml file in the directory which has activity_main.xml that will act like a fragment:

Paste this code in that bottom_fragment.xml file: bottom_fragment.xml file

Now, create a button in activity_main.xml to show this pop_up like fragment:

<?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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/bottom_fragment_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="276dp"
        android:text="Bottom Fragment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.537"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.531" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

Now, Create a MyBottomFragment.java file and write this code there:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

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

public class MyBottomFragment extends BottomSheetDialogFragment {

    public MyBottomFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.bottom_fragment,container,false);
    }

}

In the above code, bottom_fragment.xml layout is inflated and sent to the calling Activity.

We can also make functions as to what should happen if any user clicks or performs any action on this fragment. (Like Update Data on Button Press)

Now write this code in MainActivity.java file:

package org.codespeedy;

import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    Button mBottomFragmentButton;

    @Override
    public void onCreate(Bundle saved) {
        super.onCreate(saved);
        setContentView(R.layout.activity_main);

        mBottomFragmentButton = findViewById(R.id.bottom_fragment_button);

        mBottomFragmentButton.setOnClickListener(v -> {
            BottomSheetDialogFragment bottomSheetDialogFragment = new MyBottomFragment();
            bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
        });
    }
}

In the above code,

  1. First we get the bottom_fragment_button by id.
  2. Then we create a onClickListener()
  3. Then we write code to show the bottom_fragment as a bottom sheet element.

A customized beautiful bottom to top smooth popup like screen has been made successfully!!

 

Leave a Reply

Your email address will not be published. Required fields are marked *