Skip to content

Commit

Permalink
Merge branch 'release-1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
markormesher committed Apr 7, 2016
2 parents d927357 + 7401eae commit c66490b
Show file tree
Hide file tree
Showing 8 changed files with 9,524 additions and 68 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ The `FloatingActionButton` view must be placed at the **root** of your layout, *

The icon displayed in the centre of the FAB should be set with `fab.setIcon(...)`, passing in the `View`, `Drawable` or `Drawable` resource ID to use. This `View` will be centred in a 24dp x 24dp view group, as per the Android Material Design specs.

See the note below on [state preservation](#note-state-preservation).

### FAB Background Colour

The background colour to be used for the FAB should be set with `fab.setBackgroundColor(...)`, passing in an RGBa colour value (e.g. `0xffff9900` for a dark orange). Note that this method does **not** take a colour resource ID, so passing in `R.color.some_colour_name` will not work.

See the note below on [state preservation](#note-state-preservation).

### FAB Click Listener

A click listener can be added to the FAB in the same way as any other button:
Expand Down Expand Up @@ -110,4 +114,16 @@ If the functionality has changed such that `getCount()` or `getViews(...)` will

### Controls

The speed-dial menu can be manually opened and closed with `fab.openSpeedDialMenu()` and `fab.closeSpeedDialMenu()`. These methods will do nothing if no speed-dial menu adapter is set, or if they are called when the menu is already in the indicated state (i.e. `fab.openSpeedDialMenu()` will do nothing if the menu is already open).
The FAB can be hidden and shown with the `fab.hide()` and `fab.show()` methods, and the method `fab.isShown()` will return a boolean indicating the current state. These methods animate the FAB in and out of visibility. If the speed-dial menu is open when `.hide()` is called it will be closed.

The speed-dial menu can be manually opened and closed with `fab.openSpeedDialMenu()` and `fab.closeSpeedDialMenu()`. These methods will do nothing if no speed-dial menu adapter is set, if the FAB is hidden, or if they are called when the menu is already in the indicated state (i.e. `fab.openSpeedDialMenu()` will do nothing if the menu is already open).

### Note: State Preservation

When a configuration change happens within your app and the activity/fragment is forced to rebuild its layout, the FAB does some primitive state preservation and restoration. The following settings are preserved between configuration changes:

- whether the icon is shown or hidden (see [Controls](#controls));
- the FAB icon, **if and only if** it was set as a `Drawable` resource ID;
- the FAB background colour.

All other properties (`View`/`Drawable` icons, speed-dial menu adapters, etc.) will need to be restored "manually". The demo application shows how this can be accomplished.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ buildscript {
}

def version = new Properties()
version['code'] = 5
version['name'] = '1.0.1'
version['code'] = 6
version['name'] = '1.1.0'

def secrets = getSecrets()

Expand Down
162 changes: 113 additions & 49 deletions app/src/main/java/uk/co/markormesher/android_fab/app/DemoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,90 +16,154 @@

public class DemoActivity extends AppCompatActivity {

// state
private boolean fabHidden;
private boolean inClickMode;
private int iconSelected;
private int colourSelected;
private boolean speedDialColoursEnabled;
private boolean speedDialOptionalCloseEnabled;

// views
private FloatingActionButton fab;
private boolean inClickMode = false;
private int iconSelected = -1;
private int fabColourSelected = -1;
private boolean speedDialColoursEnabled = false;
private boolean speedDialOptionalCloseEnabled = false;
private Button hideShowButton;
private Button switchModeButton;
private Button toggleSpeedDialColoursSwitch;
private Button toggleSpeedDialOptionalCloseButton;
private Button openSpeedDialButton;

// button values
private static int[] icons = new int[]{
R.mipmap.ic_add,
R.mipmap.ic_done,
R.mipmap.ic_cloud,
R.mipmap.ic_swap_horiz,
R.mipmap.ic_swap_vert
};
private static int[] colours = new int[]{
0xff0099ff,
0xffff9900,
0xffff0099,
0xff9900ff
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fab_activity);
setTitle(R.string.app_name);

// set/restore state
if (savedInstanceState == null) {
// initial state
fabHidden = false;
inClickMode = true;
iconSelected = 0;
colourSelected = 0;
speedDialColoursEnabled = false;
speedDialOptionalCloseEnabled = false;
} else {
// restore state
fabHidden = savedInstanceState.getBoolean("fabHidden");
inClickMode = savedInstanceState.getBoolean("inClickMode");
iconSelected = savedInstanceState.getInt("iconSelected");
colourSelected = savedInstanceState.getInt("colourSelected");
speedDialColoursEnabled = savedInstanceState.getBoolean("speedDialColoursEnabled");
speedDialOptionalCloseEnabled = savedInstanceState.getBoolean("speedDialOptionalCloseEnabled");
}

// get reference to FAB
fab = (FloatingActionButton) findViewById(R.id.fab);

// initialise FAB
if (savedInstanceState == null) {
fab.setIcon(icons[iconSelected]);
fab.setBackgroundColour(colours[colourSelected]);
}
updateFabMode();

// get references to buttons
final Button switchModeButton = (Button) findViewById(R.id.switch_mode);
final Button toggleSpeedDialColoursSwitch = (Button) findViewById(R.id.toggle_colours);
final Button toggleSpeedDialOptionalCloseButton = (Button) findViewById(R.id.toggle_optional_close);
final Button openSpeedDialButton = (Button) findViewById(R.id.open_speed_dial);
hideShowButton = (Button) findViewById(R.id.hide_show);
switchModeButton = (Button) findViewById(R.id.switch_mode);
toggleSpeedDialColoursSwitch = (Button) findViewById(R.id.toggle_colours);
toggleSpeedDialOptionalCloseButton = (Button) findViewById(R.id.toggle_optional_close);
openSpeedDialButton = (Button) findViewById(R.id.open_speed_dial);

// update view state
updateButtonStates();

// set click listeners
hideShowButton.setOnClickListener(v -> {
if (fabHidden) {
fab.show();
} else {
fab.hide();
}
fabHidden = !fabHidden;
updateButtonStates();
});

switchModeButton.setOnClickListener(v -> {
inClickMode = !inClickMode;
if (inClickMode) {
fab.setOnClickListener(iv -> Toast.makeText(DemoActivity.this, R.string.click_simple, Toast.LENGTH_SHORT).show());
switchModeButton.setText(R.string.switch_mode_1);
} else {
fab.setMenuAdapter(new SpeedDialAdapter());
switchModeButton.setText(R.string.switch_mode_2);
}
toggleSpeedDialColoursSwitch.setEnabled(!inClickMode);
toggleSpeedDialOptionalCloseButton.setEnabled(!inClickMode);
openSpeedDialButton.setEnabled(!inClickMode);
updateButtonStates();
updateFabMode();
});

findViewById(R.id.change_icon).setOnClickListener(v -> {
int[] icons = new int[]{
R.mipmap.ic_add,
R.mipmap.ic_done,
R.mipmap.ic_cloud,
R.mipmap.ic_swap_horiz,
R.mipmap.ic_swap_vert
};
iconSelected = ++iconSelected % icons.length;
fab.setIcon(icons[iconSelected]);
});

findViewById(R.id.change_button_colour).setOnClickListener(v -> {
int[] colours = new int[]{
0xff0099ff,
0xffff9900,
0xffff0099,
0xff9900ff
};
fabColourSelected = ++fabColourSelected % colours.length;
fab.setBackgroundColour(colours[fabColourSelected]);
colourSelected = ++colourSelected % colours.length;
fab.setBackgroundColour(colours[colourSelected]);
});

toggleSpeedDialColoursSwitch.setOnClickListener(v -> {
speedDialColoursEnabled = !speedDialColoursEnabled;
if (speedDialColoursEnabled) {
toggleSpeedDialColoursSwitch.setText(R.string.toggle_colours_2);
} else {
toggleSpeedDialColoursSwitch.setText(R.string.toggle_colours_1);
}
updateButtonStates();
fab.rebuildSpeedDialMenu();
});

toggleSpeedDialOptionalCloseButton.setOnClickListener(v -> {
speedDialOptionalCloseEnabled = !speedDialOptionalCloseEnabled;
if (speedDialOptionalCloseEnabled) {
toggleSpeedDialOptionalCloseButton.setText(R.string.toggle_optional_close_2);
} else {
toggleSpeedDialOptionalCloseButton.setText(R.string.toggle_optional_close_1);
}
updateButtonStates();
fab.rebuildSpeedDialMenu();
});

openSpeedDialButton.setOnClickListener(v -> fab.openSpeedDialMenu());
openSpeedDialButton.setOnClickListener(v -> {
fab.openSpeedDialMenu();
if (!fab.isShown()) Toast.makeText(this, R.string.disabled_when_hidden, Toast.LENGTH_SHORT).show();
});
}

// set stuff going
findViewById(R.id.switch_mode).performClick();
findViewById(R.id.change_icon).performClick();
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("fabHidden", fabHidden);
outState.putBoolean("inClickMode", inClickMode);
outState.putInt("iconSelected", iconSelected);
outState.putInt("colourSelected", colourSelected);
outState.putBoolean("speedDialColoursEnabled", speedDialColoursEnabled);
outState.putBoolean("speedDialOptionalCloseEnabled", speedDialOptionalCloseEnabled);
}

private void updateButtonStates() {
hideShowButton.setText(fabHidden ? R.string.show_fab : R.string.hide_fab);
switchModeButton.setText(inClickMode ? R.string.switch_mode_to_speed_dial : R.string.switch_mode_to_click);
toggleSpeedDialColoursSwitch.setEnabled(!inClickMode);
toggleSpeedDialColoursSwitch.setText(speedDialColoursEnabled ? R.string.toggle_colours_off : R.string.toggle_colours_on);
toggleSpeedDialOptionalCloseButton.setEnabled(!inClickMode);
toggleSpeedDialOptionalCloseButton.setText(speedDialOptionalCloseEnabled ? R.string.toggle_optional_close_off : R.string.toggle_optional_close_on);
openSpeedDialButton.setEnabled(!inClickMode);
}

private void updateFabMode() {
if (inClickMode) {
fab.setOnClickListener(iv -> Toast.makeText(DemoActivity.this, R.string.click_simple, Toast.LENGTH_SHORT).show());
} else {
fab.setMenuAdapter(new SpeedDialAdapter());
}
}

private class SpeedDialAdapter extends SpeedDialMenuAdapter {
Expand Down Expand Up @@ -141,7 +205,7 @@ protected MenuItem getViews(Context context, int position) {
// example: Drawable ID and String ID
return new MenuItem() {{
iconDrawableId = R.mipmap.ic_cloud;
labelStringId = R.string.label_optional_close;
labelStringId = R.string.label_optional_close;
}};

default:
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/res/layout/fab_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
android:orientation="vertical"
android:gravity="center_horizontal">

<Button
android:id="@+id/hide_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>

<Button
android:id="@+id/switch_mode"
android:layout_width="wrap_content"
Expand All @@ -39,14 +45,14 @@
android:id="@+id/toggle_colours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_colours_1"
android:text="@string/toggle_colours_on"
android:layout_marginTop="10dp"/>

<Button
android:id="@+id/toggle_optional_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_optional_close_1"
android:text="@string/toggle_optional_close_on"
android:layout_marginTop="10dp"/>

<Button
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/res/values/values.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
<string name="yes">Yes</string>
<string name="no">No</string>

<string name="switch_mode_1">Switch to Speed-Dial Mode</string>
<string name="switch_mode_2">Switch to Click Mode</string>
<string name="hide_fab">Hide FAB</string>
<string name="show_fab">Show FAB</string>
<string name="switch_mode_to_speed_dial">Switch to Speed-Dial Mode</string>
<string name="switch_mode_to_click">Switch to Click Mode</string>
<string name="change_icon">Change FAB Icon</string>
<string name="change_button_colour">Change FAB Colour</string>
<string name="toggle_colours_1">Enable Speed-Dial Colours</string>
<string name="toggle_colours_2">Disable Speed-Dial Colours</string>
<string name="toggle_optional_close_1">Enable Optional Speed-Dial Close</string>
<string name="toggle_optional_close_2">Disable Optional Speed-Dial Close</string>
<string name="toggle_colours_on">Enable Speed-Dial Colours</string>
<string name="toggle_colours_off">Disable Speed-Dial Colours</string>
<string name="toggle_optional_close_on">Enable Optional Speed-Dial Close</string>
<string name="toggle_optional_close_off">Disable Optional Speed-Dial Close</string>
<string name="open_speed_dial">Open Speed Dial</string>

<string name="speed_dial_label">Option #%d</string>
Expand All @@ -29,6 +31,8 @@
<string name="click_simple">You clicked on the FAB!</string>
<string name="click_with_item">You clicked on item #%d!</string>

<string name="disabled_when_hidden">Disabled when the FAB is hidden</string>

<!--
Styles
-->
Expand Down
4 changes: 2 additions & 2 deletions fab/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ buildscript {
}

def version = new Properties()
version['code'] = 5
version['name'] = '1.0.1'
version['code'] = 6
version['name'] = '1.1.0'

apply plugin: 'me.tatarka.retrolambda'

Expand Down
Loading

0 comments on commit c66490b

Please sign in to comment.