Trying to implement nested TabLayouts - Tab with its tabs - android

I'm trying to create an activity like on the image below:
I have a main TabLayout and I want one of its fragments (the one in Tab2) to be an other TabLayout.
My question is how can I create the fragment Frag2.java (as seen in the code below) so that it can implement its own ViewPager and a TabLayout in order to create the nested tabs.
I don't want to use a TabHost because I want all tabs to allow swiping.
The xml for the main TabLayout( my Activity)
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#212121"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="#46bedc"
android:background="#212121"
android:fillViewport="false" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
Here's my Activity.java
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.test.fragments.Frag1;
import com.test.fragments.Frag2;
import java.util.ArrayList;
import java.util.List;
public class Activity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Frag1(), getString(R.string.frag1));
adapter.addFragment(new Frag2(), getString(R.string.frag2));
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
and the Frag1.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.test.R;
public class Frag1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container, false);
return view;
}
}
Thanks for the help

It is possible use google i belive u will find solution.
I have another solution it is:
when u will open flag2(flag2 will like flag1 but add on onCreateView(..) Intent to go activity of flag2) and create activity of flag2 on the activty add viewpaper and wht u want

Related

Give tabs who are a child of a viewpager a style/icon

I have a viewpager activity with tabs on the bottom:
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.susan.myapplication.R;
public class ViewPagerActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_pager, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 5 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Home";
case 1:
return "Account";
case 2:
return "Data";
case 3:
return "Settings";
}
return null;
}
}
}
My layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.susan.myapplication.activities.ViewPagerActivity">
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top">
</android.support.design.widget.TabLayout>
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
I have been following Google's viewpager tutorial and have created a viewpager with 4 slides and a set of tabs. Currently the tabs are just strings which I specified. I would like to have more control of these tabs and be able to also give them an icon and to style the background. In the layout all I see is the tablayout so I am unsuyre how to access the tabs themselves to give them these features.
Update your layout XML. Put your TabLayout outside of ViewPager and give an Id to TabLayout.
activity_view_pager.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.susan.myapplication.activities.ViewPagerActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
In your ViewPagerActivity, onCreate() method do the followings:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
// Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// TabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
To customize the Tab style, follow this nice Tutorial
Hope this will help~

TabLayout or horizontal scrollview Android

Image for the TabLayout that I wanted to achieve.
Can somebody shows me the way to achieve this?
My code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="center"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white" />
</LinearLayout>
I already set the attributes app:tabMode="scrollable" and app:tabGravity="center" but all the tabs just stick to the left in the layout.
I wanna achieve the result like the above image no matter how many the tabs are. Means the position of the tabs will away at the center or the tablayout.
PageFragment.java:
package com.example.testing;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
// In this case, the fragment displays simple text based on the page
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
SampleFragmentPagerAdapter.java:
package com.example.testing;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
TabActivity.java:
package com.example.testing;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class TabAcitivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),TabAcitivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
The tabs start from the left. How do I place it to make it start at the middle? Or is it better to use a horizontal scrollable ListView?
use this for scrolling tabs:
sliding_tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
For any other reference please have a look at the following url.
https://guides.codepath.com/android/google-play-style-tabs-using-tablayout

Changing Text visibility according to tab and setting background color of tab

I have used a custom toolbar.Inside custom toolbar I do have a textView above the tabs.Now I want to set the visibility of TextView to off for the second and third tabs.This is a image of my layout
In this image when the Ongoing tabs is selected the textfield above the tab(The ongoing survey closes on.........GMT) should be visible while for rest of the tabs the textview visibility should be gone.How can I do this?
The next problem is I have set the background of whole layout to using a background image in parent layout.But I am getting default background behind tabs.What I want is to remove the default tab background color and get the background of parent.How to do this?
This is my code for my layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="#drawable/innerpg_bg"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#42474b"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<ImageView
android:id="#+id/toolbarlogo"
android:src="#mipmap/toolbarlogo"
android:layout_width="wrap_content"
android:layout_marginRight="200dp"
android:layout_height="46dp"
/>
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/deadline"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#494e51"
android:gravity="center_vertical"
android:paddingLeft="40dp"
android:textSize="10dp"
android:textColor="#fff"
android:text="The Ongoing survey closes on Dec 31,2016 at 2:00pm GMT"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
And here is my java class
package com.example.user.timothysurvey.activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.user.timothysurvey.R;
import java.util.ArrayList;
import java.util.List;
import fragment.Ongoing;
import fragment.Result;
import fragment.Upcomming;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
TextView deadLine;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.mipmap.ongoing,
R.mipmap.upcoming,
R.mipmap.results
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
deadLine= (TextView) findViewById(R.id.deadline);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setSelectedTabIndicatorHeight(0);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
LayoutInflater inflater = LayoutInflater.from(this);
View view1 = inflater.inflate(R.layout.custom_tabs, null, false);
((TextView) view1.findViewById(R.id.text)).setText("Ongoing");
((ImageView) view1.findViewById(R.id.image)).setImageResource(tabIcons[0]);
View view2 = inflater.inflate(R.layout.custom_tabs, null, false);
((TextView) view2.findViewById(R.id.text)).setText("Upcomming");
((ImageView) view2.findViewById(R.id.image)).setImageResource(tabIcons[1]);
View view3 = inflater.inflate(R.layout.custom_tabs, null, false);
((TextView) view3.findViewById(R.id.text)).setText("Results");
((ImageView) view3.findViewById(R.id.image)).setImageResource(tabIcons[2]);
tabLayout.getTabAt(0).setCustomView(view1);
tabLayout.getTabAt(1).setCustomView(view2);
tabLayout.getTabAt(2).setCustomView(view3);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Ongoing(), "Ongoing");
adapter.addFrag(new Upcomming(), "Upcomming");
adapter.addFrag(new Result(), "Result");
viewPager.setAdapter(adapter);
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
return true;
}
return super.onOptionsItemSelected(item);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Implement OnPageChangeListener for ViewPager
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
Override onPageSelected methods
#Override
public void onPageSelected(int position) {
//This method will be invoked when a new page becomes selected.set your visiblity here with respect to tab
if( position == 0){
deadLine.setVisibility(View.VISIBLE);
}
else if(position == 1){
deadLine.setVisibility(View.GONE);
}
else if(position == 2){
deadLine.setVisibility(View.GONE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
getting default background behind tabs.add transparent background to AppBarLayout.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">

actionbar is not showing hide feature when i use grid layout with appbar layout

i am using AppBarLayout. it has a ViewPager in which i have added four fragments two fragments have recyclerView in them. and one has GridView. when i am on the fragment with RecyclerView the ActionBar hides when i scroll list up, but in case of gridview fragment this doesn't happen. ActionBar remains still at its position.
please help me! thanks in advance.
MainActivity
package com.bajpays.financemanager;
import android.support.design.internal.NavigationMenu;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TabLayout tabLayout;
DrawerLayout drawerLayout;
NavigationView navigationView;
ViewPager viewPager;
Toolbar toolbar;
CircleImageView img_user1,img_user2,img_user3,img_user4,img_user5,img_activeuser;
NavigationView navview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar=getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.grouppic);
actionBar.setDisplayShowHomeEnabled(true);
viewPager=(ViewPager)findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
if (viewPager!=null){
setUpViewPager(viewPager);
}
tabLayout=(TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
navigationView=(NavigationView) findViewById(R.id.navigatioview);
if (navigationView != null) {
setupDrawerContent(navigationView);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer);
}
navview=(NavigationView)findViewById(R.id.navigatioview);
View headerview=navview.getHeaderView(0);
RelativeLayout header=(RelativeLayout) headerview.findViewById(R.id.header);
img_activeuser=(CircleImageView)headerview.findViewById(R.id.img_icon_activeuser);
img_user1=(CircleImageView)headerview.findViewById(R.id.img_icon_user1);
img_user2=(CircleImageView)headerview.findViewById(R.id.img_icon_user2);
img_user3=(CircleImageView)headerview.findViewById(R.id.img_icon_user3);
img_user4=(CircleImageView)headerview.findViewById(R.id.img_icon_user4);
img_user5=(CircleImageView)headerview.findViewById(R.id.img_icon_user5);
img_user1.setTag(R.drawable.deep);
img_user2.setTag(R.drawable.grouppic);
img_user3.setTag(R.drawable.indelhi);
img_user4.setTag(R.drawable.kanishk);
img_user5.setTag(R.drawable.tshirt);
img_user1.setOnClickListener(this);
img_user2.setOnClickListener(this);
img_user3.setOnClickListener(this);
img_user4.setOnClickListener(this);
img_user5.setOnClickListener(this);
}
static class MyAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments=new ArrayList<>();
ArrayList<String> titles=new ArrayList<>();
public MyAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment,String title){
fragments.add(fragment);
titles.add(title);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
public void setUpViewPager(ViewPager viewPager){
MyAdapter adapter=new MyAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment_Home(),"Home");
adapter.addFragment(new Fragment_Catagory(),"Category");
adapter.addFragment(new Fragment_Top5(),"Top 5 Expences");
adapter.addFragment(new Fragment_Statistics(),"Statistics");
viewPager.setAdapter(adapter);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
}
}
Fragment with grid layout
package com.bajpays.financemanager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by Bajpay's on 7/22/2016.
*/
public class Fragment_Home extends Fragment {
View view;
ArrayList<Model_Grid> grids;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.layout_home, container, false);
grids=makegrids();
GridAdapter adapter=new GridAdapter(this.getActivity(),grids);
GridView gridView=(GridView) view.findViewById(R.id.gridview);
gridView.setAdapter(adapter);
Toast.makeText(this.getActivity(), "oncreate of home rns", 200).show();
return view;
}
public ArrayList<Model_Grid> makegrids()
{
ArrayList<Model_Grid> grids=new ArrayList<>();
grids.add(new Model_Grid(R.drawable.cheese_1, "food"));
grids.add(new Model_Grid(R.drawable.cheese_2,"cloth"));
grids.add(new Model_Grid(R.drawable.cheese_3,"outing"));
grids.add(new Model_Grid(R.drawable.cheese_4,"bank"));
grids.add(new Model_Grid(R.drawable.cheese_5,"travel"));
grids.add(new Model_Grid(R.drawable.cheese_1,"rent"));
grids.add(new Model_Grid(R.drawable.cheese_2,"health"));
grids.add(new Model_Grid(R.drawable.cheese_3,"makeup"));
grids.add(new Model_Grid(R.drawable.cheese_4,"home"));
grids.add(new Model_Grid(R.drawable.cheese_5,"daily"));
grids.add(new Model_Grid(R.drawable.cheese_1,"study"));
grids.add(new Model_Grid(R.drawable.cheese_5,"electronics"));
grids.add(new Model_Grid(R.drawable.cheese_4,"mobile"));
grids.add(new Model_Grid(R.drawable.ic_forum,"add new"));
return grids;
}
}
class GridAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
View view;
ImageView imageView;
TextView gridname;
ArrayList<Model_Grid> grids=new ArrayList<>();
GridAdapter(Context context,ArrayList<Model_Grid> gridlist){
this.context=context;
grids=gridlist;
inflater=LayoutInflater.from(context);
}
#Override
public int getCount() {
return grids.size();
}
#Override
public Object getItem(int position) {
return grids.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view=inflater.inflate(R.layout.dapter_for_home,null);
imageView=(ImageView)view.findViewById(R.id.grid_img);
gridname=(TextView)view.findViewById(R.id.grid_name);
imageView.setImageResource(grids.get(position).getImageid());
gridname.setText(grids.get(position).getGridName());
return view;
}
}
Layout of MainActivity
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#659eaf"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="enterAlways|scroll|snap" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff75cc"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigatioview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:menu="#menu/menu"/>
</android.support.v4.widget.DrawerLayout>
layout of fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:columnWidth="120dp"
android:stretchMode="spacingWidthUniform"
android:paddingBottom="50dp">
</GridView>
</LinearLayout>
it works only on Lollipop add this code-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
gridview.setNestedScrollingEnabled(true);
}
otherwise try to put your grid layout inside NestedScrollview. Hope this will help.

ViewPager with FragmentPagerAdapter

My adapter works fine but my fragment views are not inflated.
My main activity:
package com.example.rishi.flipper;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager= (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout= (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter viewpageradapter= new ViewPagerAdapter(getSupportFragmentManager());
viewpageradapter.addFragment(new Fragone(),"One");
viewpageradapter.addFragment(new FragTwo(),"Two");
viewpageradapter.addFragment(FragTwo.newInstance(),"Two");
viewPager.setAdapter(viewpageradapter);
}
}
The resource of the main activity :
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
>
<TextView
android:layout_width="wrap_content"
android:text="Toolbar"
android:height="24sp"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabs"
app:tabGravity="fill"
app:tabMode="scrollable"
></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:id="#+id/viewpager"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
And here's my adapter:
package com.example.rishi.flipper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> array=new ArrayList<>();
ArrayList<String> title=new ArrayList<>();
public ViewPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
return array.get(position);
}
public void addFragment(Fragment fragment, String text) {
array.add(fragment);
title.add(text);
}
#Override
public int getCount() {
return array.size();
}
#Override
public CharSequence getPageTitle(int position) {
return title.get(position);
}
}
I'm using Layout inflater to inflate a text view in fragment.
ViewPager cannot wrap_content for it's height as it can't know the maximum height of all of its children, as they may not all be instantiated at the same time (ViewPager creates and destroys its children as needed when scrolling between pages). It is possible changing to match_parent will fix this issue.
It is also a very bad idea to hold an ArrayList of Fragments as this will inevitably cause a memory leak, and crashes after process death in production. The intended behaviour for ViewPager is that you instantiate the fragments in getItem as they are needed.
If, after both of these changes, it still isn't inflating anything, it's likely that the issue is in the onCreateView code of your fragments.

Categories

Resources