Hello Developers, In this blog I will show you that how to make custom tab layout and view pager as well as bottom navigation for Android Studio users. In a custom tablayout viewpager you can customize the design easily according to your choice. You can fuly customise a custom viewpager tablayout. Thank you for visiting this Article and we have discussed withan example for better understanding and not getting any error in the project, Just watch this example given below and implement to your project and enjoy it.
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="@+id/_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#607D8B"
android:orientation="vertical" />
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/base"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/linear3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:background="#BDBDBD"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:background="#42A5F5"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:background="#DCE775"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" />
</LinearLayout>
<FrameLayout
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:layout_gravity="bottom">
<LinearLayout
android:id="@+id/buttomm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear52"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/linear12"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="4dp"
android:padding="8dp"
android:background="#9E9E9E"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/linear47"
android:layout_width="match_parent"
android:layout_height="55dp"
android:paddingBottom="6dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linear50"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:id="@+id/imageview8"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/default_image"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/textview24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HOME"
android:textSize="12sp"
android:textColor="#00796B" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear51"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:id="@+id/imageview9"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/default_image"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/textview25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CODES"
android:textSize="12sp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear49"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:id="@+id/imageview10"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/default_image"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/textview26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LIBRARY"
android:textSize="12sp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear48"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:id="@+id/imageview11"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/default_image"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/textview27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FILES"
android:textSize="12sp"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</FrameLayout>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
package com.my.newproject183;
import androidx.appcompat.app.AppCompatActivity;
import androidx.annotation.*;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.media.*;
import android.net.*;
import android.text.*;
import android.text.style.*;
import android.util.*;
import android.webkit.*;
import android.animation.*;
import android.view.animation.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.text.*;
import org.json.*;
import java.util.ArrayList;
import android.widget.LinearLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Intent;
import android.net.Uri;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.DialogFragment;
public class MainActivity extends AppCompatActivity {
private Toolbar _toolbar;
private AppBarLayout _app_bar;
private CoordinatorLayout _coordinator;
private double position_number = 0;
private ArrayList<String> list = new ArrayList<>();
private LinearLayout tablayout;
private FrameLayout main;
private LinearLayout base;
private LinearLayout trash;
private LinearLayout linear3;
private FrameLayout linear1;
private LinearLayout layout1;
private LinearLayout layout2;
private LinearLayout layout3;
private LinearLayout buttomm;
private LinearLayout linear52;
private LinearLayout linear12;
private LinearLayout linear47;
private LinearLayout linear50;
private LinearLayout linear51;
private LinearLayout linear49;
private LinearLayout linear48;
private ImageView imageview8;
private TextView textview24;
private ImageView imageview9;
private TextView textview25;
private ImageView imageview10;
private TextView textview26;
private ImageView imageview11;
private TextView textview27;
private Intent in = new Intent();
@Override
protected void onCreate(Bundle _savedInstanceState) {
super.onCreate(_savedInstanceState);
setContentView(R.layout.main);
initialize(_savedInstanceState);
initializeLogic();
}
private void initialize(Bundle _savedInstanceState) {
_app_bar = findViewById(R.id._app_bar);
_coordinator = findViewById(R.id._coordinator);
_toolbar = findViewById(R.id._toolbar);
setSupportActionBar(_toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View _v) {
onBackPressed();
}
});
tablayout = findViewById(R.id.tablayout);
main = findViewById(R.id.main);
base = findViewById(R.id.base);
trash = findViewById(R.id.trash);
linear3 = findViewById(R.id.linear3);
linear1 = findViewById(R.id.linear1);
layout1 = findViewById(R.id.layout1);
layout2 = findViewById(R.id.layout2);
layout3 = findViewById(R.id.layout3);
buttomm = findViewById(R.id.buttomm);
linear52 = findViewById(R.id.linear52);
linear12 = findViewById(R.id.linear12);
linear47 = findViewById(R.id.linear47);
linear50 = findViewById(R.id.linear50);
linear51 = findViewById(R.id.linear51);
linear49 = findViewById(R.id.linear49);
linear48 = findViewById(R.id.linear48);
imageview8 = findViewById(R.id.imageview8);
textview24 = findViewById(R.id.textview24);
imageview9 = findViewById(R.id.imageview9);
textview25 = findViewById(R.id.textview25);
imageview10 = findViewById(R.id.imageview10);
textview26 = findViewById(R.id.textview26);
imageview11 = findViewById(R.id.imageview11);
textview27 = findViewById(R.id.textview27);
}
private void initializeLogic() {
FrameLayout rl = new FrameLayout(this); FrameLayout.LayoutParams lparams = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); rl.setLayoutParams(lparams); main.removeAllViews(); rl.addView(base); rl.addView(linear1); main.addView(rl);
viewPager = new androidx.viewpager.widget.ViewPager(this);
viewPager.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
MyPagerAdapter adapter = new MyPagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
base.addView(viewPager);
viewPager.addOnPageChangeListener(new androidx.viewpager.widget.ViewPager.OnPageChangeListener() {
public void onPageSelected(int position) {
if (viewPager.getCurrentItem() == 0) {
}
else {
if (viewPager.getCurrentItem() == 1) {
}
else {
if (viewPager.getCurrentItem() == 2) {
}
}
}
}
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override public void onPageScrollStateChanged(int state) {
}
});
tabLayout = new com.google.android.material.tabs.TabLayout(this);
tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);
}
private class MyPagerAdapter extends androidx.viewpager.widget.PagerAdapter {
public int getCount() {
return 3;
}
@Override public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.empty, null);
LinearLayout container = (LinearLayout) v.findViewById(R.id.linear1);
if (position == 0) {
ViewGroup parent = (ViewGroup) layout1.getParent();
if (parent != null) {
parent.removeView(layout1);
}container.addView(layout1);
} else if (position == 1) {
ViewGroup parent = (ViewGroup) layout2.getParent();
if (parent != null) {
parent.removeView(layout2);
}
container.addView(layout2);
} else if (position == 2) {
ViewGroup parent = (ViewGroup) layout3.getParent();
if (parent != null) {
parent.removeView(layout3);
}
container.addView(layout3);
}
collection.addView(v, 0);
return v;
}
@Override public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
trash.addView((View) view);
}
@Override public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);}
@Override public Parcelable saveState() {
return null;
}
}
androidx.viewpager.widget.ViewPager viewPager;
com.google.android.material.tabs.TabLayout tabLayout;
private void foo() {
}
@Deprecated
public void showMessage(String _s) {
Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show();
}
@Deprecated
public int getLocationX(View _v) {
int _location[] = new int[2];
_v.getLocationInWindow(_location);
return _location[0];
}
@Deprecated
public int getLocationY(View _v) {
int _location[] = new int[2];
_v.getLocationInWindow(_location);
return _location[1];
}
@Deprecated
public int getRandom(int _min, int _max) {
Random random = new Random();
return random.nextInt(_max - _min + 1) + _min;
}
@Deprecated
public ArrayList<Double> getCheckedItemPositionsToArray(ListView _list) {
ArrayList<Double> _result = new ArrayList<Double>();
SparseBooleanArray _arr = _list.getCheckedItemPositions();
for (int _iIdx = 0; _iIdx < _arr.size(); _iIdx++) {
if (_arr.valueAt(_iIdx))
_result.add((double)_arr.keyAt(_iIdx));
}
return _result;
}
@Deprecated
public float getDip(int _input) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, _input, getResources().getDisplayMetrics());
}
@Deprecated
public int getDisplayWidthPixels() {
return getResources().getDisplayMetrics().widthPixels;
}
@Deprecated
public int getDisplayHeightPixels() {
return getResources().getDisplayMetrics().heightPixels;
}
}
<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"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:orientation="vertical"
android:layout_weight="1" />
</LinearLayout>
Thank you for visiting our article and hope that you got some help b visiting our article. Please bookmark our site to your browser for future help. And if you get any error then do not forget to contact us, We will try to solve your problem as soon as possible. Thank You
0 Comments