Create FloatingActionButton programmatically (without xml)

11,978

Solution 1

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)

Solution 2

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

Share:
11,978
slashdottir
Author by

slashdottir

a time for cats a time for lulz a time for trolls a time to bait trolls a time to gather cats together

Updated on June 16, 2022

Comments

  • slashdottir
    slashdottir almost 2 years

    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.

  • slashdottir
    slashdottir about 7 years
    The layout inflator does not work because a FloatingActionButton is not a layout.
  • Shreyash S Sarnayak
    Shreyash S Sarnayak about 7 years
    I tested with layout inflator it worked. I put exact same xml (only FAB no layout) as you posted in the question.
  • Shreyash S Sarnayak
    Shreyash S Sarnayak about 7 years
    Getting theme attributes programmatically doesn't look easy. See this answer
  • slashdottir
    slashdottir about 7 years
    Hm, ok. Well, when I try it it doesn't compile, it just says: Expected resource of type layout. If I move the xml that defines the fab to its own file, it still does not recognize it as a layout. The only way to access it is by R.id.fabFoo, there is no identifier called R.layout.fabFoo
  • slashdottir
    slashdottir about 7 years
    Ok, I see. If you want to call it by R.layout.fabFoo, the actual layout file must be named 'fabFoo.xml'. Android programming API is so inconsistent, I'm amazed anyone is able to keep up with all the endless non-intuitive quirks.
  • Shreyash S Sarnayak
    Shreyash S Sarnayak about 7 years
    Lol. Ok. By the way, I think capital letter in name fabFoo.xml is not allowed.
  • slashdottir
    slashdottir about 7 years
    Almost there, so now how do I get a handle to the parent layout? I tried findViewById, but it requires a ViewGroup, not a view.
  • Shreyash S Sarnayak
    Shreyash S Sarnayak about 7 years
    Layout is nothing but a ViewGroup. So whichever layout you have (LinearLayout, RelativeLayout, etc.) can be used. Do findViewById of the layout in which you want to put this FAB and type cast it to ViewGroup. Example - vg = (ViewGroup) findViewById(...)
  • slashdottir
    slashdottir about 7 years
    You have gone over and above to help me. Thank you!