1

This should be an easy question.

Im trying to create either an "if statement" or "switch statement" based off a floating action button image resource in the drawable folder.

I coded a floating action button within my ActivityMain file and my activity_main.xml file.

Within ActivityMain I only display my fab when tab.getPosition() == 2. So its only displayed in 1 out of 5 fragments. Within that 1 fragment, I have two tabs.

Below is the Fragment code and my specific question. Thanks for your help. Its much appreciated.

public class PageMessageFragment extends Fragment {

private RecyclerView recyclerView;
private View view;
private TabLayout chat_tabs;
private SearchView search;
public FloatingActionButton fab;

// Random Model
private List<Friend> items_friend = new ArrayList<>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.page_fragment_message, container, false);

    // activate fragment menu
    setHasOptionsMenu(true);

    // Random Model
    List<Friend> items = Constant.getFriendsData(getActivity());
    items_friend = items.subList(0, 4); // Actual friends list
    fab = (FloatingActionButton) getActivity().findViewById(R.id.fab); // Gets fab ID from activity_main.xml

    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setHasFixedSize(true);

    chat_tabs = (TabLayout) view.findViewById(R.id.chat_tabs);
    chat_tabs.addTab(chat_tabs.newTab().setText("MESSAGES"), true);
    chat_tabs.addTab(chat_tabs.newTab().setText("GROUP"));

    chat_tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            if(tab.getPosition()==0){
                showListMessages(); // Actual Friends List
                fab.setImageResource(R.drawable.ic_create);
            }else if(tab.getPosition()==1){
                showListGroups();
                fab.setImageResource(R.drawable.ic_add_group);
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {}
        @Override
        public void onTabReselected(TabLayout.Tab tab) {}

    });

    showListMessages();
    //fab.show();
    return view;
}

/* This is one thing that I tried, but cant seem to get it to work correctly. The if statement doesnt work
 * and always displays the snackbar. What Im trying to do is execute ActivitySelectFriend based on the
  * fab.setImageResource file. Which changes depending on the tab Im in. So if Im in "Tab Messages" I want
  * display the Activity. If Im on "Tab Group" I want to just display a snackbar for now. */
@Override
public void onViewCreated (final View view, Bundle savedInstanceState) {
    final FloatingActionButton fab1 = (FloatingActionButton) getActivity().findViewById(R.id.fab); // Gets fab ID from activity_main.xml
     /*
    fab1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(fab1.getDrawable() == R.drawable.ic_create) {
                Intent i = new Intent(v.getContext(), ActivitySelectFriend.class);
                startActivity(i);
            } else {
                Snackbar.make(view, "Select Group", Snackbar.LENGTH_SHORT).show();
            }

        }
    }); */
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jason
  • 813
  • 11
  • 13
  • FloatingActionButton is just an ImageView... `fab1.getDrawable()` therefore is a Drawable object, which can never `== (some int value)` – OneCricketeer Sep 23 '17 at 22:37
  • If this is a duplicate, can I get a link to the answer. And if the if statement doesnt work. Can you create a switch statement. Im looking for an answer. – Jason Sep 23 '17 at 22:45
  • The linked duplicate clearly shows a switch statement – OneCricketeer Sep 23 '17 at 22:45
  • You're looking to do `fab.setTag(R.drawable.ic_create);` and later switch on `((Integer) fab.getTag())` – OneCricketeer Sep 23 '17 at 22:48
  • I'll be honest. I just started learning how to code in android studio a month ago. I wish you didn't mark this as a duplicate because I dont believe the answer is clearly defined. – Jason Sep 23 '17 at 23:17
  • Alright... You have `fab.setImageResource(R.drawable.ic_create);`.. So, as the answers there show, you can **also** do `fab.setTag(R.drawable.ic_create);` ... Then, simply use `fab.getTag()`, and compare it! – OneCricketeer Sep 23 '17 at 23:31

0 Answers0