I don't want to use normal or mini size of FAB. So, i'm trying to set width and height of a FAB by using LayoutParams but setLayoutParams is not working. I tried the same with ImageButton since FAB extends ImageButton and it worked for ImageButton. Can someone tell me why this is happening?
final GridLayout.LayoutParams lp = (GridLayout.LayoutParams) mFab1.getLayoutParams();
ViewTreeObserver vto = mFab2.getViewTreeObserver();
ViewTreeObserver.OnGlobalLayoutListener listener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
lp.height = mFab2.getHeight();//Height is ready
lp.width = mFab2.getWidth(); //Width is ready
Log.d(LOG_TAG, (String.valueOf(mFab2.getWidth())) + " and " + (String.valueOf(mFab2.getHeight())));
}
} ;
vto.addOnGlobalLayoutListener(listener);
mFab1.setLayoutParams(lp);// Works here if mFab1 is ImageButton
mFab1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.d(LOG_TAG, "Outside "+(String.valueOf(mFab1.getWidth())) + " and " + (String.valueOf(mFab1.getHeight())));
if(mFab1.getWidth()==0){
mFab1.setLayoutParams(lp);//trying to set LayoutParams again
Log.d(LOG_TAG, "Inside "+(String.valueOf(mFab1.getWidth())) + " and " + (String.valueOf(mFab1.getHeight())));
}
}
});
I'm using v7 GridLayout and in xml both height and width of mFab1 is set zero because I'm using weights. Since column span is 2 and layout_gravity is set as "center_horizontal" therefore, setting width to zero will not work that is why I want to set the width in the java code and it works very well for ImageButton.
<android.support.v7.widget.GridLayout 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"
app:columnCount="2"
app:rowCount="6"
tools:context=".fragment.MainNavigationActivityFragment"
>
<ImageButton
android:id="@+id/mFab2"
app:layout_rowWeight="1"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/ic_home"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@drawable/home_bg"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"/>
<ImageButton
app:layout_rowWeight="1"
android:id="@+id/mFab3"
app:layout_columnSpan="1"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/ic_home"
android:background="@drawable/home_bg"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" />
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:id="@+id/button1"
android:text="Some Text"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/transparent"
android:textColor="@color/colorPrimary"
android:fontFamily="roboto-condensed"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:id="@+id/button2"
android:text="Some Text"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="roboto-condensed"
android:textColor="@color/colorPrimary"
android:background="@color/transparent"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<ImageButton
app:layout_rowWeight="1"
android:id="@+id/mFab3"
android:textColor="#FFFFFF"
android:textSize="30sp"
app:layout_columnSpan="1"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/ic_home"
android:background="@drawable/home_bg"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<ImageButton
app:layout_rowWeight="1"
android:id="@+id/mFab4"
app:layout_columnSpan="1"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/ic_home"
android:background="@drawable/home_bg"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" />
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:id="@+id/button3"
android:text="Some Text"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/transparent"
android:textColor="@color/colorPrimary"
android:fontFamily="roboto-condensed"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="fill_horizontal"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
android:id="@+id/button4"
android:text="Some Text"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="roboto-condensed"
android:textColor="@color/colorPrimary"
android:background="@color/transparent"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" />
<android.support.design.widget.FloatingActionButton
app:layout_rowSpan="1"
app:layout_rowWeight="1"
android:id="@+id/mFab1"
app:layout_columnSpan="2"
app:layout_gravity="center_horizontal"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
app:backgroundTint="@color/colorPrimary"
android:src="@drawable/icon"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
/>
<Button
app:layout_rowSpan="1"
app:layout_rowWeight="0.2"
app:layout_gravity="center_horizontal"
app:layout_columnSpan="2"
app:layout_columnWeight="1"
android:id="@+id/button5"
android:text="Some Text"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:fontFamily="roboto-condensed"
android:textColor="@color/colorPrimary"
android:background="@color/transparent"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp" />
</android.support.v7.widget.GridLayout>