Gaurav Bhagat
Gaurav Bhagat

Reputation: 31

Android Studio - Floating Action Button not working on ViewPager Initialization

Please Help In Floating Action Button

I am trying to load the sharedFab(FloatingActionButton) with ViewPager. But it is not working when I load for first time (Before Page Change).

enter image description here

viewPagerMainAct.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0 :
                        sharedFab.setImageDrawable(getDrawable(R.drawable.ic_medical));
                        sharedFab.show();
                        sharedFab.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(MainActivity.this, "New Chat", Toast.LENGTH_SHORT).show();
                            }
                        });
                        break;
                    case 1:
                        sharedFab.setImageDrawable(getDrawable(R.drawable.ic_sticky_notes));
                        sharedFab.show();
                        sharedFab.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(MainActivity.this, "New Post", Toast.LENGTH_SHORT).show();
                            }
                        });
                        break;
                    case 2:
                        sharedFab.hide();
                        break;
                    default:
                        sharedFab.hide();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

Upvotes: 0

Views: 219

Answers (3)

Gaurav Bhagat
Gaurav Bhagat

Reputation: 31

I totally agree that ur answers are correct but I am using a single FAB with two viewpager fragments and different work. And if it not loading at initialization then what we do?

Upvotes: 0

Paul Maltsev
Paul Maltsev

Reputation: 521

You use addOnPageChangeListener it wait for your request before do something. So, most easy way, it's just add this sharedFab.setImageDrawable(getDrawable(R.drawable.ic_medical)) before viewPagerMainAct.

Second way it add picture in xml, like default picture. And then change it in changeListener.

Upvotes: 1

Javad Dehban
Javad Dehban

Reputation: 1292

Insert this line of code into the onCreate.

sharedFab.setImageDrawable(getDrawable(R.drawable.ic_medical));
                            sharedFab.show();

The first time you run the app, you don't put photos inside the button. But in the first click, this photo is placed

Upvotes: 0

Related Questions