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



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);
}
}


Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.