SoFunction
Updated on 2025-04-09

How to specify the location of SnackBar on the screen in Android and solve the problem

Android specifies the location of SnackBar on the screen

Snackbar often appears under the phone screen or at the bottom left of the desktop in the form of a small pop-up box, and is at the top of all layers of the screen. If you want to specify where it appears on the screen, you can place the SnackBar inside.

Add CoordinatorLayout to RelativeLayout as follows:

<
 android:layout_width="match_parent"
 android:layout_height="200dp"
 android:
 android:layout_alignParentTop="true"
 android:layout_alignParentLeft="true"
 android:layout_alignParentStart="true">
</>

Calling() takes CoordinatorLayout as the first parameter, as follows:

final View viewPos = findViewById(); 
(viewPos, .snackbar_text, Snackbar.LENGTH_LONG)
       .setAction(.snackbar_action_undo, showListener)
       .show();

You can change the position of the Snackbar by changing the position of the CoordinatorLayout, such as setting android:paddingBottom="16dp".

A little problem with the display of Snackbar location

compile ':design:23.4.0'

Android Studio new project will have a method to display Snackbar by default, which has never been useful. I saw a WeChat official account two days ago
/p/996dc15d21be This article. It is well written, very comprehensive, with basic functions and style customization.

Some of them are written to the custom display position, which feels very necessary, because this is different from Toast. Toast will be displayed on the soft keyboard, and this will be blocked by the soft keyboard.

 vl = ();
         cl = new (,);               
         = ;//Set the display position center        (cl);

But when setting the position to


Setting the offset distance but not taking effect

Rect frame = new Rect();
(frame);
 =;

It turns out that some View parent inherits FrameLayout needs to be noted.

 l = () ();

Of course, it is very troublesome to write and display on it yourself, and you also need to deal with display and hide animations.

Here I amanimateViewIn() andanimateViewout() There are regulations in it

There is a very good one on Github /AndreiD/TSnackBar

It is done according to the system, and then many small functions are added. Remember first, if the project is used, download it to change it

There is a sentence in design_layout_snackbar.xml

android:layout_gravity="bottom"

Remember to change this to top

This will show it above.

Then there is the corresponding animation.design_snackbar_in and.design_snackbar_out

But both of them are

.SDK_INT <Build.VERSION_CODES.ICE_CREAM_SANDWICH//(14)

Called at the time, so you can delete it without changing it

Just saw the systemfinal void showView()methodpublic void onDismiss(View view)There is a line

();

But I deleted this sentence in Tsnackbar, okay, it seems that I need to pay attention to whether there is any other big difference.

Summary: If it is displayed on the top for a long time, you can use Github projects, otherwise you can use the design library.

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.