Search code examples
androidmenufloating-action-button

How to attach floating action menu to an already existing floating action menu?


I am trying to make one of my Floating Action Buttons have multiple choices.

I have a Floating action button on the side of my screen.

When I press it I get three buttons pop out, first is the search button, second is the add button and third is the settings button. image 1

When I press the add button I want it to have multiple buttons appear. image 2

I have looked on SO and other sites like Android Arsenal and YouTube for tutorials but haven't found anything.

I have had a shot at writing the code but it is not working even though it builds with no errors.

I am using com.oguzdev:CircularFloatingActionMenu:1.0.2 for the FAB library if it helps

Image 1

Image 2

Here is my code:

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private String[] navDrawerItemTitles;
    private DrawerLayout drawerLayout;
    private ListView listView;
    Toolbar toolbar;
    public static final String TAG_ADD = "Add";
    private CharSequence drawerTitle;
    private CharSequence title;
    android.support.v7.app.ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Window window = this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this, R.color.Green));
        title = drawerTitle = getTitle();
        navDrawerItemTitles = getResources().getStringArray(R.array.nav_d_items);
        drawerLayout = (DrawerLayout) findViewById(R.id.mainDrawer);
        listView = (ListView) findViewById(R.id.left_drawer);

        setupToolbar();

        // ToolBox Image
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.drawable.hammers_green);
        // Settings
        ImageView SettingsImagView = new ImageView(this);
        SettingsImagView.setImageResource(R.drawable.settings_green);
        // Add Button
        ImageView AddButton = new ImageView(this);
        AddButton.setImageResource(R.drawable.plus_green);
        // Search Button
        ImageView SearchImage = new ImageView(this);
        SearchImage.setImageResource(R.drawable.searches);
        // Tag Green
        ImageView TagGreen = new ImageView(this);
        TagGreen.setImageResource(R.drawable.tag_green_circle);
        // Tag Grey
//        ImageView TagGrey = new ImageView(this);
//        TagGrey.setImageResource(R.drawable.);
        // Tag Blue
        ImageView TagBlue = new ImageView(this);
        TagBlue.setImageResource(R.drawable.tag_blue_circle);
        // Tag Red
//        ImageView TagRed = new ImageView(this);
//        TagRed.setImageResource(R.drawable.);

        // Layout Parameters for the Toolbox Button
        FloatingActionButton.LayoutParams layoutParams = new FloatingActionButton.LayoutParams(300, 300);


        FloatingActionButton actionButton = new FloatingActionButton.Builder(this)
                .setContentView(imageView)
                .setLayoutParams(layoutParams)
                .setPosition(3)
                .build();

        // SubActionBuilder for Initial Toolbox Button Click
        SubActionButton.Builder ToolboxButtons = new SubActionButton.Builder(this);
        SubActionButton Settings = ToolboxButtons.setContentView(SettingsImagView).build();
        SubActionButton Add = ToolboxButtons.setContentView(AddButton).build();
        SubActionButton Search = ToolboxButtons.setContentView(SearchImage).build();

        // SubActionBuilder for Add Button Click
        SubActionButton.Builder AddButtonSelections = new SubActionButton.Builder(this);
        SubActionButton TagGreenButton = AddButtonSelections.setContentView(TagGreen).build();
        SubActionButton TagBlueButton = AddButtonSelections.setContentView(TagBlue).build();
        Add.setOnClickListener(this);
        Add.setTag(TAG_ADD);


        FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(Settings, 200, 200)
                .addSubActionView(Add, 400, 400)
                .addSubActionView(Search, 200, 200)
                .setRadius(400)
                .attachTo(actionButton)
                .build();



        FloatingActionMenu actionMenu1 = new FloatingActionMenu.Builder(this)
                .attachTo(Add)
                .addSubActionView(TagGreenButton, 150, 150)
                .addSubActionView(TagBlueButton, 150, 150)
                .setRadius(200)
                .build();





        DrawerObjectDefiner[] drawerObjectDefiners = new DrawerObjectDefiner[5];

        drawerObjectDefiners[0] = new DrawerObjectDefiner(R.drawable.house, "Home");
        drawerObjectDefiners[1] = new DrawerObjectDefiner(R.drawable.import_export_green, "Import/Export");
        drawerObjectDefiners[2] = new DrawerObjectDefiner(R.drawable.photo_green, "Pictures");
        drawerObjectDefiners[3] = new DrawerObjectDefiner(R.drawable.settings_icon, "Settings");
        drawerObjectDefiners[4] = new DrawerObjectDefiner(R.drawable.qustion_mark_green, "Help");

        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(true);

        DrawerCustomAdapter adapter = new DrawerCustomAdapter(this, R.layout.list_view_item_row, drawerObjectDefiners);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new DrawerItemClickListener());
        drawerLayout = (DrawerLayout)findViewById(R.id.mainDrawer);
        drawerLayout.addDrawerListener(drawerToggle);
        setupDrawerToggle();


    }

    @Override
    public void onClick(View view) {
        if (view.getTag().equals(TAG_ADD)){
            actionMenu();
        }


    }

activtiy_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android= "http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainDrawer"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/Green">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/container_toolbar"
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_height="0dp"
                android:layout_width="0dp" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="38dp"
                android:layout_height="31dp"
                app:srcCompat="@drawable/menu_green"
                android:layout_marginLeft="8dp"
                app:layout_constraintLeft_toLeftOf="@+id/toolbar"
                android:layout_marginRight="8dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/toolbar"
                android:layout_marginBottom="8dp"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintVertical_bias="0.555"
                android:contentDescription="@string/Menu"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp" />

        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

                <ListView
                android:layout_width="400dp"
                android:layout_height="596dp"
                android:layout_marginRight="8dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginLeft="8dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="8dp"
                app:layout_constraintHorizontal_bias="0.562"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    app:layout_constraintVertical_bias="0.489" />

        </android.support.constraint.ConstraintLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/White"
        android:choiceMode="singleChoice"
        android:divider="@color/Green"
        android:dividerHeight="1dp">

    </ListView>
</android.support.v4.widget.DrawerLayout>

If you need any other info, please feel free to ask.


Solution

  • So I figured it out.

    All i needed to do was change the buttons to public vars and place the Floating Action Menu method that handled the add button in to the add buttons onclicklistener.

    Like this:

    @Override
    public void onClick(View view) {
        if (view.getTag().equals(TAG_ADD)){
            FloatingActionMenu actionMenu1 = new FloatingActionMenu.Builder(this)
                    .attachTo(Add)
                    .addSubActionView(TagGreenButton, 150, 150)
                    .addSubActionView(TagBlueButton, 150, 150)
                    .setRadius(200)
                    .build();
        }
    
    
    }
    

    Thanks for the help every body!!