Add Spinner as Tab item using design support library - android

I am using Design support library to create Tabs. I added Toolbar and Tab view layout.
I want to add Spinner widget as one of the TAB with design support library.
Is it possible to add Spinner as one of the TAB item? Can you give example?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rootLayout"
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="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:headerLayout="#layout/layout_drawer_header"
app:itemTextColor="#000000"
app:menu="#menu/global"></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
public class MainActivity extends AppCompatActivity {
private ActionBarDrawerToggle drawerToggle;
private DrawerLayout drawerLayout;
CoordinatorLayout rootLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
initToolbar();
//getSupportFragmentManager().beginTransaction().replace(R.id.flContent, new LettersFragment()).commit();
rootLayout = (CoordinatorLayout) findViewById(R.id.rootLayout);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.opendrawer,
R.string.closedrawer);
drawerLayout.setDrawerListener(drawerToggle);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 0:
showToast("One");
break;
case 1:
break;
case 2:
showToast("Three");
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.accent_material_light)), "ENGLISH");
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.ripple_material_light)), "TELUGU");
viewPager.setAdapter(adapter);
}
}

TabLayout tabLayout = findViewById(R.id.tab_layout);
TabLayout.Tab tab = tabLayout.newTab();
Spinner spinner = new Spinner(MainActivity.this);
// spinner.setAdapter(new SimpleAdapter());
tab.setCustomView(spinner);
tabLayout.addTab(tab);

Related

App crashes while clicking some tabs on tabLayout

I am working on an Android app, that would have 4 tabs with 4 fragments. I have run the app on my Android device. But the problem I am encountering is that, when I click on the first 2 tabs, it works fine, but when I open the 3rd and 4th tab, APP CRASHES IMMEDIATELY!
Initially, I thought there was a bug on the 3rd and 4th fragments, but I later discovered that this is not a 3rd or 4th fragment bug. I replaced the 1st and 2nd fragments with the 3rd and 4th fragment, and the 1st and 2nd fragments replaced with the 3rd and 4th fragments. And the same thing still happened!
This is my main_activity.java.
public class Main_activity extends AppCompatActivity {
#Bind(R.id.main_activity)
View mView;
private int[] tabIcons = {
R.drawable.friend_chat,
R.drawable.call,
R.drawable.tab_contact,
R.drawable.profile
};
private TabLayout tabLayout;
private FragmentManager mFragmentManager;
private FragmentTransaction mFragmentTransaction;
private NonSwipeableViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.viewpager, new ConversationsFragment()).commit();
viewPager = (NonSwipeableViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
int tabIconColor = ContextCompat.getColor(Main_Intergister.this, R.color.holo_red_dark);
tabLayout.getTabAt(0).getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
ColorStateList colors;
if (Build.VERSION.SDK_INT >= 23) {
colors = getResources().getColorStateList(R.color.ash, getTheme());
}
else {
colors = getResources().getColorStateList(R.color.ash);
}
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
assert tab != null;
Drawable icon = tab.getIcon();
if (icon != null) {
icon = DrawableCompat.wrap(icon);
DrawableCompat.setTintList(icon, colors);
}
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
switch (tab.getPosition()) {
case 0:
viewPager.setCurrentItem(0);
break;
case 1:
viewPager.setCurrentItem(1);
break;
case 2:
viewPager.setCurrentItem(2);
case 3:
viewPager.setCurrentItem(3);
break;
default:
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(Color.parseColor("#a0a3a7"), PorterDuff.Mode.SRC_IN);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(NonSwipeableViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ConversationsFragment(), "Chats");
adapter.addFragment(new ContactsFragment(), "Calls");
adapter.addFragment(new CallsFragment(), "Contacts");
adapter.addFragment(new Profile_fragment(), "Profile");
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 null;
}
}
}
This is my main_activity.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"
android:id="#+id/main_activity"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:id="#+id/appbar">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="0dp">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:background="#color/white"
app:layout_scrollFlags="scroll|enterAlways"
app:elevation="6dp"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toolbar_app_name"
android:text="#string/app_name"
android:textColor="#color/holo_red_dark"
android:textSize="30sp"
android:textStyle="bold"/>
</android.support.v7.widget.Toolbar>
</android.support.v7.widget.CardView>
</android.support.design.widget.AppBarLayout>
<com.solid.helpers.NonSwipeableViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="0dp">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabIndicatorColor="#color/white"
app:tabIndicatorHeight="8dp"
app:elevation="6dp"
app:tabGravity="fill"
android:background="#color/white"/>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Since you have 4 tabs, please try adding the following code under main_activity onCreate event and see would it solve your issue.
viewpager.setOffscreenPageLimit(4);

Android studio ActionBar with Tabs inside a fragment

I have created a project with the navigation drawer template and I have different fragments for each navigation item.
I have an ActionBar with Tabs on one of the fragments and I have set the width to match parent but when I run it the tabs doesn't stick to the actionbar and the width is also not matching the parent. check the code below:
XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yardbird.justice.yardbird.Fragments.DrinksFragment">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="47dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
Java:
public class DrinksFragment extends Fragment {
public DrinksFragment() {
// Required empty public constructor
}
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_drink, container, false);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Frabment 1"));
tabLayout.addTab(tabLayout.newTab().setText("Frabment 1"));
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getFragmentManager(),
tabLayout.getTabCount()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
private class CustomAdapter extends FragmentStatePagerAdapter {
int numberOfTabs;
public CustomAdapter(FragmentManager fragmentManager, int numberOfTabs) {
super( fragmentManager);
this.numberOfTabs = numberOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
Tab1Fragment tab1 = new Tab1Fragment();
return tab1;
case 1:
Tab2Fragment tab2 = new Tab2Fragment();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return numberOfTabs;
}
}
}
Screen Shot of the app:
Can anyone help fix that?
Try 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"
android:orientation="vertical"
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="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

How to create a Fragment over the Viewpager in android?

Explanation:
I tried more but i failed to find the how to set the transparent fragment over the Viewpager. I am working in application. The requirement of my client is who will make a application like below url
https://play.google.com/store/search?q=munchery&hl=en
In above url application. In this application they were added more functionality like navigationDrawer, Tabs with viewpager and etc.
One thing is when i click on the actionBar Today's menu it's open something. I am not able to recognized to find what it actually open while i click on actionBar in the Munchery application.
I tried to implement but not implemented as same as the munchery application.
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.millu.whatsappdemo.MainActivity">
<android.support.design.widget.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" >
<TextView
android:id="#+id/action_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:textSize="20sp"
android:clickable="true"
android:textColor="#FFFFFF"
android:text="Text view"
android:drawableRight="#drawable/ic_up_arrow">
</TextView>
</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/colorPrimary"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<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>
MainActivity.java
public class MainActivity extends AppCompatActivity {
public TextView action_text;
public Toolbar toolbar;
TabLayout tabs;
ViewPager viewPager;
List<Fragment> fragmentList;
List<String> tab_title;
boolean flag=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabs = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
action_text = (TextView) toolbar.findViewById(R.id.action_text);
fragmentList = new ArrayList<>();
tab_title = new ArrayList<>();
tab_title.add("ONE");
tab_title.add("TWO");
fragmentList.add(new OneFragment());
fragmentList.add(new TwoFragment());
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList, tab_title);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(0);
tabs.setupWithViewPager(viewPager);
action_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment fragment=new ThirdFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
if(!fragment.isHidden()) {
fragmentTransaction.replace(R.id.container,fragment);
flag=true;
}
else{
fragmentTransaction.remove(fragment);
flag=false;
}
fragmentTransaction.commit();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
In above code i set the flat onClick of the textview over the actionBar for hiding and showing the fragment. In my demo the problem is it show the tab when my third fragment is open and it does not hide.
Please help me to recognized what is it open on click of the actionBar?
Try the code as below: activity_main.xml
<?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=".ScrollingActivity">
<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">
<TextView
android:id="#+id/action_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:clickable="true"
android:drawableRight="#mipmap/ic_launcher"
android:gravity="center"
android:text="Text view"
android:textColor="#FFFFFF"
android:textSize="20sp"></TextView>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tabs"
android:background="#android:color/white" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/reltv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:background="#80ffffff"
android:visibility="gone">
<RelativeLayout
android:id="#+id/reltv2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Do whatever you like it here."
android:textAppearance="?attr/textAppearanceLargePopupMenu" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
And your ManiActivity.java as below:
public class MainActivity extends AppCompatActivity {
public TextView action_text;
public Toolbar toolbar;
TabLayout tabs;
ViewPager viewPager;
List<Fragment> fragmentList;
List<String> tab_title;
boolean flag = false;
RelativeLayout reltv;
RelativeLayout reltv2;
boolean isShown = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabs = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
action_text = (TextView) toolbar.findViewById(R.id.action_text);
reltv = (RelativeLayout) findViewById(R.id.reltv);
reltv2 = (RelativeLayout) findViewById(R.id.reltv2);
fragmentList = new ArrayList<>();
tab_title = new ArrayList<>();
tab_title.add("ONE");
tab_title.add("TWO");
fragmentList.add(new OneFragment());
fragmentList.add(new TwoFragment());
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList, tab_title);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(0);
tabs.setupWithViewPager(viewPager);
action_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isShown) {
isShown=true;
reltv.setVisibility(View.VISIBLE);
}else {
isShown=false;
reltv.setVisibility(View.GONE);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_scrolling, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
First, you should use some translucent or transparent colour for the android: background of parent's attribute in the fragment layout e.g DayFragment . This is the fragment you will inflate in the overlay FrameLayout.
For a smooth inflation of that fragment, you can use custom animation to make the transition smooth like in the Munchery app(uses slide from top animation)
protected void replaceFragment(int containerViewId, Fragment fragment, boolean isAddToBackStack, boolean isAnimateTransition) {
String backStateName = fragment.getClass().getName();
FragmentManager fm = this.getSupportFragmentManager();
boolean fragmentPopped = fm.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped && getSupportFragmentManager().findFragmentByTag(backStateName) == null) {
FragmentTransaction ft = fm.beginTransaction();
if(isAnimateTransition) {
animateFragmentTransition(ft);
}
ft.replace(containerViewId, fragment, backStateName);
if (isAddToBackStack) {
ft.addToBackStack(backStateName);
}
ft.commit();
}
}
private void animateFragmentTransition(FragmentTransaction ft) {
ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.slide_in_from_top, R.anim.slide_out_to_top);
}
DayFragment fragment = DayFragment.newInstance(null);
replaceFragment(R.id.container, fragment, false, true);
Alternitively, you can use ObjectAnimators to revael and hide views as you wish. For example:-
private void animate() {
View viewToShiftOut = mNormalModelayoutContainer;
View viewToShiftIn = mEditModelayoutContainer;
ObjectAnimator outAnim = ObjectAnimator.ofFloat(viewToShiftOut, "y", 0, -heightOfScreen);
ObjectAnimator inAnim = ObjectAnimator.ofFloat(viewToShiftIn, "y", heightOfScreen , 0);
outAnim.setDuration(1000);
inAnim.setDuration(1000);
outAnim.start();
inAnim.start();
}

Replace Fragment: IllegalArgumentException: No view found for id

All of my activities extend MainActivity and share activity_main layout. In 1 of my activities, I'm using the slidinguppanel library, so the layout is different. When I try to attach the fragment after adding the layout view, I get error:
10-16 15:25:14.155 32590-32590/com.myproject.myapp E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.myapp/com.myproject.myapp.search.SearchActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0d00ea (com.myproject.myapp/searchContainer) for fragment SearchFragment{2e02f51a #0 id=0x7f0d00ea SearchFragment}
What am I doing wrong?
MainActivity.java oncreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_hamburger_icon);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
mMainContainer = (FrameLayout) findViewById(R.id.mainContainer);
mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
mAppBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mUserImageView = (RoundedImageView) navigationView.findViewById(R.id.userImageView);
mRealNameView = (TextView) navigationView.findViewById(R.id.realNameText);
mUserNameView = (TextView) navigationView.findViewById(R.id.userNameText);
showUserInfo(mUser != null);
}
activity_main.xml:
<?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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/drawer_items" />
</android.support.v4.widget.DrawerLayout>
SearchActivity.java onCreate (I marked the line having the problem):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
mDrawerLayout.removeView(mCoordinatorLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.search_appbar_layout, mDrawerLayout, false);
mSlidingPanel = (SlidingUpPanelLayout) view.findViewById(R.id.sliding_layout);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
mDrawerLayout.addView(view);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggleDrawer();
}
});
SearchFragment searchFragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag("SearchFragment");
if (searchFragment == null) {
FrameLayout searchContainer = (FrameLayout) view.findViewById(R.id.searchContainer);
searchContainer.setId(R.id.searchContainer);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.searchContainer, SearchFragment.newInstance(), "SearchFragment") // here's the problem
.commit();
}
viewPager = (ViewPager) view.findViewById(R.id.vp_searchResultViewPager);
SearchResultPagerAdapter pagerAdapter = new SearchResultPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
TabLayout tabStrip = (TabLayout) view.findViewById(R.id.tl_searchResultTabs);
tabStrip.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_SETTLING) {
switch (viewPager.getCurrentItem()) {
case 1:
if (foursquareResultsObject != null) {
EventBus.getDefault().post(new PopulateSearchMapEvent(foursquareResultsObject));
}
break;
default: //case 0
if (foursquareResultsObject != null) {
EventBus.getDefault().post(new PopulateSearchListEvent(foursquareResultsObject));
}
break;
}
}
}
});
}
search_appbar_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoDragView="#+id/toolbar"
sothree:umanoInitialState="expanded"
sothree:umanoOverlay="true"
sothree:umanoPanelHeight="?attr/actionBarSize"
sothree:umanoParallaxOffset="0dp"
sothree:umanoShadowHeight="4dp">
<!-- MAIN CONTENT -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:paddingBottom="?attr/actionBarSize">
<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.design.widget.TabLayout
android:id="#+id/tl_searchResultTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/vp_searchResultViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
<!-- SLIDING LAYOUT -->
<LinearLayout
android:id="#+id/ll_searchDragView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:bar="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:gravity="end|center_vertical"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingStart="#dimen/activity_horizontal_margin"
bar:navigationIcon="#drawable/ic_action_hamburger_icon"
bar:theme="#style/ThemeOverlay.AppCompat.ActionBar"
bar:title="#string/search_venues"
bar:titleTextAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/searchContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
In your SearchActivity you are using the activity_main layout instead of the search_app_bar_layout where you have the fragment container.
Change from this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_appbar_layout);

How to add Material design Navigation drawer with sliding tabs?

I want something very similar to this:
I have already added Sliding tabs and it's working perfectly fine but I am not able to figure out how to add a navigation drawer to existing code.
So far, I have created a PagerAdapter class to view different Fragment class. Following is my MainActivity class:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu_main.xml
<menu 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"
tools:context=".MainActivity">
......
......
<item
android:id="#+id/action_user"
android:orderInCategory="300"
android:title="User"
android:icon="#drawable/ic_drawer"
app:showAsAction="ifRoom"></item>
</menu>
In above code I added a icon for open drawer. This my Ëšactivity_main.xml
<RelativeLayout
android:id="#+id/main_layout"
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">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout"/>
</RelativeLayout>
Any hint how to add Navigation Drawer to android bar ?
here is useful template for Android Studio, which adds Material Design Navigation Drawer
UPD
To add sliding tabs, include ViewPager with PagerTabStrip in activity_main layout file
<android.support.v4.view.ViewPager
android:id="#+id/tabanim_viewpager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#color/primary_500"
android:textColor="#color/white"
android:paddingTop="5dp"
android:layout_marginBottom="5dp" />
</android.support.v4.view.ViewPager>
activity onCreate:
PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_title_strip);
pagerTabStrip.setDrawFullUnderline(false);
pagerTabStrip.setTabIndicatorColor(getResources().getColor(R.color.red_dark));
final ViewPager viewPager = (ViewPager) findViewById(R.id.tabanim_viewpager);
setupViewPager(viewPager);
Code in Activity:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
//adding tabs
adapter.addFrag(new PinsFragment(), "Pins");
adapter.addFrag(new BoardsFragment(), "Boards");
adapter.addFrag(new InterestsFragment(), "Likes");
adapter.addFrag(new SubscriptionsFragment(), "Subscriptions");
adapter.addFrag(new FollowersFragment(), "Followers");
viewPager.setAdapter(adapter);
}

Categories

Resources