I'm dynamically injecting a content view into the CoordinatorLayout and would now like to apply a layout_below property to the injected view so that it isn't hidden behind the AppBar.
Is there any way to do this at runtime from code instead of xml properties of the annotation?
Taking one step back and building the entire view in plain xml, I realized that layout_below is not the property I needed for my use case: placing the content view below the app bar. I did not make this clear in my question though, as I assumed layout_below would be the proper option for that.
In fact, to insert a non scrolling view into the CoordinatorLayout it should first be wrapped with a android.support.v4.widget.NestedScrollView. Then, to avoid its content being hidden behind the app bar, it is necessary to update its behavior to android.support.design.widget.AppBarLayout.ScrollingViewBehavior. Otherwise a default behavior is used which will hide behind the app bar.
val viewToInsert = getLayoutInflater.inflate( id, coordinatorWrapper, false )
val p = viewToInsert.getLayoutParams.asInstanceOf[Coordinator.LayoutParams]
p.setBehavior( new ScrollingViewBehavior )
coordinatorWrapper.addView( viewToInsert, 1, p )
You could try setting an anchor.
ContentView view = getContentView(); //your view
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
params.setAnchorId(R.id.app_bar_layout);
params.anchorGravity = Gravity.BOTTOM; //we will anchor to the bottom line of the appbar
params.gravity = Gravity.BOTTOM; //we want to be BELOW that line
view.setLayoutParams(params);
Related
So suppose I have a textview called tvPlaceholder with the following property android:layout_alignParentEnd="true". How would I go about setting this to be false in the code (Kotlin)? I know you can change things like visibility, text size, e.t.c. in the code but could not find a definitive answer for layout_alignParentEnd. I am also calling tvPlaceholder from a viewholder if that matters at all: viewholder.tvPlaceholder.
When a view is added to a parent, it adds a LayoutParams object that defines any rules for laying it out. You can get this object by calling getLayoutParams(). Each layout parent has their own layout params subclass, I'm assuming this is a relative layout. So for a RelativeLayout, the LayourParams has a function removeRule that can do this.
val params = view.getLayoutParams() as RelativeLayout.LayoutParams
params.removeRule(RelativeLayout.ALIGN_PARENT_END)
I have a CollapsingToolbar that I have conditionally disabled. When the user loads the view under that condition, it just looks like a normal ToolBar object.
The only weird thing is that if they drag down, such as in a pull to refresh style action, the CollapsingToolbar expands, despite my wishes and code to the contrary!
Here is what I have, and the commented out code reflects what I have also tried
appBar.setExpanded(false);
appBar.setActivated(false);
/*CollapsingToolbarLayout.LayoutParams p = (CollapsingToolbarLayout.LayoutParams)toolbar.getLayoutParams();
p.setCollapseMode(CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_PIN);
toolbar.setLayoutParams(p);
CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
appBarLayoutParams.setBehavior(null);
appBar.setLayoutParams(appBarLayoutParams);*/
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBar.getLayoutParams();
lp.height = (int) getResources().getDimension(R.dimen.app_bar_height);
I want to disable the drag down expansion, and I didn't see a way to do it. This activity contains a recyclerview and that is what users primarily interact with.
I was closely with method. Expand layout behavior controls AppBarLayout. So you need update expand methods of that widget. Based on AppBarLayout Doc.
AppBarLayout appBarLayout;
//....
public void setExpandToolbar (boolean isEpand) {
if (isExpand) {
appBarLayout.setExpanded(true,true);
}
else {
appBarLayout.setExpanded(false,true);
}
}
Let me know, about this issue. Because you can control expand via other solution. For example, by custom layout params.
AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams)
toolbar.getLayoutParams();
p.setScrollFlags(0);
toolbar.setLayoutParams(p);
To prevent scrolling of RecyclerView or NestedScrollView from expanding or collapsing the CollapsingToolbarLayout.
// scrollView can be RecyclerView or NestedScrollView
ViewCompat.setNestedScrollingEnabled(scrollView, false)
https://code.luasoftware.com/tutorials/android/how-to-disable-or-lock-collapsingtoolbarlayout-collapse-or-expand/
In kotlin looks like this: yourRV.isNestedScrollingEnabled = false
I have a LinearLayout that contains several views - when I add or remove it from my view I used the default LayoutTransition.
I'm adding the view to my AppbarLayout - and I added the animations programmatically by setting a new LayoutAnimation on the appbar before adding the view and setting it to null after the view is added - I don't do it using the tag in the xml due to https://code.google.com/p/android/issues/detail?id=191170
The problem is that when i remove it from the view the default animation is done in two parts
all the views inside immediately disappear
the closing of the view is animated from bottom to top
Which change is needed to the LayoutTransition in order to have only the 2nd animation where the view is closed from bottom to top?
CommonsWare answer
Modify its LayoutParams to move it to the end position. Use
getLayoutParams() on the View, cast it to the appropriate type based
on its container, modify the LayoutParams object, then call
setLayoutParams() on the View to commit the changes.
Try this:
final LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutTransition transition = container.getLayoutTransition();
transition.disableTransitionType(LayoutTransition.APPEARING);
transition.disableTransitionType(LayoutTransition.DISAPPEARING);
Follwing the new android snackbar, i'm trying to set it to be positioned on a specific y coordinate. Its seems to be not even a possible.
I've tried with getting the parent of the snackbar's view, but, there's nothing to be done to the parent for it to set the position of it.
mSnackBar.getView().getParent();
When digging into the actual class, there's an instance of mView, and mParent which is private, and can't be reached anyhow.
https://developer.android.com/reference/android/support/design/widget/Snackbar.html
It is possible to set the location that the Snackbar is displayed by positioning a android.support.design.widget.CoordinatorLayout within your existing Activity layout.
For example, say your existing layout is a RelativeLayout you could add a CoordinatorLayout as follows:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/myCoordinatorLayout"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.design.widget.CoordinatorLayout>
Then, make sure you pass the CoordinatorLayout as the first argument of the Snackbar.make() command.
final View viewPos = findViewById(R.id.myCoordinatorLayout);
Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_action_undo, showListener)
.show();
This will result in the Snackbar being shown at the bottom of the CoordinatorLayout provided to the make() function.
If you pass a View that is not a CoordinatorLayout the Snackbar will walk up the tree until it finds a CoordinatorLayout or the root of the layout.
Defining a layout explicitly for the Snackbar may not always be practical.
The issue seems to have been targeting the parent, rather than the Snackbar.
Snackbar snackbar = Snackbar.make(layout, message, duration);
View snackbarLayout = snackbar.getView();
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
// Layout must match parent layout type
lp.setMargins(50, 0, 0, 0);
// Margins relative to the parent view.
// This would be 50 from the top left.
snackbarLayout.setLayoutParams(lp);
snackbar.show();
I have an interesting idea for you.
You can just use the code below to get where you want.
Snackbar.make(View,Message , Snackbar.LENGTH_LONG)
.setAnchorView(pb)
.show();
Here setAnchorView is used to change the postion. You can create your layout in the xml file. Just call your layout inside the setAnchorView and it will show on the position of the xml layout.
You can use setAnchorView (#IdRes int anchorViewId) method of a snackbar to set it above anchor view
https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar#make(android.view.View,%20java.lang.CharSequence,%20int)
In addition to brentm's answer, I had to use:
androidx.constraintlayout.widget.ConstraintLayout
and add the dependency for it:
implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"
I have a custom view which will be jar'ed up and added into another project. In the view I want to give an option of a button.
Here is what I have in the CustomView class.
final CustomView currentView = (CustomView) findViewById(this.getId());
RelativeLayout.LayoutParams params = (new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT ));
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
closeButton.setLayoutParams(params);
currentView.addView(closeButton);
This is all wrapped in a RelativeLayout Tag as well as the other objects of the application
Everything compiles however in the CustomView the Button is aligning left instead of right.Any Ideas???
I would guess the problem is your CustomView. It probably doesn't take the entire width of the window, and is just wide enough to fill its children (which, in your case, is the close button). Make sure your CustomView has a fill_parent horizontal layout.
Since your CustomView extends WebView, which, in turn, extends AbsoluteLayout, you can't expect it to handle RelativeLayout's parameters. Instead, it's best you put your customview and your close button inside a RelativeLayout and position them properly.
When adding your closeButton to your currentView you need to supply the LayoutParams as an argument as well in order for them to take effect.
Basically, switch
currentView.addView(closeButton);
with
currentView.addView(closeButton, params);
Since the width of your button is set to wrap_content, you could also try setting its layout_gravity to right.
params.gravity = Gravity.RIGHT;