0

i want use fab Button

enter image description here

the i want use this on fab Button

enter image description here

this code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.browser, menu);

    SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
    String links = sharedPreferences.getString(WEB_LINKS, null);

    if (links != null) {

        Gson gson = new Gson();
        ArrayList<String> linkList = gson.fromJson(links, new TypeToken<ArrayList<String>>() {
        }.getType());

        if (linkList.contains(current_page_url)) {
            menu.getItem(0).setIcon(R.drawable.ic_bookmark_black_24dp);
        } else {
            menu.getItem(0).setIcon(R.drawable.ic_bookmark_border_black_24dp);
        }
    } else {
        menu.getItem(0).setIcon(R.drawable.ic_bookmark_border_black_24dp);
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == R.id.action_bookmark) {

        String message;

        SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
        String jsonLink = sharedPreferences.getString(WEB_LINKS, null);
        String jsonTitle = sharedPreferences.getString(WEB_TITLE, null);


        if (jsonLink != null && jsonTitle != null) {

            Gson gson = new Gson();
            ArrayList<String> linkList = gson.fromJson(jsonLink, new TypeToken<ArrayList<String>>() {
            }.getType());

            ArrayList<String> titleList = gson.fromJson(jsonTitle, new TypeToken<ArrayList<String>>() {
            }.getType());

            if (linkList.contains(current_page_url)) {
                linkList.remove(current_page_url);
                titleList.remove(mXWalkView.getTitle().trim());
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(WEB_LINKS, new Gson().toJson(linkList));
                editor.putString(WEB_TITLE, new Gson().toJson(titleList));
                editor.apply();


                message = "تم الازالة من قائمة المفضلة";

            } else {
                linkList.add(current_page_url);
                titleList.add(mXWalkView.getTitle().trim());
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(WEB_LINKS, new Gson().toJson(linkList));
                editor.putString(WEB_TITLE, new Gson().toJson(titleList));
                editor.apply();

                message = "تم الاضافة الى المفضلة";
            }
        } else {

            ArrayList<String> linkList = new ArrayList<>();
            ArrayList<String> titleList = new ArrayList<>();
            linkList.add(current_page_url);
            titleList.add(mXWalkView.getTitle());
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(WEB_LINKS, new Gson().toJson(linkList));
            editor.putString(WEB_TITLE, new Gson().toJson(titleList));
            editor.apply();

            message = "تم الاضافة من قبل";
        }

        Snackbar snackbar = Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG);
        snackbar.show();

        invalidateOptionsMenu();
    }

    return super.onOptionsItemSelected(item);
}

i need help for my project i hope i have some people help me to solve this

thank you

A floating action button represents the primary action in an application. A floating action button is used for a promoted action. Shaped like a circled icon floating above the UI, it changes color upon focus and lifts upon selection. When pressed, it may contain more related actions.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
ahmmed
  • 98
  • 9

2 Answers2

1

I hope this will work for you.

Required This dependency in gradle file.

implementation 'com.android.support:design:26.1.0'

In xml File

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimaryDark">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:src="@mipmap/ic_back"
            android:layout_alignParentLeft="true"/>

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fabSize="auto"
            android:src="@mipmap/ic_add"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"/>

        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

</LinearLayout>

In Activity

FloatingActionButton mFloatingActionButton;
 mFloatingActionButton=(FloatingActionButton) findViewById(R.id.fab);
    mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //do your task
        }
    });
GParekar
  • 1,209
  • 1
  • 8
  • 15
0

Add the Event Listener to FloatingActionButton:

FloatingActionButton myFab = (FloatingActionButton) myView.findViewById(R.id.myFAB); 
myFab.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
        doMyThing(); 
    } 
});

The same issue in this Thread

For more details follow : FloatingActionButton example with Support Library

R. García
  • 815
  • 9
  • 20