Tuesday, November 3, 2015

Disable FloatingActionButton


To disable FloatingActionButton, we can call its setEnabled() method. We can also make it invisible or gone by calling its setVisibility() method.


Create a Hello World project of Blank Activity in Android Studio.


Edit layout/content_main.xml to add RadioButtons.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/groupenable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/fabenable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enable"
            android:checked="true"/>
        <RadioButton
            android:id="@+id/fabdisable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Disable"/>
    </RadioGroup>
    <RadioGroup
        android:id="@+id/groupvisibility"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/fabvisible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Visible"
            android:checked="true"/>
        <RadioButton
            android:id="@+id/fabinvisible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Invisible"/>
        <RadioButton
            android:id="@+id/fabgone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gone"/>
    </RadioGroup>
</LinearLayout>


Modify MainActivity.java
package com.blogspot.android_er.androidfloatingactionbutton;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    RadioGroup groupEnable, groupVisibility;
    RadioButton btnEnable, btnDisable;
    RadioButton btnVisible, btnInvisible, btnGone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        groupEnable = (RadioGroup)findViewById(R.id.groupenable);
        btnEnable = (RadioButton)findViewById(R.id.fabenable);
        btnDisable = (RadioButton)findViewById(R.id.fabdisable);
        groupVisibility = (RadioGroup)findViewById(R.id.groupvisibility);
        btnVisible = (RadioButton)findViewById(R.id.fabvisible);
        btnInvisible = (RadioButton)findViewById(R.id.fabinvisible);
        btnGone = (RadioButton)findViewById(R.id.fabgone);

        groupEnable.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(btnEnable.isChecked()){
                    fab.setEnabled(true);
                }else{
                    fab.setEnabled(false);
                }
            }
        });

        groupVisibility.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(btnVisible.isChecked()){
                    fab.setVisibility(FloatingActionButton.VISIBLE);
                }else if(btnInvisible.isChecked()){
                    fab.setVisibility(FloatingActionButton.INVISIBLE);
                }else{
                    fab.setVisibility(FloatingActionButton.GONE);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


1 comment:

Anonymous said...

I think most folks would know how to disable the functionality of floating action button. What I was curious about making the actual button visually seem disabled. If you have some suggestions that would be really helpful!