I have added layout_anchor property on fab in layout xml. After clicking a button the fab should position on end of the screen. To achieve that I am thinking to add gravity property. But how do i remove layout_anchor property programmatically.
layout.xml
<com.samsoft.sam.Fab
android:id="#+id/fab"
app:layout_anchor="#id/asd"
app:layout_anchorGravity="top|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The above is my fab code and it positions perfectly, now I need to replace layout anchor property with android:gravity="top|end" programmatically. Please help me.
I assume you mean you want the FloatingActionButton's layout_gravity to end up as top|end.
You can do this by getting the FloatingActionButton's CoordinatorLayout.LayoutParams, clearing the anchor's ID, setting the gravity field as needed, and then setting the LayoutParams back on the FloatingActionButton.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.setAnchorId(View.NO_ID);
lp.gravity = Gravity.TOP | GravityCompat.END;
fab.setLayoutParams(lp);
((CoordinatorLayout.LayoutParams) floatingActionButton.getLayoutParams()).setAnchorId(View.NO_ID);
This is the code you are looking for. Happy coding :)
Related
I'm trying to implement android.support.v7.widget.SearchView with a collapseIcon on the Toolbar. Everything works fine i.e, the SearchView is working as expected but I'm unable to remove/ reduce the padding between the SearchView and collapseIcon, as visible in the attached screenshot.
Any ideas on how can I do that?
I've tried :
app:contentInsetStartWithNavigation="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
but this doesn't work.
The problem can be solved by setting the leftMargin = 0 in the layout parameters associated with the following LinearLayout in the SearchView:
(To get the layout parameters use the getLayoutParams)
LinearLayout searchEditFrame = (LinearLayout) searchView.findViewById(R.id.search_edit_frame); // Get the Linear Layout
// Get the associated LayoutParams and set leftMargin
((LinearLayout.LayoutParams) searchEditFrame.getLayoutParams()).leftMargin = 0;
I managed to solve the problem by looking at the xml file that's used by SearchView, so the padding can be removed/reduced by changing the leftMargin value:
layoutParams.leftMargin = 0;
for the following LinearLayout inside the SearchView:
LinearLayout searchEditFrame = (LinearLayout) searchView.findViewById(R.id.search_edit_frame);
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 ImageView and TabLayout(4 Tabs) inside CollapsingToolbarLayout, Now i want to collapse Appbar when clicking on Tabs(2,3,4) and for first tab it should work normally(as per scrolling). Is there a way to expand and collapse Appbar programmatically?
however i have seen solution, appBarLayout.setExpanded(false) collapses Appbar but again it is able to drag down. i want to prevent AppBar Expansion until Tab 1 is clicked?
Use mAppBarLayout.setExpanded(true) to expand Toolbar and use mAppBarLayout.setExpanded(false) to collapse Toolbar.
If you want to prevent CollapsingToolbarLayout expansion until
Tab 1 is clicked then you should use mAppBarLayout.setLayoutParams(params) programmatically to change
CollapsingToolbarLayout height.
Collapse: Use when Tabs(2,3,4) clicked
CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*80; // COLLAPSED_HEIGHT
mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(false);
Expand: Use when Tab 1 clicked
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*200; // EXPANDED_HEIGHT
mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(true);
Hope this will help you~
I have a floating action button. I want to change its gravity from right to left with help of my java code.
There is no any such methods to set the floating action button in left side. You can look at the http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html
If you are using RelativeLayout as parent you can set the gravity like this,
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_gravity="start|bottom"
If you are using CoordinatorLayout you can set de gravity like this,
app:layout_anchorGravity="top|start"
I met the problem like you. And I solved it using LayoutParams. Here is the code.
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) this.mFab.getLayoutParams();
layoutParams.gravity = Gravity.START | Gravity.BOTTOM;
this.mFab.setLayoutParams(layoutParams);
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);