loading...
DotNek app development
DotNek بازدید : 136 چهارشنبه 19 خرداد 1400 نظرات (0)

so that they can be opened from the bottom of the page to the top of the page and show the required information to users, and Bottom Sheets , including components Material Design are on Android. They have two different types, one Persistent Bottom Sheet and Modal Bottom Sheet (modal bottom sheets android), both of which are used to display information to users, and the way to access them is by dragging them from the bottom of the page and then the item information We see the needs and details. It should be noted that these can be used in applications such as Google Maps and Google Drive. Google Drive and 

6- Define and create a simple Bottom Sheet. Like the following:

<? xml version = "1.0" encoding = "utf-8"?>
<android. support.design. widget. CoordinatorLayout 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"
xmlns: app = "http://schemas.android.com/apk/res-auto"
tools: context = ". MainActivity">

<LinearLayout
android: layout_width = "match_parent"
android: layout_height = "200dp"
android: orientation = "vertical"
android: background = "# 009688"
android: padding = "8dp"
app: layout_behavior = "android. support.design. widget. BottomSheetBehavior">

<TextView
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: textColor = "# ffffff"
android: textSize = "20sp"
android: textStyle = "bold"
android: text = "Bottom Sheet Title" />

<TextView
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: textColor = "# ffffff"
android: textSize = "16sp"
android: text = "Lorem ipsum dolor sit amet, te sed vide delicata,
per ex salutandi intellegat temporibus, ei insolens molestiae vis.
Eum ei possim aperiam, fuisset suscipit vim ut. Voluptua repudiare
gubergren id eum, nullam labores an nam. Sed et tota quae referendum,
enim elit democritum quo et.
Lorem ipsum dolor sit amet, te sed vide delicata,
per ex salutandi intellegat temporibus, ei insolens molestiae vis.
Eum ei possim aperiam, fuisset suscipit vim ut. Voluptua repudiare
gubergren id eum, nullam labores an nam. Sed et tota quae referendum,
enim elit dem ocritum quo et. "/>

</LinearLayout>

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

In the code above, we add a LinearLayout to the layout that has two TextViews. Note that we must define LinearLayout as a Bottom Sheet. Like the following:

app: layout_behavior = "android. support.design. widget. BottomSheetBehavior"

By inserting the above code and adding a layout to it, we will cause Android to consider it as a Bottom Sheet. The value of this property can be defined as follows:

app: layout_behavior = "@ string / bottom_sheet_behavior"

Then, hold down the Ctrl key on the keyboard and click on the value entered above. This will connect to the values.xml file that is linked to the design library.

7- We run the project. After running the program, we will see that a bar with a background color of 200dp will appear at the bottom of the screen.

8- It should be noted that the content of this bar has two TextViews.

9- This bar that we have created is currently fixed and does not change by dragging down or up.

10- In order to be able to adjust it so that it changes by dragging down or up, we must add the property listed below to  is used to show the contents.

For example, we can say that the user selects a place on the map in Google Map and by dragging the bar upwards he can see the information and details about it, and if he presses the MORE INFO button, he can even see the contents related to it. Slowly

1- We create a new project in Android Studio and choose its name as desired. The name chosen in this tutorial is PersistentBottomSheet.

2- In this project, we create an Empty Activity.

3- We also add the design library to this section.

4- In this project, unlike the previous project, we want it to be opened and closed by clicking on the Bottom Sheet button.

5- The codes that should be in the activity_main.xml section are as follows:

<? xml version = "1.0" encoding = "utf-8"?>
<android. support.design. widget. CoordinatorLayout
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"
tools: context = ". MainActivity">

<RelativeLayout
android: layout_width = "match_parent"
android: layout_height = "match_parent">

<Button
android: id = "@ + id / btn_expand"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_centerInParent = "true"
android: text = "Expand" />

</RelativeLayout>

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

What is the function of Bottom Sheets in Android and what are they used for?

In the code above, because the CoordinatorLayout does not have the power and ability to manage the layout, we put the button in RelativeLayout.

6- Add a Bottom Sheet to the layout.

<LinearLayout
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: orientation = "vertical"
android: id = "@ + id / bottom sheet"
android: background = "# 94aab4"
android: padding = "8dp"
app: behavior_hideable = "true"
app: behavior_peekHeight = "60dp"
app: layout_behavior = "@ string / bottom_sheet_behavior">

<TextView
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: textColor = "# 000000"
android: textSize = "20sp"
android: text = "Location: Austria "
android: paddingbottom = "10dp"
android: paddingtop = "10dp" />

<TextView
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: textColor = "# 452f2f"
android: textSize = "16sp"
android: text = " Austria initially emerged as a margraviate around 976 and developed into a duchy and archduchy. In the 16th century, Austria started serving as the heart of the Habsburg Monarchy and the junior branch of the House of Habsburg – one of the most influential royal dynasties in history. As an archduchy, it was a major component and administrative center of the Holy Roman Empire. Early in the 19th century, Austria established its own empire, which became a great power and the leading force of the German Confederation, but pursued its own course independently of the other German states following its defeat in the Austro-Prussian War in 1866. In 1867, in compromise with Hungary, the Austria-Hungary Dual Monarchy was established. "/>
</LinearLayout>

In the code above, we used wrap_content to specify the size and height of the bar, which causes the bar to be so high that the content is there. In other words, it can be said that the height of this bar will be equal to the content that is inside it. In this section, we did not specify a height for the bar.

7- Create a new layout with the desired name, for example bottom_sheet.xml, and put the code inserted below it.

<? 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 / bottom sheet"
android: background = "# 94aab4"
android: padding = "8dp"
app: behavior_hideable = "true"
app: behavior_peekHeight = "60dp"
app: layout_behavior = "@ string / bottom_sheet_behavior">

<TextView
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: textColor = "# 000000"
android: textSize = "20sp"
android: text = "Location: Austria "
android: paddingbottom = "10dp"
android: paddingtop = "10dp" />

<TextView
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: textColor = "# 452f2f"
android: textSize = "16sp"
android: text = " Austria initially emerged as a margraviate around 976 and developed into a duchy and archduchy. In the 16th century, Austria started serving as the heart of the Habsburg Monarchy and the junior branch of the House of Habsburg – one of the most influential royal dynasties in history. As an archduchy, it was a major component and administrative center of the Holy Roman Empire. Early in the 19th century, Austria established its own empire, which became a great power and the leading force of the German Confederation, but pursued its own course independently of the other German states following its defeat in the Austro-Prussian War in 1866. In 1867, in compromise with Hungary, the Austria-Hungary Dual Monarchy was established. "/>

</LinearLayout>

8- The complete and final codes that should be in the activity_main.xml section are as follows:

<? xml version = "1.0" encoding = "utf-8"?>
<android. support.design. widget. CoordinatorLayout 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"
tools: context = ". MainActivity">

<RelativeLayout
android: layout_width = "match_parent"
android: layout_height = "match_parent">

<Button
android: id = "@ + id / btn_expand"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_centerInParent = "true"
android: text = "Expand" />

</RelativeLayout>

<include layout = "@ layout / bottom sheet" />

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

9- After applying the changes, if we run the project, we will see that the tape will open at 60dp first, and if we then drag it upwards, its size will be larger.

10- In this section, we have to make changes and adjustments so that this opening of the page is done by the button.

11- We complete the activity as follows:

import android. support.design. widget. BottomSheetBehavior;
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 {

Button banshee;
LinearLayout sheet Layout;
BottomSheetBehavior bottom Sheet;

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

banshee = findViewById (R.id.btn_expand);
sheet Layout = findViewById (R.id. bottom sheet);
bottom Sheet = BottomSheetBehavior. From (sheet Layout);

btnPhoto.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {

bottomSheet.setState (BottomSheetBehavior.STATE_EXPANDED);

}
});

}
}

In the code above, we define a Button and LinearLayout that belong to Bottom Sheet. Then we create an example of the BottomSheetBehavior method with the desired name bottom Sheet.

Then we create a Listener for the button. To be able to set the Bottom Sheet mode, we use the set State () method.

In the code above, we have selected STATE_EXPANDED, but by selecting it, we can make it open by touching the status bar button.

12- We will implement the project.

13- After the implementation of the project, we will see that the operation is done correctly and the settings that we have provided have been successful.

14- Then, we have to make changes that are hidden by touching the bar button. Like the following:

btnPhoto.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {

if (bottomSheet.setState () == BottomSheetBehavior.STATE_COLLAPSED) {

bottomSheet.setState (BottomSheetBehavior.STATE_EXPANDED);

}
else if (bottom Sheet. getState () == BottomSheetBehavior.STATE_EXPANDED) {

bottomSheet.setState (BottomSheetBehavior.STATE_COLLAPSED);

}

}
});

In the code above, using the if else and the getState () method, we set a condition that will check that if the bar is collapsed, its status will change to Expanded by touching the button.

15- Use the setText () method and modify the condition, as follows:

btnPhoto.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {

if (bottomSheet.setState () == BottomSheetBehavior.STATE_COLLAPSED) {

bottomSheet.setState (BottomSheetBehavior.STATE_EXPANDED);
btnSheet.setText ("Collapse");

}
else if (bottomSheet.setState () == BottomSheetBehavior.STATE_EXPANDED) {

bottomSheet.setState (BottomSheetBehavior.STATE_COLLAPSED);
btnSheet.setText ("Expand");

}

}
});

What is the function of Bottom Sheets in Android and what are they used for?

16- If we run the project after making all the changes, we will see that everything is successful.

17- There is only one problem and that is that if we move the bar down or up using the button, the text of the button will also change, but if we move it up or down by hand, there is no change in text will not be done and we have to fix this problem and apply the relevant settings.

18- We use the setBottomSheetCallback method, which can manage the Bottom Sheet.

bottomSheet.setBottomSheetCallback (new BottomSheetBehavior.BottomSheetCallback () {
@Override
public void onTextChanged (@NonNull View bottom Sheet, int new State) {

}

@Override
public void on Slide (@NonNull View bottom Sheet, float slide Offset) {

}
});

19- It should be noted that this method has two functions. One onTextChanged and on Slide.

 20- In this section, we use the first function to solve the problem.

public void onTextChanged (@NonNull View bottom Sheet, int new State) {

if (new State == BottomSheetBehavior.STATE_EXPANDED) {
btnSheet.setText ("Collapse");
} else if (new State == BottomSheetBehavior.STATE_COLLAPSED) {
btnSheet.setText ("Expand");
}

}

21- The complete and final code that should be in the MainActivity.java section is as follows:

import android. support. annotation. Nonnull;
import android. support.design. widget. BottomSheetBehavior;
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 {

Button btnSheet;
LinearLayout sheet Layout;
BottomSheetBehavior bottom Sheet;

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

btnSheet = findViewById (R.id.btn_expand);
sheet Layout = findViewById (R.id. bottom sheet);
bottom Sheet = BottomSheetBehavior. From (sheet Layout);

btnPhoto.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {

if (bottomSheet.setState () == BottomSheetBehavior.STATE_COLLAPSED) {

bottomSheet.setState (BottomSheetBehavior.STATE_EXPANDED);
//btnSheet.setText("Collapse ");

}
else if (bottomSheet.setState () == BottomSheetBehavior.STATE_EXPANDED) {

bottomSheet.setState (BottomSheetBehavior.STATE_COLLAPSED);
//btnSheet.setText("Expand ");

}

}
});

bottomSheet.setBottomSheetCallback (new BottomSheetBehavior.BottomSheetCallback () {
@Override
public void onTextChanged (@NonNull View bottom Sheet, int new State) {

if (new State == BottomSheetBehavior.STATE_EXPANDED) {
btnSheet.setText ("Collapse");
} else if (new State == BottomSheetBehavior.STATE_COLLAPSED) {
btnSheet.setText ("Expand");
}

}

@Override
public void on Slide (@NonNull View bottom Sheet, float slide Offset) {

}
});

}
}

Modal Bottom Sheet

In this section, we also display a Dialog as a Bottom Sheet. Like Google Drive.

It should be noted that Modal is used to display options such as Upload, Copy and Share or even other options.

1- We create a new project in Android Studio and select its name as desired, and the name selected in this section for this project is ModalBottomSheet.

2- After creating the project and performing the next steps, we must create an Empty Activity.

3- Like previous projects, we add the Support Design library to the project.

4- The codes that should be in the activity_main.xml section are as follows:

<? xml version = "1.0" encoding g = "utf-8"?>
<android. support.design. widget. CoordinatorLayout 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"
tools: context = ". MainActivity">

<RelativeLayout
android: layout_width = "match_parent"
android: layout_height = "match_parent">

<Button
android: id = "@ + id / btn_expand"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_centerInParent = "true"
android: text = "Expand" />

</RelativeLayout>

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

5- Create a fragment to display the Bottom Sheet in the form of a Dialog.

6- After creating the fragment, we have to change its inheritance, that is, change it from Fragment to BottomSheetDialogFragment.

7- The code that should be in the BottomSheetFragment.java section is as follows:

import android.os. Bundle;
import android. support.design. widget. BottomSheetDialogFragment;
import android. view. LayoutInflater;
import android. view. View;
import android. view. ViewGroups;


public class BottomSheetFragment extends BottomSheetDialogFragment {


public BottomSheetFragment () {
// Required empty public constructor
}


@Override
public View onCreateView (LayoutInflater inflater, ViewGroups container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater. inflate (R. layout. fragment_bottom_sheet, container, false);
}

}

8- Then we have to complete the layout of this fragment.

9- It is worth mentioning that we use the content that was created in the previous project for bottom_sheet.xml in this project as well.

10- The codes that should be in the fragment_bottom_sheet.xml section are as follows:

<? 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 / bottom sheet"
android: background = "# 94aab4"
android: padding = "8dp"
app: behavior_hideable = "true"
app: behavior_peekHeight = "60dp"
app: layout_behavior = "@ string / bottom_sheet_behavior">

<TextView
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: textColor = "# 000000"
android: textSize = "20sp"
android: text = "Location: Austria "
android: paddingbottom = "10dp"
android: paddingtop = "10dp" />

<TextView
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: textColor = "# 452f2f"
android: textSize = "16sp"
android: text = " Austria initially emerged as a margraviate around 976 and developed into a duchy and archduchy. In the 16th century, Austria started serving as the heart of the Habsburg Monarchy and the junior branch of the House of Habsburg – one of the most influential royal dynasties in history. As an archduchy, it was a major component and administrative Centre of the Holy Roman Empire. Early in the 19th century, Austria established its own empire, which became a great power and the leading force of the German Confederation, but pursued its own course independently of the other German states following its defeat in the Austro-Prussian War in 1866. In 1867, in compromise with Hungary, the Austria-Hungary Dual Monarchy was established. "/>

</LinearLayout>

11- Then we have to write a Listener for the Expand button.

12- We will implement the project.

import android.support.v7.app.AppCompatActivity;
import android.os. Bundle;
import android. view. View;
import android. widget. Button;

public class MainActivity extends AppCompatActivity {

Button btnSheet;

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

btnSheet = findViewById (R.id.btn_expand);

btnPhoto.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {

BottomSheetFragment bottom Fragment = new BottomSheetFragment ();
bottomFragment.show (getSupportFragmentManager (), bottomFragment.getTag ());

}
});

}
}

What is the function of Bottom Sheets in Android and what are they used for?

13- We will see that like AlertDialog, this bar has elevation.

14- It is removed by pressing the back button or dragging it down.

15- We complete it as follows:

ارسال نظر برای این مطلب

کد امنیتی رفرش
اطلاعات کاربری
  • فراموشی رمز عبور؟
  • آرشیو
    آمار سایت
  • کل مطالب : 383
  • کل نظرات : 0
  • افراد آنلاین : 2
  • تعداد اعضا : 0
  • آی پی امروز : 40
  • آی پی دیروز : 20
  • بازدید امروز : 48
  • باردید دیروز : 28
  • گوگل امروز : 0
  • گوگل دیروز : 0
  • بازدید هفته : 178
  • بازدید ماه : 618
  • بازدید سال : 7,903
  • بازدید کلی : 57,336