Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to create an Action Button Fragment? [closed]

Action button is this:

Action button
(source: android.com)

I think the framework should provide easy-to-use Fragment where developer just provides title, icon, background image and listener. I didn't find it in the docs, do you know about it?

like image 605
David Vávra Avatar asked Jul 26 '14 08:07

David Vávra


1 Answers

I ended up writing it myself according to design guidelines:

ActionFragment.java

public class ActionFragment extends Fragment implements View.OnClickListener {

    private static Listener mListener;
    private CircledImageView vIcon;
    private TextView vLabel;

    public static ActionFragment create(int iconResId, int labelResId, Listener listener) {
        mListener = listener;
        ActionFragment fragment = new ActionFragment();
        Bundle args = new Bundle();
        args.putInt("ICON", iconResId);
        args.putInt("LABEL", labelResId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_action, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        vIcon = (CircledImageView) view.findViewById(R.id.icon);
        vLabel = (TextView) view.findViewById(R.id.label);
        vIcon.setImageResource(getArguments().getInt("ICON"));
        vLabel.setText(getArguments().getInt("LABEL"));
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mListener.onActionPerformed();
    }

    public interface Listener {
        public void onActionPerformed();
    }
}

layout/fragment_action.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:paddingLeft="16dp"
              android:paddingRight="16dp"
              android:paddingTop="24dp"
              android:paddingBottom="12dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.wearable.view.CircledImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        tools:src="@drawable/ic_full_cancel"
        app:circle_color="@color/action_button"
        app:circle_radius="52dp"
        app:circle_border_width="0dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        tools:text="Cancel"
        android:id="@+id/label"
        android:fontFamily="sans-serif-condensed-light"
        android:textSize="20sp"
        android:textColor="@color/white"/>

</LinearLayout>

color/action_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#2878ff" android:state_pressed="false" />
    <item android:color="#2955C5" android:state_pressed="true" />
</selector>

Example usage (e.g. in FragmentGridPagerAdapter)

ActionFragment.create(R.drawable.ic_full_open_on_phone, R.string.open_on_phone, new ActionFragment.Listener() {
                    @Override
                    public void onActionPerformed() {

                    }
                });
like image 54
David Vávra Avatar answered Oct 17 '22 20:10

David Vávra