I have a method that displays snackbars which is called from many different fragments. Here is the code of this method:
public static void showSnackBar(Activity activity, String message, View root) {
int duration = 5000;
Snackbar currentSnackBar = Snackbar.make( activity, root, message, Snackbar.LENGTH_INDEFINITE).setDuration(duration);
View sbView = currentSnackBar.getView();
sbView.setBackgroundColor(ContextCompat.getColor(activity, R.color.colorBlue));
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)sbView.getLayoutParams();
params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
sbView.setLayoutParams(params);
currentSnackBar.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_FADE);
currentSnackBar.show();
}
Currently it shows the snackbar at the top center of the display. I would like to have the following formatting modifications:
The textsize should be bigger
The text should be aligned in the center. At the moment it is aligned to the left.
Reminder: do you know how I can do this?
You can access the snacker textview by doing -
val textView =
sbView.findViewById<TextView>(R.id.snackbar_text)
Then set whatever properties you want, to this textview
Related
I have the following code to generate button objects with a set text, onClick command (more or less), height, width, and margins
private Button generateButton(String text, char command, int height, int width, int left, int top){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(getDP(width),getDP(height));
lp.setMargins(getDP(left),getDP(top),0,0);
Button button = new Button(this.getContext());
button.setText(text);
button.setLayoutParams(lp);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("Command",""+ command);
}
private char command;
private View.OnClickListener init(char var){
command = var;
return this;
}
}.init(command));
return button;
}
Along with this method to get the dp
private int getDP(int size){
return (int) (size * this.getContext().getResources().getDisplayMetrics().density);
}
But when I run the app the button have the proper height and width, along with the proper text and onclick action, but they have no margins, they're all bunched up into one corner
The view hierchy according to the Layout Inspector in Android studio goes
DecorView
LinearLayout
FrameLayout
FitWindowLinearLayout
ContentFrameLayout
CoordinatorLayout
ViewPager
ConstraintLayout
ConstraintLayout
Buttons
And from what I've read online, LayoutParams has to be from the same class as the layout, as is LinearLayout.LayourParams, or ConstraintLayout.LayoutParams and I've tried all the layout types that made sense to me and still no margins
It might be worth noting this is in a fragment
For views that are children of ConstraintLayout, you need to add horizontal and vertical constraints. Otherwise they layout will not know how to place them, and places them at the top left corner.
You can use the ConstraintLayout.LayoutParams, and set the constraints by
lp.leftToLeft, lp.leftToRight and similar methods here
So for example if you want to place these buttons one after the other vertically, you will constraint each button horizontally to the parent, and vertically to the previous button.
public void setMargins (int left, int top, int right, int bottom)
This is the signature for setMargins method. You call the method with right = 0 and bottom = 0. So you will not have the margins on right and bottom. Try to set some values for right and bottom.
You should use LayoutParams to set your button margins:`
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);
Depending on what layout you're using you should use RelativeLayout.LayoutParams or LinearLayout.LayoutParams.
And to convert your dp measure to pixel, try this:
Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
yourdpmeasure,
r.getDisplayMetrics()
);
`
Im having a bug that i cant understand the reason and how to resolve it. I belive that is a problem of layout/view/context refresh but i dont know.
I have a cell from a listView(I prefer recyclerView but the project has years) and in the corner of the cell i have a button to show more options. Programatically it just make an element View.GONE and another element View.VISIBLE.
I will attach code in a moment
To this button i setted too a listener that when i tap on it it do the opposite of below mentioned. It shows some elements and hide an entire LinearLayout from the cell. The elements are showed BUT the LinearLayout keeps on the screen like bugged. If i tap anywhere it disappears and if i try to tap on it it disappears too. Its like the view of that linear got bugged and keep in there like a ghost view. I will shop some pictures.
The cell normally at the beginning: https://imgur.com/1BjK0KP
The cell after i press the entire view to show the LinearLayout at the bottom of the cell: https://imgur.com/eONSptW
The cell after i press the arrow of the corner to hide the LinearLayout. Here it shows the view bugged https://imgur.com/jrT0qxV
The cell after i tap anywhere else https://imgur.com/XD1jN7U
public void expandView(View view){
final View cellView = view;
final LinearLayout editLinear = (LinearLayout) view.findViewById(R.id.cart_edit);
editLinear.setVisibility(View.VISIBLE);
final TextViewFont countText = (TextViewFont) view.findViewById(R.id.itemCount);
countText.setVisibility(View.GONE);
final TextViewFont total = (TextViewFont) view.findViewById(R.id.itemTotal);
final ImageView imageViewArrow = (ImageView) view.findViewById(R.id.cart_edit_image);
imageViewArrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arrow_up));
//notifyDataSetChanged();
imageViewArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
countText.setVisibility(View.VISIBLE);
editLinear.setVisibility(View.GONE);
imageViewArrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arrow_down));
}
});
First of all, you don't need to store context because in View you have the method view.getContext().
I recommend this article:
https://possiblemobile.com/2013/06/context/
On the other hand, ensure that you don't have a ghost view that is overlapping your image view.
I'm wondering, how can I get any current view property? For example, I create a button, change it's properties (color, height, etc) in code and I want to get it back as a string.
Is there any chance to get any property I want, such as: visibility, background, layout_width, layout_height, gravity, minWidth etc.
How can I do it quickly and should I use listener for this purpose?
If I understand you correctly, here is code, that allows you obtain some properties in string:
Button button = (Button) findViewById(R.id.button);
String visibility = String.valueOf(button.getVisibility());
String width = String.valueOf(button.getWidth());
String height = String.valueOf(button.getHeight());
Please note that getWidth() and getHeight() methods returns size in pixels (not dp).
You need to use the Listener action on a View and then fetch the properties that you would like to check when that view is clicked.
Example for a TextView and fetching it's properties upon a click on that TextView:
TextView tv = (TextView) findViewById(R.id.textView);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ColorDrawable cd = (ColorDrawable) tv.getBackground();
int colorCode = cd.getColor();//colorCode in integer
}
});
In case you want to access just the property of an View then you can just access it by calling the property you want to access, such as:
TextView tv = (TextView) findViewById(R.id.textView);
ColorDrawable cd = (ColorDrawable) tv.getBackground();
int colorCode = cd.getColor();//colorCode in integer
I have a Snackbar in need to set its height or set height to wrap content. Is there any way?
Snackbar snack = Snackbar.make(findViewById(R.id.activity_container), "Message", Snackbar.LENGTH_SHORT);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
view.setBackgroundColor(Color.RED);
tv.setTextColor(Color.WHITE);
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
We are going to provide multiple answers. First a statement or two! You can set the height and width of a Snackbar but it is messy and time consuming period.
One realization about a Snackbar widget is most tutorials do not talk about styling. The opinion is they should be just the size that the widget gives you NOT MY VIEW. So we have noticed that the text size and number of max lines plays a BIG roll is the size of a well styled Snackbar. So design your Snackbar and style away
OK how to implement the mess Suggestion DO NOT DO THIS declare this variable where you would declare any other variable in your Activity
RelativeLayout rl;
Then when you need to increase the size of your RelativeLayout that is in your XML file but is not the root Layout in this case use this code
rl = (RelativeLayout) findViewById(R.id.svRL);
rl.getLayoutParams().height = 1480;
When you get done with this increased size which can mess with the size of other object in the root Layout you might want to set the size of the root Layout back to what it was. In this case the root Layout was set to layout height 615dp we are working with a Nexus 7 Tablet. If you have not noticed this yet here is the MESS part that 1480 is in units of pixels and you need it in dp. I am sure the conversion can be made just do not ask me. So here is the set back line of code
rl.getLayoutParams().height = 1230;
Now for a easy way to design and style two types of Snackbar's one with an Action button and one with out. First you need a CoordinatorLayout in what ever Activity corresponding XML file that looks like this Note it has an id
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coorSB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
<!-- android.support.design.widget.SnackBar -->
<!--stuff you want inside the coordinator ... -->
</android.support.design.widget.CoordinatorLayout>
Now we are ready to do some work in the Activity to design and style after a little advanced string and color set up. Please do not be offended I am being very thorough because you seem to be very new to programming.
<string name="snackbar_text">I Am a NEW SnackBAR TEXT</string>
<string name="snackbar_action">EXIT</string>
<string name="second_text">Second Text</string>
<string name="csb_text">I am the Custom Guy</string>
<string name="csb_action">EXIT</string>
<string name="the_text">Password must have one Numeric Value\n"
"One Upper & Lower Case Letters\n"
"One Special Character $ # ! % * ? &\n"
"NO Spaces in the PASSWORD"</string>
Now for the Rainbow many ways to manage Color this is my mine.
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303f9f</color>
<color name="colorAccent">#FF4081</color>
<color name="color_Black">#000000</color>
<color name="color_White">#FFFFFF</color>
<color name="color_darkGray">#606060</color>
<color name="color_lightGray">#C0C0C0</color>
<color name="color_super_lightGray">#E0E0E0</color>
<color name="color_Red">#FF0000</color>
<color name="color_Yellow">#FFFF66</color>
<color name="color_deepBlue">#0000ff</color>
<color name="color_lightBlue">#3333FF</color>
<color name="color_Purple">#9C27B0</color>
<color name="color_Transparent">#android:color/transparent</color>
Done with house keeping in your Activity where you declare variables add this
private CoordinatorLayout myLayout;
Snackbar sb = null;
private CoordinatorLayout noActLayout;
Snackbar sbNoAct = null;
There here is the implementation of both types of Snackbars
public void makeNoAct(View view){
// this is declared on a Button android:onClick="makeNoAct"
noActLayout = (CoordinatorLayout)findViewById(R.id.coorSB);
sbNoAct = Snackbar.make(noActLayout,R.string.the_text,1);// any interger will make it happy
sbNoAct.setDuration(3000);// 3 sec // OR Snackbar.LENGTH_LONG
// matters NOT you are setting duration
View sbView = sbNoAct.getView();
sbView.setBackgroundColor(ContextCompat.getColor(this, R.color.color_Black));
TextView textViewNoAct = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
//set text color
textViewNoAct.setTextColor(ContextCompat.getColor(this,R.color.color_Yellow));
textViewNoAct.setMaxLines(10);
textViewNoAct.setTextSize(24);
//increase max lines of text in snackbar. default is 2.
sbNoAct.show();
int height = sbView.getHeight();
etNewData.setText(String.valueOf(height));
}
public void makeCOOR(View view) {
// this is declared on a Button android:onClick="makeCOOR"
// We were to Lazy to write an OnClickListener
myLayout = (CoordinatorLayout) findViewById(R.id.coorSB);
sb = Snackbar.make(myLayout, R.string.csb_text, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.csb_action, myOnClickListener)
.setActionTextColor(ContextCompat.getColor(context, R.color.color_Red));
View sbView = sb.getView();
sbView.setBackgroundColor(ContextCompat.getColor(this, R.color.color_White));
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
//set text color
textView.setTextColor(ContextCompat.getColor(this,R.color.color_deepBlue));
textView.setTextSize(30);
//increase max lines of text in snackbar. default is 2.
textView.setMaxLines(10);
// NOTE new View
TextView textAction = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_action);
//set Action text color
textAction.setTextColor(ContextCompat.getColor(this,R.color.color_Red));
textAction.setTextSize(30);
sb.show();
}
View.OnClickListener myOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// OR use and Intent to go somewhere have a nice trip
sb.dismiss();
System.out.println("========= I WAS DISMISSED ===============");
}
};
Enjoy the code and let us know with a comment if this solves your issue.
This is very simple to change the height or width of Snackbar .
Just we need to write 2 , 3 line of code to do this.
Check the below code snippet .
Snackbar snackbar = Snackbar.make(view, "Your message", Snackbar.LENGTH_LONG);
snackbar.setAction("Ok", new View.OnClickListener() {
#Override
public void onClick(View view) {
//your click action.
}
});
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)snackbar.getView();
layout.setMinimumHeight(50);//your custom height.
snackbar.show();
final String CR= System.getProperty("line.separator") ;
String snackMsg= "First line" + CR;
snackMsg+="Second line." +CR;
snackMsg+="... more lines." +CR;
final Snackbar snack = Snackbar.make(findViewById(android.R.id.content), snackMsg, Snackbar.LENGTH_INDEFINITE);
snack.setAction("OK", new View.OnClickListener() {
#Override
public void onClick(View v) {
// Respond to the click, such as by undoing the modification that caused
// this message to be displayed
}
});
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
// tv.setBackgroundColor(Color.RED);
tv.setLines(12);
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
//params.height=2000;
params.bottomMargin=10;
view.setLayoutParams(params);
snack.show();
You may also need to set the size for the inner container of a snackbar in order for the text to be aligned/centered vertically. Here is a solution (Kotlin):
Snackbar.make( containerView, msg, duration ).also {
// outer container
it.view.minimumHeight = minHeightPx
// inner container
( it.view as? Snackbar.SnackbarLayout )?.getChildAt( 0 )?.let { innerView ->
innerView.minimumHeight = minHeightPx
}
}.show()
Snackbar snack = Snackbar.make(findViewById(R.id.activity_container), "Message", Snackbar.LENGTH_SHORT);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
params.height = 80;
tv.setTextColor(Color.WHITE);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setLayoutParams(params);
snack.show();
I've been working on a system overlay and I found that moving my imageview around the screen is a hassle using WindowManager.LayoutParams x and y, so I've decided to make a RelativeLayout with the height of the device to hold my imageview. The only problem is I want to make the Layout non clickable so that the activity below it can be clicked, but not the imageview itself as it launches an activity. Is there anyway to do this? Here is my code so far
final RelativeLayout floaterLayout = new RelativeLayout(getApplicationContext());
//this layout makes the relativelayout non clickable so that the activity below it can be clicked but it doesn't allow any children to be clicked either.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN + WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL + WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
params.height = 2560;
params.y = 0;
floaterLayout.setBackgroundColor(Color.RED);
floaterLayout.setAlpha(0.5f);
floaterLayout.setLayoutParams(params);
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).addView(floaterLayout, params);
ImageView view = new ImageView(getApplicationContext());
Util.setImageDrawable(view, R.drawable.floater_dots);
floaterLayout.addView(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
#Nicolas you are creating a relative layout that's not added to your parent layout.Simply create a xml with a relativeLayout and an imageView. Set that imageView's onClickListener in java code. Through this the parent layout will be nonclickable and only imageview will be clickable