Skip to content

Commit

Permalink
Add open_src and close_src attributes, maybe closes arthur-ghazaryan#3
Browse files Browse the repository at this point in the history
  • Loading branch information
wasylews committed Feb 4, 2018
1 parent 7798e65 commit 0c841ff
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/ag/fabo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ private void initFabActionMenu() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Main fab clicked!", Toast.LENGTH_SHORT).show();
if (fabWithOptions.isOptionsMenuOpened()) {
fabWithOptions.closeOptionsMenu();
}
}
});

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
custom:color="@color/colorAccent"
custom:options_color="@color/colorPrimary"
custom:options_menu="@menu/fab_menu"
custom:src="@drawable/ic_add_white_24dp" />
custom:open_src="@drawable/ic_add_white_24dp"
custom:close_src="@drawable/ic_close_white_24dp" />


</android.support.design.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class FabWithOptions extends LinearLayout {
private boolean enableMiniFabs = false;
private Context context;
private OptionsFabLayout mOptionsFabLayout;
private int mainFabOpenDrawable;
private int mainFabCloseDrawable;


/**
Expand Down Expand Up @@ -72,16 +74,17 @@ private void init(Context context, AttributeSet attrs) {
* Set Main FloatingActionButton parameters.
*/
private void setupMainFab(Context context, TypedArray a) {
int mainFabDrawable = a.getResourceId(R.styleable.OptionsFabLayout_src, -1);
mainFabOpenDrawable = a.getResourceId(R.styleable.OptionsFabLayout_open_src, -1);
mainFabCloseDrawable = a.getResourceId(R.styleable.OptionsFabLayout_close_src, -1);

int color = a.getColor(
R.styleable.OptionsFabLayout_color,
ContextCompat.getColor(context, R.color.colorAccent));

mMainFab.setBackgroundTintList(ColorStateList.valueOf(color));

if (mainFabDrawable != -1) {
mMainFab.setImageResource(mainFabDrawable);
if (mainFabOpenDrawable != -1) {
mMainFab.setImageResource(mainFabOpenDrawable);
}
//Set margins on Main FloatingActionButton.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Expand Down Expand Up @@ -186,6 +189,10 @@ public void run() {
}
visibilitySetup(View.VISIBLE);
enableMiniFabs = true;

if (mainFabCloseDrawable != -1) {
mMainFab.setImageResource(mainFabCloseDrawable);
}
} else {
listener.onClick(view);
}
Expand Down Expand Up @@ -213,6 +220,10 @@ MenuItem getMenuItem(int index) {
void closeOptionsMenu() {
enableMiniFabs = false;
visibilitySetup(View.GONE);

if (mainFabOpenDrawable != -1) {
mMainFab.setImageResource(mainFabOpenDrawable);
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion floatingactionmenu/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<declare-styleable name="OptionsFabLayout">
<attr name="options_menu" format="reference" />
<attr name="options_color" format="color" />
<attr name="src" format="reference" />
<attr name="open_src" format="reference" />
<attr name="close_src" format="reference" />
<attr name="color" format="color" />
<attr name="background_color" format="color" />
</declare-styleable>
Expand Down

0 comments on commit 0c841ff

Please sign in to comment.