Question

Hi ?, I have a question in the android studio, I am using button sheet to...

Hi ?, I have a question in the android studio, I am using button sheet to display information, I want to display the button sheet depending on condition, display or hidden and, when collapse and expand the bottom sheet is repeated on background how to solve this problem?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

See below for a layout file having a LinearLayout with two buttons in the main layout visible to the user, and then by a NestedScrollView which acts as the bottom sheet.

<?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"
tools:context="com.thetechnocafe.gurleensethi.bottomsheets.BottomSheetActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<Button
android:id="@+id/openBottomSheetButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Bottom Sheet" />

<Button
android:id="@+id/closeBottomSheetButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close Bottom Sheet" />

</LinearLayout>

<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@android:color/holo_green_light"
android:clipToPadding="true"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/random_title"
android:textColor="@android:color/white"
android:textSize="24sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/lorem_ipsum"
android:textColor="@android:color/white" />
</LinearLayout>

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

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

In the above lines, the attribute app:behavior_hideable=”true” allows the bottom sheet to be completely hide-able even if you have set a peak height.

To make a layout behave as BottomSheet, you need to add the app:layout_behaviour=”@string/bottom_sheet_behaviour” to the layout

Even after adding this behaviour, sometimes the bottom sheet will not hide and will keep displaying. In this case, you will have to explicitly set the peek height of the bottom sheet. To do this just add the following line:

app:behavior_peekHeight="0dp"

This will set the peek height of the bottom sheet. Increase the value to observe how the bottom sheets comes up.

In order to control the bottom sheet from an activity you will have to get a reference of it. Here is how to get the reference:

public class BottomSheetActivity extends AppCompatActivity {

private BottomSheetBehavior mBottomSheetBehaviour;

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

View nestedScrollView = (View) findViewById(R.id.nestedScrollView);
mBottomSheetBehaviour = BottomSheetBehavior.from(nestedScrollView);
}
}

Bottom sheets define 5 different states, you can listen to state changes by setting a callback function:

mBottomSheetBehaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
String state = "";

switch (newState) {
case BottomSheetBehavior.STATE_DRAGGING: {
state = "DRAGGING";
break;
}
case BottomSheetBehavior.STATE_SETTLING: {
state = "SETTLING";
break;
}
case BottomSheetBehavior.STATE_EXPANDED: {
state = "EXPANDED";
break;
}
case BottomSheetBehavior.STATE_COLLAPSED: {
state = "COLLAPSED";
break;
}
case BottomSheetBehavior.STATE_HIDDEN: {
state = "HIDDEN";
break;
}
}

Toast.makeText(BottomSheetActivity.this, "Bottom Sheet State Changed to: " + state, Toast.LENGTH_SHORT).show();
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {

}
});

You will find a lot more information in the Android docs (Bottom Sheets in Material components and BottomSheetBehavior in Android Developers). All the best!

Add a comment
Know the answer?
Add Answer to:
Hi ?, I have a question in the android studio, I am using button sheet to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Mobile-Android Studio) Hello. Thank you for coming to help me:) I am using the Andriod studio...

    (Mobile-Android Studio) Hello. Thank you for coming to help me:) I am using the Andriod studio on mac. and using Java code to develop. and Please start with basic activity with constraints layout (not empty). (audio file is not given, let's say there any audio file). I just would like to see the codes how it works. Thank you very much. I would like to know how to write a program that includes two buttons called plays Play and Stop....

  • Using java in android studio, I need help in making a text to speech button that...

    Using java in android studio, I need help in making a text to speech button that reads off of my text view thanks for the help

  • In android studio, I want to get the user current location when he clicks on a...

    In android studio, I want to get the user current location when he clicks on a button after asking him for the permission, store its latitude and longitude on double variables and then do calculations depends on the user location, How can I do that in maps activity? I want on click to be outside onCreate, I want inside on click to get double lat = latlong.getlatutide not to call function getlocation

  • Hi, I am trying to create a simple android app using spinners. It need not be...

    Hi, I am trying to create a simple android app using spinners. It need not be very complex. The app should allow the user to use a simple spinner to select a team from the following list of teams (Kansas City Chiefs, New England Patriots, New Orleans Saints, and the Los Angeles Rams), once the user selects the team they wish to view, the app should display a small paragraph of information (hardcoded) To the screen for the user to...

  • Hey there, I am having a hard time using GUI for c# in visual studio. I...

    Hey there, I am having a hard time using GUI for c# in visual studio. I am making an alarm clock. I have a few questions that I would like to know how to do: --using a button called set time to capture text from a box containing a user-inputted hour, minute, second, and setting the current time to that. --using a button called alarm, Capture the Text in the TextBoxes - the Hours, Minutes, Seconds and Text (am/pm) and...

  • Using Android Studios. Scenario: You recently started working as the mobile app developer for a University...

    Using Android Studios. Scenario: You recently started working as the mobile app developer for a University and have been assigned to build a mobile app that calculates the students' grade. Your app should contain following screens: Screen 1 Labels for subject1, subject2, and subject3 Textboxes for subject1, subject2, and subject3 Labels for MaxGrade, MinGrade, and Avg.Grade Submit button Once user clicks submit button, you need to display letter grade in a label Screen 2 Screen that allows students to register...

  • I need help developing this app using java in android studio Assignment: Tic-Tac-Toe app User interface...

    I need help developing this app using java in android studio Assignment: Tic-Tac-Toe app User interface Operation The app allows the user to play a game of Tic-Tac-Toe. The user can click the New Game button at any time to start a new game. The app displays other messages to the user as the game progresses such as (1) whose turn it is, (2) if a player wins, and (3) if the game ends in a tie. Specifications The app...

  • Hi, I am trying to solve a problem where I have to find the fourier series...

    Hi, I am trying to solve a problem where I have to find the fourier series for a square wave with an amplitude of one, w = 2pi. How can I show that the An coeffcients of the fourier serie will be zero for a square wave? The square wave is first wave in the picture.

  • Hi! Can someone explain how to to answer the problem? I have tried but I am...

    Hi! Can someone explain how to to answer the problem? I have tried but I am confused on how to write it? Assume that a competitive market is currently represented by the following supply and demand equations: Qs P 20 Qd 110-0.5P 1. (2 Points) Solve for the equilibrium price and quantity. 2. (1 Point) Solve for the price elasticity of demand. 3. (4 Points) Graph the market equilibrium. Fully label the graph.

  • Please help me with this project by using Android Studio. I need to know the xml...

    Please help me with this project by using Android Studio. I need to know the xml and the java activities in both activities. Also the manifest activity. I appreciate the effort and I'll rate you with 5 stars. 1. SUMMARY This project will simulate the card game Concentration for one player. In Concentration, all cards, or pictures in this case, are upside down. Two cards are chosen, and if they match they are taken out of the game. If they...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT