0

I am trying to add FloatingActionButton to FloatingActionMenu but when during initilization of FloatingActionButton cannot get context all methods return null

this is my fragment calls

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.getbase.floatingactionbutton.FloatingActionButton;
import com.getbase.floatingactionbutton.FloatingActionsMenu;
import demo.com.example.syed.test.R;
public class stdDetails extends android.support.v4.app.Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    FloatingActionMenu detailsfabmenu=(FloatingActionMenu)Container.findViewById(R,id,std_Main_Detials_fab_menu);
    FoatingActionButton(getContext());
    detailsfabmenu.addButton(changename);
    return inflater.inflate(R.layout.stddetailsfragment,container,false);
}}

code

FloatingActionButton changename=new FloatingActionButton(getContext());

returns null value i have even tried following options based on avilabel answers

FloatingActionButton changename=new FloatingActionButton(container.getContext());

FloatingActionButton changename=new FloatingActionButton(getActivity().getApplicationContext());

FloatingActionButton changename=new FloatingActionButton(this.getContext());

FloatingActionButton changename=new FloatingActionButton(this.getActivity().getApplicationContext());

this are all answer's provided here Using context in a fragment

Community
  • 1
  • 1
Syed Furqan
  • 115
  • 6

2 Answers2

0

This is how you initialize a FAB:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabBtn);

Here fabBtn is the id of your FAB in the layout XML file.

Update:

Use this line of code:

FloatingActionButton changename=new FloatingActionButton(getBaseContext());
AlphaQ
  • 656
  • 8
  • 18
  • Yes you are wright about initilizing fab button but if i use that method cannot add button.I am trying to impliment whats given in this exapmle https://github.com/futuresimple/android-floating-action-button/blob/master/sample/src/main/java/com/getbase/floatingactionbutton/sample/MainActivity.java – Syed Furqan Dec 10 '16 at 09:06
  • got it wright thaxs..its workin under method public void onViewCreated(View view, @Nullable Bundle savedInstanceState) if i use it under onCreate method its giving nullpointer error – Syed Furqan Dec 10 '16 at 12:57
0

You should call your Floating Action Button from MainActivity

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabBtn);
fab.show().

If you want to show your fab for a specific fragment you can use fragment tag:

Fragment fragmentA = new FragmentA();
getFragmentManager().beginTransaction()
.replace(R.id.MainFrameLayout,fragmentA,"YOUR_TARGET_FRAGMENT_TAG")
.addToBackStack("YOUR_SOURCE_FRAGMENT_TAG").commit(); 

And then check if you have the desired fragment in Mainactivity:

 if(currentFrag.equals("YOUR_TARGET_FRAGMENT_TAG")){
        fab.show();
    }
    else
        fab.hide();
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
  • i get your idea but thats not the case i dont want any button on my main activity i want to add button to my floatingactionbutton in fragment..it should be like Fab of Inbox app – Syed Furqan Dec 10 '16 at 09:01
  • No, not exactly main activity. That activity, which will be inflated by your fragment. – tahsinRupam Dec 10 '16 at 09:31