akshay2211 / BubbleTabBar

BubbleTabBar is a bottom navigation bar with customizable bubble-like tabs

Home Page:https://github.com/akshay2211/BubbleTabBar

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to Add Fragments ?

rajam1215 opened this issue · comments

Hello @akshay2211

How to add multiple fragments in Main Activity to show fragments instead of colors. please give some solutions.

Thanks

Hello @rajam1215
The gradient colored backgrounds are in the sample i created.
You can add a ViewPager and add fragments just like usual
just connect the viewpager with bubbletabbar for the page change

Hello @rajam1215
The gradient colored backgrounds are in the sample i created.
You can add a ViewPager and add fragments just like usual
just connect the viewpager with bubbletabbar for the page change

Hello @akshay2211

This is code to add multiple fragment with view pager

MainActivity

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import com.fxn.BubbleTabBar;
import com.fxn.OnBubbleClickListener;
import com.fxn.bubbletabbarapp.R;

import com.fxn.bubbletabbarapp.adapter.ViewPagerAdapter;
import org.jetbrains.annotations.Nullable;

public class MainActivity extends AppCompatActivity {

private ViewPager viewpager;
public BubbleTabBar tabBar;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager windowManager = this.getWindowManager();
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);

    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;
    Log.e("height", "-> " + height);
    Log.e("width", "-> " + width);

    Window window = this.getWindow();
    View view = window.getDecorView();
    view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

    viewpager = (ViewPager) findViewById(R.id.viewpager);
    tabBar = (BubbleTabBar) findViewById(R.id.bubbleTabBar);
    tabBar.addBubbleListener(new OnBubbleClickListener() {
        @Override
        public void onBubbleClick(int id) {
            switch (id) {
                case R.id.home:
                    viewpager.setCurrentItem(0);
                    break;
                case R.id.log:
                    viewpager.setCurrentItem(1);
                    break;
                case R.id.doc:
                    viewpager.setCurrentItem(2);
                    break;
                case R.id.setting:
                    viewpager.setCurrentItem(3);
            }
        }
    });

    tabBar.setupBubbleTabBar(viewpager);

    FragmentManager fragmentManager = getSupportFragmentManager();
    ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager);

    viewpager.setAdapter(adapter);
}}

activity_main

  <?xml version="1.0" encoding="utf-8"?>
  <androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="#f0f0f0"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

<androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toTopOf="@id/bubbleTabBar"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

</androidx.viewpager.widget.ViewPager>

<com.fxn.BubbleTabBar
        android:id="@+id/bubbleTabBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF"
        android:elevation="16dp"
        android:padding="7dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:bubbletab_menuResource="@menu/list"
        app:bubbletab_disabled_icon_color="@color/colorPrimaryDark"
        app:bubbletab_horizontal_padding="20dp"
        app:bubbletab_icon_size="20dp"
        app:bubbletab_title_size="16sp"
        app:bubbletab_vertical_padding="10dp">

</com.fxn.BubbleTabBar>

</androidx.constraintlayout.widget.ConstraintLayout>

ViewPagerAdapter Class

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.fxn.bubbletabbarapp.fragment.DocumentsFragment;
import com.fxn.bubbletabbarapp.fragment.HomeFragment;
import com.fxn.bubbletabbarapp.fragment.LoggerFragment;
import com.fxn.bubbletabbarapp.fragment.SettingsFragment;

public class ViewPagerAdapter extends FragmentPagerAdapter {

private Fragment[] childFragments;

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
    childFragments = new Fragment[] {
            new HomeFragment(), //0
            new LoggerFragment(), //1
            new DocumentsFragment(),//2
            new SettingsFragment()//3
    };
}

@Override
public Fragment getItem(int position) {
    return childFragments[position];
}

@Override
public int getCount() {
    return childFragments.length; // 4 items
}

@Override
public CharSequence getPageTitle(int position) {
    String title = getItem(position).getClass().getName();
    return title.subSequence(title.lastIndexOf(".") + 1, title.length());
}}

Please note
Don't use more then 5 tabs and less then 3 Tabs.

Thanks

Hello @akshay2211

Please describe my little bit code (code snippet) of java in your MD file, means how to add multiple fragments in view pager with your tab bar.

So user or beginners can use your library with less efforts. now i close that issue.

Thanks