Displaying TabLayout and fragments with ViewPager inside card view - android

I want to display two fragments in a cardview, i was able to display tablayout but not the fragments, tried everything, help me as this might be simple I guess. I uploaded screenshot as well, as seen from the screenshot tablayout is displayed correctly but not the fragment below it
Code in the Activity.
....
//Creating our pager adapter
Pager adapter = new Pager(getSupportFragmentManager(), Titles, 2);
...
viewPager = (ViewPager) findViewById(R.id.srviewpager);
//Adding adapter to pager
viewPager.setAdapter(adapter);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
..
public class Pager extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
private static final String TAG = "ViewPagerAdapter";
// Build a Constructor and assign the passed Values to appropriate values in the class
public Pager(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//Overriding method getItem
#Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
return new ShippingFragment();
case 1:
return new ReturnsFragment();
}
return null;
}
// Overriden method getCount to get the number of tabs
#Override
public int getCount() {
return NumbOfTabs;
}
#Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
}
this is my layout file, contains 3 cardview, the last cardview has to display two fragments, shipping and returns. So far I am able to display only tablayout with two tabs but not the fragments.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/description_cd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
cardview:cardCornerRadius="8dp"
cardview:cardElevation="15dp"
cardview:cardBackgroundColor="#color/cardview_light_background"
cardview:contentPadding="2dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/features_cd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
cardview:cardCornerRadius="8dp"
cardview:cardElevation="15dp"
cardview:cardBackgroundColor="#color/cardview_light_background"
cardview:contentPadding="2dp"
android:layout_below="#+id/description_cd"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_margin="8dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/sp_cd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
cardview:cardCornerRadius="8dp"
cardview:cardElevation="15dp"
cardview:cardBackgroundColor="#color/cardview_light_background"
cardview:contentPadding="2dp"
android:layout_below="#+id/features_cd"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_margin="8dp">
<com.spoorthy.apsaratrendz.thirdpartyviews.RippleView
android:id="#+id/shipping_payment_rpv"
android:layout_width="match_parent"
android:layout_height="match_parent"
cardview:rv_color="#color/green"
cardview:rv_type="rectangle"
cardview:rv_rippleDuration="#integer/ripduration_rpv">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/shippingandreturns"
android:id="#+id/textViewsp"
android:layout_alignParentTop="true"
android:singleLine="true"
android:textSize="25sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:textColor="#d52e9e"
android:inputType="text"
android:padding="1dp"
android:paddingEnd="1dp"
android:paddingStart="1dp" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
android:layout_below="#+id/textViewsp"/>
<android.support.v4.view.ViewPager
android:id="#+id/srviewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tabs"/>
</com.spoorthy.apsaratrendz.thirdpartyviews.RippleView>
</android.support.v7.widget.CardView>
</RelativeLayout>
Here are my two fragment layouts
shippingfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<com.spoorthy.apsaratrendz.thirdpartyviews.ReadMoreTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text="#string/returns"
android:layout_margin="4dp"
android:padding="4dp" />
returnsfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<com.spoorthy.apsaratrendz.thirdpartyviews.ReadMoreTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text="#string/returns"
android:layout_margin="4dp"
android:padding="4dp" />
And my two fragment java files..
public class ReturnsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.returnsfragment, null);
return v;
}
}
public class ShippingFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.shippingfragment, null);
return v;
}
}

Solved the problem by changing the CardView layout height to 350dp, instead of match_parent.
<android.support.v7.widget.CardView
android:id="#+id/sp_cd"
android:layout_width="match_parent"
android:layout_height="350dp".../>

Related

Fragment components are invisible in actual running app

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">

How to place PagerTabStrip in single page

I am trying to design this page
but I have design like this ( both fragment are there but the pagertabstrip of unseen page only see when I moved to this page )
Issue Facing
How would I set the width of the PagerTabStrip so that both login and register( highlighted in the first image) is visible in the same page.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:weightSum="2"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_weight="1.6"
android:gravity="center"
android:id="#+id/logo"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="25dp"
android:gravity="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold"
android:text=""
android:visibility="visible"
android:src="#drawable/logo_new"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView8" />
</LinearLayout>
<LinearLayout
android:layout_weight="0.4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
app:tabMode="fixed"
android:id="#+id/vpPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
app:tabMode="fixed"
android:id="#+id/pager_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="4dp"
android:paddingTop="4dp" />
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
Java File
public class Authentication extends AppCompatActivity {
FragmentPagerAdapter adapterViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authentication);
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Boolean login_already=app_preferences.getBoolean("login",false);
if (login_already)
{
startActivity(new Intent(Authentication.this,MainActivity.class));
finish();
}
ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);
// Attach the page change listener inside the activity
vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected.
#Override
public void onPageSelected(int position) {
Toast.makeText(Authentication.this,
"Selected page position: " + position, Toast.LENGTH_SHORT).show();
}
// This method will be invoked when the current page is scrolled
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Code goes here
}
// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
#Override
public void onPageScrollStateChanged(int state) {
// Code goes here
}
});
}
public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 2;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return Login.newInstance(0, "Login");
case 1: // Fragment # 0 - This will show FirstFragment different title
return Registration.newInstance(1, "Register");
default:
return null;
}
}
// Returns the page title for the top indicator
#Override
public CharSequence getPageTitle(int position) {
if (position==0)
return "Login";
else
return "Register";
//return "Page " + position;
}
}
}
That layout xml worked for me...
EDIT: Oh sry i'm using another lib:
https://github.com/jpardogo/PagerSlidingTabStrip
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.astuetz.PagerSlidingTabStrip
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:textColor="#FFFFFFFF"
app:pstsDividerColor="#FFFFFFFF"
app:pstsDividerPadding="10dp"
app:pstsDividerWidth="1dp"
app:pstsIndicatorColor="#FF33B5E6"
app:pstsIndicatorHeight="5dp"
app:pstsTabPaddingLeftRight="20dip"
app:pstsUnderlineColor="#FF33B5E6">
</com.astuetz.PagerSlidingTabStrip>
<FrameLayout
android:id="#+id/movie_framelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="#+id/movie_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
</FrameLayout>
When you want to display only selected tab and other will hide then set your PagerTabStrip as bellow.
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="4dp"
android:paddingTop="4dp"
app:tabMode="scrollable"
app:tabTextColor="#000000"
app:tabSelectedTextColor="#ff0000"
app:tabIndicatorColor="#ff0000"/>
app:tabTextColor="" is your normal tab text color.
app:tabSelectedTextColor="" is color of your selected tab.
app:tabIndicatorColor="" is your selected tab indicatore color.
So use as per your requirement. For detail that how to use tab layout then check this tutorial.

Android ViewPager inside a ScrollView isnt scrolling

I have a ViewPager linked with a TabLayout and they are both inside a ScrollView the ViewPager has a PagerAdapter with views added at runtime but the problem is that the ScrollView doesn't scroll vertically, I tried using different height for the ViewPager and the TabLayout but it didn't work I checked out a bunch on question on the subject but none of the recommendations worked for me the ViewPager has a list of views but I dont want to use a Recycler view or a ListView because I want the whole screen to scroll and not just the ViewPager.
here's the xml for the fragment
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/white"
app:tabTextColor="#color/dark_gray"
/>
<android.support.v4.view.ViewPager
android:id="#+id/character_info_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
/>
</android.support.design.widget.AppBarLayout>
</ScrollView>
The xml layout for the list items
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/list_item_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:layout_weight="5"
tools:src="#color/dark_gray"
/>
<TextView
android:id="#+id/list_item_name_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingTop="18dp"
android:paddingBottom="18dp"
android:layout_marginLeft="6dp"
tools:text="Item Name goes here"
/>
</LinearLayout>
and the ViewPager's PagerAdapter (where I add the views at runtime)
private class MyPagerAdapter extends PagerAdapter
{
#Override
public Object instantiateItem(ViewGroup container, int position)
{
LinearLayout itemsContainer = new LinearLayout(getActivity());
itemsContainer.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < ProfileInfoItems.getSize(position); i++)
{
LinearLayout item = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.list_item_info_view, container, false);
TextView itemTitle = (TextView) item.findViewById(R.id.list_item_name_text_view);
ImageView itemIcon = (ImageView) item.findViewById(R.id.list_item_icon);
itemTitle.setText(ProfileInfoItems.getName(position, i));
itemIcon.setImageDrawable(getResources().getDrawable(ProfileInfoItems.getIcon(position, i)));
itemsContainer.addView(item, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
itemsContainer.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
container.addView(itemsContainer);
return itemsContainer;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == object;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((View) object);
}
#Override
public int getCount()
{
return TAB_LABELS.length;
}
}
A screen shot
Thanking you all in advance for your time and effort :)

Android UI Elements in tabbed view not loading

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" />

Android Fragments drag or move to left to right or right to left

My requirement is need to adjust or move left to right or right to left fragment. My main layout contain two fragment. I need to drag & resize the fragment width like android setting screen / message (on tablet) divider moving right to left or left to right.
My application consider only tablet only. & those two fragment are independently (not master detail)
courses_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:baselineAligned="false" >
<fragment
android:id="#+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.mobile.courses.CousesFragment" ></fragment>
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:background="#color/list_divider_color" />
<fragment
android:id="#+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.mobile.whathappenfeed.DiscussionFeedFragment" ></fragment>
</LinearLayout>
CoursesActivity
public class CoursesActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.courses_main);
}
}
This my `CourseFragment.java
public class CousesFragment extends Fragment {
private List<Course> courses;
private RefreshableListViewContainer refresh;
private RefreshableListView listView;
protected CourseArrayAdapter courseAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.courses_fragment, container,
false);
refresh = (RefreshableListViewContainer) view
.findViewById(R.id.pull_to_refresh_list);
listView = (RefreshableListView) refresh
.findViewById(android.R.id.list);
// Set a listener to be invoked when the list should be refreshed.
refresh.setOnRefreshListener(new OnRefreshListener() {
public void onRefresh(boolean loading) {
refresh.finishLoading();
reloadCurrentCourses();
}
});
loadAndDisplayCourses();
return view;
}
This is my DiscussionFragment.java
public class DiscussionFeedFragment extends ListFragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.discussion_fragment, container,false);
return view;
}
This is my course_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<include
android:id="#+id/list_header_item"
layout="#layout/list_header_item" />
<View
android:padding="4dip"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#color/list_divider_color" />
<include
android:id="#+id/empty_courses"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout="#layout/empty_courses"
android:visibility="gone" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<com.mobile.widget.RefreshableListViewContainer
android:id="#+id/pull_to_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.mobile.widget.RefreshableListViewOverflowItem
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.mobile.widget.RefreshableListViewItem
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.mobile.widget.RefreshableListView
android:id="#android:id/list"
style="#style/MotorolaListViewFooterFix"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:cacheColorHint="#00000000"
android:paddingLeft="10dip"
android:paddingTop="10dip"
android:divider="#drawable/list_item_divider"
android:dividerHeight="3dp"
android:drawSelectorOnTop="true"
android:fadingEdge="none"
android:smoothScrollbar="true"
android:fastScrollEnabled="false"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:listSelector="#drawable/item_background_holo_light"
android:textFilterEnabled="true" />
</com.mobile.widget.RefreshableListViewContainer>
</FrameLayout>
</LinearLayout>
I need to adjust fragment let to right or right to left. Like master/detail or setting screen. But I didn't create as master detail application. How to do this part?
In your activity class inside onCreate() method add this code segment,
viewPager=(ViewPager) findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
Then create MyPagerAdapter private class in same activity class,
private class MyPagerAdapter extends FragmentPagerAdapter{
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0: return new AFragment();
case 1: return new BFragment();
default: return null;
}
}
#Override
public int getCount() {
return 2;
}
}
Update your activity layout adding this:
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

Categories

Resources