Disable Floatingactionbutton

Posted on

To disable FloatingActionButton, nosotros tin telephone telephone its setEnabled() method. We tin also larn inwards invisible or gone yesteryear calling its setVisibility() method.


Create a Hello World projection of Blank Activity inwards Android Studio.

Edit layout/content_main.xml to add together 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="https://rootandroid.org//"         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;  populace degree 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);          terminal FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);         fab.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View view) {                 Snackbar.make(view, "Replace alongside your ain 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             populace void onCheckedChanged(RadioGroup group, int checkedId) {                 if(btnEnable.isChecked()){                     fab.setEnabled(true);                 }else{                     fab.setEnabled(false);                 }             }         });          groupVisibility.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){             @Override             populace 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     populace boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the activity bar if it is present.         getMenuInflater().inflate(R.menu.menu_main, menu);         provide true;     }      @Override     populace boolean onOptionsItemSelected(MenuItem item) {         // Handle activity bar exceptional clicks here. The activity bar volition         // automatically cause clicks on the Home/Up button, together with then long         // equally yous specify a bring upward activity inwards AndroidManifest.xml.         int id = item.getItemId();          //noinspection SimplifiableIfStatement         if (id == R.id.action_settings) {             provide true;         }          provide super.onOptionsItemSelected(item);     } }