I wanna create an activity that consist of 3 fragments. Problem is after adding views to fragment, in xml design section it shows normally but after running in actual device or emulator, views go invisible.I can click on (for example) EditText and keyboard shows itself, but i can not see that EditText!
fragment.xml code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".ProfileTab">
<EditText
android:id="#+id/edtProfileName"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="65dp"
android:layout_marginEnd="20dp"
android:background="#drawable/et_profiletab_custom"
android:ems="10"
android:hint="Profile Name"
android:inputType="textPersonName"
android:padding="15dp"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
.
.
.
activity.xml code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="#+id/fragment_holder"
tools:context=".SocialMediaActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<include
android:id="#+id/myToolbar"
layout="#layout/my_toolbar" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/purple_500"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabSelectedTextColor="#AEA0A0"
app:tabTextColor="#fff" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/appBarLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment.java code:
public class ProfileTab extends Fragment {
private EditText edtProfileName;
public ProfileTab() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_profile_tab, container, false);
edtProfileName = view.findViewById(R.id.edtProfileName);
//....
return view;
activity.java
public class SocialMediaActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager viewPager;
private TabLayout tabLayout;
private TabAdapter tabAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_social_media);
setTitle("Social Media App!!!");
toolbar = findViewById(R.id.myToolbar);
setSupportActionBar(toolbar);
viewPager = findViewById(R.id.viewPager);
tabAdapter = new TabAdapter(getSupportFragmentManager());
//get the data from tab that we have created from fragments and put it to viewPager
viewPager.setAdapter(tabAdapter);
tabLayout = findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager, false);
}
TabAdapter.java code: // java says that FragmentPagerAapter is deprecetad
public class TabAdapter extends FragmentPagerAdapter {
public TabAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int tabPosition) {
switch (tabPosition){
case 0:
return new ProfileTab();
.
.
.
}
}
#Override
public int getCount() {
return 3;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Profile";
.
.
.
}
}
}
I try fragment.xml root layout with RelativeLayout or disable actionbar replacement with toolbar or change theme of tablayout and toolbar but problem is exist.
How can i solve the problem?
change these lines in XML activity layout
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout" />
and also in AppBarLayout set layout height to:
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
Related
I've got simple tabLayout within Fragment file which is CoordinatorLayout.
It works good, when user swipes, different Fragments are displayed.
The problem is, tabs aren't respond for clicking, user cannot select particular tab,it has oportunity to swipe only
Below I launch files containing XML, ViewPager2 adapter, and Fragment.java in which I've implemented mechanism for swiping.
For global, in MainActivity I've implemented toolbar, that's why in Fragment's TabLayout I'm using margin top 50dp.
I also lunch its code.
<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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Widget.MyApp.TabLayout"
android:layout_marginTop="56dp"
android:layout_below="#+id/toolbar"
app:tabMode="fixed" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end|right">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/minusFab"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_remove_circle_outline_24px"
app:layout_constraintEnd_toEndOf="#id/plusFab"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/plusFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_marginTop="20dp"
android:layout_marginBottom="24dp"
android:src="#drawable/ic_add_circle_outline_24px"
app:layout_constraintBottom_toTopOf="#+id/mainFab"
app:layout_constraintEnd_toEndOf="#+id/mainFab"
app:layout_constraintTop_toBottomOf="#+id/minusFab" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/mainFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_expand_less_24px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I've configured all mechanisms responsible for tabs logic:
public class ViewPagerAdapter extends FragmentStateAdapter {
public ViewPagerAdapter(#NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
#NonNull
#Override
public Fragment createFragment(int position) {
switch (position)
{
case 0: return new FirstFragment();
case 1: return new SecondFragment();
case 2: return new ThirdFragment();
default:
return null;
}
}
#Override
public int getItemCount() {
return 3;
}
}
And implementation in Fragment.Java
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
ViewPager2 viewPager2 = view.findViewById(R.id.viewPager);
viewPager2.setAdapter(new ViewPagerAdapter(getActivity()));
TabLayout tabLayout = view.findViewById(R.id.tab_layout);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
#Override
public void onConfigureTab(#NonNull TabLayout.Tab tab, int position) {
switch (position){
case 0: tab.setText("First");
break;
case 1: tab.setText("Second");
break;
case 2: tab.setText("Third");
break;
}
}
}
);
tabLayoutMediator.attach();
// Inflate the layout for this fragment
return view;
}
}
MainActivity Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.FragmentsWithTabs.AppBarOverlay"
app:layout_constraintTop_toTopOf="parent">
<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/Theme.FragmentsWithTabs.PopupOverlay">
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="#+id/color_mode_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|end"
android:text="Switch here"
android:textAppearance="#style/TextAppearance.AppCompat.Small" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph"
tools:layout_editor_absoluteX="127dp"
tools:layout_editor_absoluteY="297dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
You have to add listener on tablayout to listen tab select event. Like,
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
//here you can write code to change page of viewpager based on tab id.
// like, viewpager.setCurrentItem(0);
}
});
Finally I've came with an answer. In this situation, ViewPager2 was expanded on all screen hence overlapping clicks on TabLayout :)
So solution was simple, now MainFragment's ViewPager2 code looks like follows:
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="629dp"
android:layout_gravity="bottom"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
I have this activity_category having a tabLayout. Inside Others tab I have a recyclerView which displays items from firebase like blog post example but I do not have Images, it is having only text views. It throws a null point exception even if I am initializing recyclerView inside OnCreate method
This is my activity_category
<RelativeLayout
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"
tools:context="com.solution.engineering.docket_ereceipt.CategoryActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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:layout_weight="1"
app:title="Categories">
</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:background="#color/colorPrimaryDark"
<android.support.design.widget.TabItem
android:id="#+id/tabItemHardware"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hardware" />
<android.support.design.widget.TabItem
android:id="#+id/tabItemStationary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stationary" />
<android.support.design.widget.TabItem
android:id="#+id/tabItemOthers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Others" />
</android.support.design.widget.TabLayout>
</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" />
</RelativeLayout>
Inside one of the tab item (i.e others) I have the RecyclerView which looks like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/relativeOthers">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/othersRecycler"
app:layoutManager="android.support.v7.widget.LinearLayoutManager">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Java file of activity_catagory uses switch cases to switch between tabs. This is small piece of code how its calling layouts from switch case
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
viewOthers tab6 = new viewOthers();
return tab6;
default:
return null;
}
}
The java file viewOthers() looks like this
public class viewOthers extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.others_layout, container, false);
return rootView;
}
}
I initialize recyclerView here in CategoryActivity.java
public class CategoryActivity extends AppCompatActivity {
public SectionsPagerAdapter mSectionsPagerAdapter;
private RecyclerView mReceipt;
private DatabaseReference mRef;
public ViewPager mViewPager;
public int extrasPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
//NOW HERE IT SHOWS NULL POINT EXCEPTION
mReceipt = (RecyclerView)findViewById(R.id.othersRecycler);
mReceipt.setHasFixedSize(true);
mReceipt.setLayoutManager(new LinearLayoutManager(this));
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
//display the desired fragment
mViewPager.setCurrentItem(extrasPosition);
}
I don't understand why? Any help is appreciated. Thanks in advance.
I have a base activity which has AppBarLayout with a toolbar and tablayout. There is a bottombar with contentframe to load the fragment. Now there is a fragment with which has a viewpager which will load the fragments of the selected tab.
Now the problem is that whenever I am loading the fragment in theviewpager, the height of the viewpager is 0. I am not able to see the loaded fragment.
If I specify height like 400dp or so to the viewpager then it is visible and works properly.
Toolbar xml:
<?xml version="1.0" encoding="utf-8"?>
<merge 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="wrap_content">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbarlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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:popupTheme="#style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
</merge>
Activity 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/activity_job_landing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".xxx.xxxx.xxxx">
<include
layout="#layout/layout_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
android:id="#+id/myScrollingContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/framelayout_joblanding_contentframe"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
<com.roughike.bottombar.BottomBar
android:id="#+id/bottom_bar_job_landing"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:background="#color/indigo900"
app:bb_activeTabAlpha="1"
app:bb_activeTabColor="#color/white"
app:bb_behavior="shy"
app:bb_inActiveTabAlpha="0.8"
app:bb_inActiveTabColor="#B6BDBF"
app:bb_tabXmlResource="#xml/job_landing_bottom_bar_tabs" />
</android.support.design.widget.CoordinatorLayout>
Fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xxx.xxxx.xxx"
android:background="#color/bluegrey50">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_job_applicant"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Fragment Java:
public class ApplicantFragment extends Fragment {
#BindView(R.id.viewpager_job_applicant)
ViewPager mViewPager;
//#BindView(R.id.tab_layout_job_applicant)
TabLayout mTabLayout;
public ApplicantFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_applicant, container, false);
ButterKnife.bind(this, view);
mTabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout_job_applicant);
mViewPager.setAdapter(new ApplicantFragmentPagerAdapter(getChildFragmentManager()));
mTabLayout.setupWithViewPager(mViewPager);
return view;
}
class ApplicantFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 7;
private final String tabTitles[] =
new String[]{"Fresher", "Next Round", "Hold",
"Reject", "TCFI", "CFI", "Hired"};
public ApplicantFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
CandidateListFragment candidateListFragment;
candidateListFragment = CandidateListFragment.newInstance();
return candidateListFragment;
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
}
Fragment that is loaded:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xxx.xxxx.xxxx">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
I am new to android.I created a tablayout and a viewpager with two fragments.I have elements/items in my created Detail Fragment and a TextView in my measure fragment but when i run the code on my mobile,nothing is shown in my fragment.I haven't java code of my fragment.Everything works fine on the tablayout..i can switch between the tab but nothing is being shown in my fragments...why is that happening?
My Activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.example.update">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<!--<include android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"/>-->
<include
android:id="#+id/update_screen_toolbar"
layout="#layout/toolbar_layout"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/name_age_gender"
android:background="#d3d3d3"
android:layout_below="#+id/update_screen_toolbar"/>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabLayout"
app:tabMode="fixed"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager">
</android.support.v4.view.ViewPager>
</RelativeLayout>
JAVA code for activity
package com.example.update;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class UpdateDetail extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
tabLayout=(TabLayout)findViewById(R.id.tabLayout);
viewPager=(ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new DetailFragment()," DETAIL");
viewPagerAdapter.addFragments(new MeasureFragment(),"MEASURE");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
code for DetailFragment:
<FrameLayout 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"
tools:context="com.example.DetailFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/expandableButton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#066da1"
android:drawableRight="#android:drawable/arrow_down_float"
android:onClick="expandableButton4"
android:paddingRight="10dp"
android:text="Expand/Collapse Android Example"
android:textColor="#fff" />
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="#+id/expandableLayout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/expandableButton4"
android:background="#90066da1"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="bounce"
app:ael_orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition.
Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition." />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
</FrameLayout>
Try this one:
public class FriendshipParentFragment extends Fragment {
#BindView(R.id.tabs)
TabLayout tabs;
#BindView(R.id.viewpager)
ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_friendship_parent, container, false);
ButterKnife.bind(this, view);
initTabs();
return view;
}
private void initTabs(){
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
tabs.setupWithViewPager(viewPager);
tabs.getTabAt(0).setText("Tab1");
tabs.getTabAt(1).setText("Tab2");
tabs.setTabTextColors(ContextCompat.getColor(getActivity(), R.color.colorAccent), ContextCompat.getColor(getActivity(), R.color.colorBottomNavigationPrimary));
tabs.setSelectedTabIndicatorColor(ContextCompat.getColor(getActivity(), R.color.white));
tabs.setSelectedTabIndicatorHeight(4);
}
public class TabsAdapter extends FragmentPagerAdapter {
public TabsAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FollowersFragment();
case 1:
return new FollowFragment();
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
}
xml file
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="owo.owocar.driver.OrdersParentFragment">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dp"
android:elevation="4dp"
app:tabGravity="fill" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:id="#+id/viewpager"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
I'm using a FragmentStatePagerAdapter to manage three tabbed Fragments that each have a ListView and a button. When the parent Fragment loads the via the ViewPager and PagerAdapter, the Button is missing but the ListView still shows. Switching to another tab in the Fragment causes the buttons to show up as they were supposed to.
Why isn't the button showing on the first view load?
public class DetailsPagerAdapter extends FragmentStatePagerAdapter {
private Creature creature;
public DetailsPagerAdapter(FragmentManager fm, Creature creature) {
super(fm);
this.creature = creature;
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return StatFragment.newInstance(creature);
case 1:
return ModFragment.newInstance(creature);
case 2:
return ResourceFragment.newInstance(creature);
}
return new Fragment();
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch(position) {
case 0:
return "Stats";
case 1:
return "Mods";
case 2:
return "Resources";
}
return "";
}
}
Details Fragment - Contains the tabs
public class CreatureDetailFragment extends Fragment {
private Creature creature;
private DetailsPagerAdapter detailsPagerAdapter;
private ViewPager viewPager;
private TabLayout tabs;
public CreatureDetailFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(Creature.ID_INTENT_MESSAGE)) {
this.creature = Creature.get(this.getContext(), getArguments().getLong(Creature.ID_INTENT_MESSAGE, 0l));
Activity activity = this.getActivity();
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (collapsingToolbarLayout != null) {
collapsingToolbarLayout.setTitle(this.creature.name);
}
this.detailsPagerAdapter = new DetailsPagerAdapter(this.getActivity().getSupportFragmentManager(), creature);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_creature_details_tabbed, container, false);
this.viewPager = (ViewPager) view.findViewById(R.id.viewPager);
this.viewPager.setAdapter(this.detailsPagerAdapter);
this.tabs = (TabLayout) view.findViewById(R.id.tabLayout);
this.tabs.setupWithViewPager(this.viewPager);
return view;
}
}
Parent layout that contains tabs
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:id="#+id/main_content"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:context=".DetailActivity">
<android.support.design.widget.AppBarLayout android:id="#+id/appbar"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/detail_toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay"
/>
<android.support.design.widget.TabLayout android:id="#+id/tabLayout"
android:layout_width="match_parent" android:layout_height="wrap_content"
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>
Tab Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabListView"
android:layout_alignParentTop="true"
android:layout_above="#+id/btnAddItem" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnAddItem"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="#d5d5d5"
android:background="#000"
android:textSize="24dp"
android:alpha=".7"
android:src="#drawable/ic_action_add" />
</RelativeLayout>
It seems that CoordinatorLayout wasn't the best layout to use for my tab fragment. The ListView was pushing the button out of the viewable space on first load and, for some reason, was only being rendered when redrawing the view.
I switched it to RelativeLayout and added some attributes to the ViewPager:
<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:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_below="#+id/appbar" />