Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create FloatingActionButton programmatically (without xml)

I am appreciating the FloatingActionButton (fab) feature of Android and want to use them in a lot of different places in my project.

Right now, I have something like this, where I have several xml specifications for them, all of them are the same, except the id, icon and the onclick.

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fabFoo"
    android:onClick="onFabFoo"
    android:src="@drawable/ic_foo" 
    app:backgroundTint="?attr/colorButtonNormal"
    app2:elevation="2dp"
    app:fabSize="mini" 
    android:focusable="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_margin="2dp"
    app:rippleColor="?attr/colorSwitchThumbNormal" />

In the interest of avoiding duplicate code... Is there a way to create the fab entirely programmatically without needing to specify it in xml?

...

Trying out some suggestions... There was no 'setSize' until I upgraded the SDK to current (# 25)

FloatingActionButton fab = new FloatingActionButton(this);
fab.setId(View.generateViewId());
fab.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      Log.d("DEBUG", "onFabFoo");
   }
});
fab.setImageResource(R.drawable.ic_foo);
fab.setElevation(2);
fab.setSize(android.support.design.widget.FloatingActionButton.SIZE_MINI);
fab.setFocusable(true);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lay.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lay.setMargins(2,2,2,2);
fab.setLayoutParams(lay);

Haven't yet figured out how to set the colors

//  app:backgroundTint="?attr/colorButtonNormal"
//  app:rippleColor="?attr/colorSwitchThumbNormal"

I see there are methods to set these (setBackgroundTintList and setRippleColor) but I don't see how to set it to the colors I chose in the original xml setting (colorButtonNormal and colorSwitchThumbNormal)

Also, don't know how to attach it to the parent and get it to display...

Okay, I guess I realize now, if you do all this programmatically, then you can't use features like the xml Design view in Android Studio. So it's much harder to work with.

like image 735
slashdottir Avatar asked Mar 06 '17 19:03

slashdottir


People also ask

How do I add a floating action button?

Add the floating action button to your layoutThe size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.

Does iOS have floating action button?

Sorry, But unfortunately iOS doesn't support such kind of development. You can add the Floating button inside the app but you can't add that over other apps.

Which library can you use to implement a FloatingActionButton?

Floating Action Button using Fab Option Library in Android.


2 Answers

There are two I can think of

Using java only

Create a FloatingActionButton directly in code like

public FloatingActionButton getFab(Context context) {
    FloatingActionButton fab = new FloatingActionButton(context);
    ...
    return fab;
}

Inflating the layout

public FloatingActionButton getFab(Context context, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    return (FloatingActionButton) inflater.inflate(R.layout.myfab, parent, false);
}

More about inflater

Edit:

You can use setBackgroundTintList and setRippleColor to set the 2 attributes.

And to attach it to parent you do

layout.addView(v);

But I feel using LayoutInflater is better because it does both tasks of generating a FloatingActionButton and attaching it to its parent.

inflater.inflate(R.layout.myfab, layout, true)
like image 138
Shreyash S Sarnayak Avatar answered Oct 07 '22 07:10

Shreyash S Sarnayak


We can achieve to create a Floating action button in Programmatically

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_relative_layout">
</RelativeLayout>

This is the main xml layout file Not inside this parent layout file we can crate floating action button with the following code in class file.

public class MyClass extends AppCompatActivity{
    RelativeLayout relativeLayout;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_name);

        relativeLayout = (RelativeLayout) findViewByID(R.id.my_relative_layout);

        FloatingActionButton fab = new FloatingActionButton(getContext());
        fab.setId(R.id.fab_location_main);
        fab.setLayoutParams(new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
                ));
        relativeLayout.addView(fab);
    }
}

Now

like image 31
Suresh Chaudhari Avatar answered Oct 07 '22 06:10

Suresh Chaudhari