How to customize android tabs without changing the actionBar? (using ViewPager) - android

I'm not sure if that is possible. I'm trying to do a screen with tabs and lists in every tabs (I've done that already) and I've done a custom ActionBar for the title and some icons on it, now, i want to change the text color of the tabs, I've solved the background color but i can't find anything related to the text color, here is a piece of my code .java and my manifest.xml
addMenu();
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
actionBar.show();
actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_CUSTOM);
// 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.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this)
);
}
the addMenu method (Only changes the Action Bar):
getSupportActionBar().setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.order_title, null);
TextView textView = (TextView) view.findViewById(R.id.mytext);
textView.setWidth(getWindow().getWindowManager().getDefaultDisplay().getWidth());
textView.setPadding(0, 0, 50, 0);
textView.setGravity(Gravity.CENTER);
Typeface segoeui = Typeface.createFromAsset(getAssets(),"fonts/segoeui.ttf");
textView.setTypeface(segoeui);
textView.setText("Some Text");
getSupportActionBar().setCustomView(view);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrow_left);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#afafaf")));
and my Manifest.xml:
<activity
android:name=".Activity1"
android:label="#string/title_activity1"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cleanpress.cleanpress.MainActivity" />
</activity>
This is how it looks:
http://i.stack.imgur.com/0hRbv.png
As you can see, the screen that i'm working is related to another one (if that's relevant).
I want to change just the text color of the Tabs (there are 3 tabs), anyone can help me please?
(I've already worked with HTML format and didn't work)

You should use android.support.design.widget.TabLayout to handle Tabs
so you can adjust:
app:tabIndicatorColor="#color/colorAccent" //indicator color
app:tabSelectedTextColor="#EEE" // text color on selected tab
app:tabTextColor="#EEE" // text color on tab
you should also use android.support.v7.widget.Toolbar for ActionBar.
Exmaple:
<RelativeLayout
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"
tools:context=".MainActivity"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_alignParentTop="true"
android:text="#string/app_name"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_below="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:tabIndicatorColor="#color/colorAccent"
app:tabSelectedTextColor="#EEE"
app:tabTextColor="#EEE" />
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tab_layout" />
</RelativeLayout>
In activity in onCreate
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
mAdapter = new YourPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mAdapter);
mTabLayout.setupWithViewPager(mPager);
Remember to add
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:support-v4:23.0.0'
in gradle dependencies

try this in styles
<style name="MyTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarTabTextStyle">#style/Widget.MyTabText</item>
</style>
<style name="Widget.MyTabText" parent="#style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">#color/#your_color</item>
</style>

Related

ActionBar with Tabs

Hello I am trying to put a TabLayout inside the ActionBar, not below it. Is it possible to be done?
I have searched around and found the ActionBar.Tab class, but it is already deprecated. Any other ideas please?
You can create your own Layout with back button and Tab Layout. You can use this layout at top with no ActionBar. On Back click programmatically call onBackPressed() method.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="48dp"
android:layout_height="48dp"
android:background="?selectableItemBackgroundBorderless"
android:src="#drawable/ic_back" />
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I used the library below for tabs in example code. (Add the line to your app's gradle)
compile 'com.jpardogo.materialtabstrip:library:1.1.0'
And than you can use a toolbar layout like below:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
style="#style/Widget.MyApp.Toolbar.Solid" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:layout_below="#+id/toolbar">
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:background="#color/AppPrimary"
app:pstsIndicatorColor="#fff"
app:pstsIndicatorHeight="4dp"
app:pstsTabTextAllCaps="false"
app:pstsTabTextColor="#color/tabTextColor"
app:pstsTabTextSize="16sp"
app:pstsUnderlineColor="#color/AppPrimary"
app:pstsUnderlineHeight="4dp" />
</RelativeLayout>
</RelativeLayout>
Here are the styles i used in example layout:
<style name="Widget.MyApp.Toolbar.Solid" parent="Widget.AppCompat.ActionBar">
<item name="android:background">#color/AppPrimary</item>
<item name="android:theme">#style/ThemeOverlay.MyApp.ActionBar</item>
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">#dimen/abc_action_bar_default_height_material</item>
</style>
<style name="ThemeOverlay.MyApp.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar">
<!-- Parent theme sets colorControlNormal to textColorPrimary. -->
<item name="android:textColorPrimary">#android:color/white</item>
</style>
In your activity's layout you have to include your toolbar like below:
<include
android:id="#+id/toolbar_main"
layout="#layout/toolbar_tabs" />
And you have to set your toolbar as support actionbar and to use it as an actonbar.
Toolbar mToolbar = (Toolbar)findviewById(R.id.toolbar_main);
setSupportActionBar(mToolbar);
Add a viewpager to your activity's layout: (Where you add your toolbar)
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
/>
Create an adapter for your viewpager like below:
public class TabsAdapter extends FragmentPagerAdapter {
private List<YourModel> mObjects;
public TabsAdapter(FragmentManager fm, List<YourModel> mObjects) {
super(fm);
this.mObjects = mObjects;
}
#Override
public Fragment getItem(int position) {
return new YourPagerFragment();
}
#Override
public int getCount() {
return mObjects.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mObjects.get(position).getName();
}
}
Finally, find your viewpager, pager sliding tab strip, create a new pager adapter and set it to your viewpager like below:
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
PagerSlidingTabStrip mTabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
TabsAdapter mAdapter = new TabsAdapter(getSupportFragmentManager(), objects);
mViewPager.setAdapter(mAdapter);
mTabs.setViewPager(mViewPager);
Good luck.

Using navigationView with tabLayout

So I've recently been working on updating my app to use the new material design support library. My application has one main activity with a drawerLayout and navigation view. The main content of app is shown in a frameLayout, through fragments. However, I am trying now to add material tabs to one of the navigation drawer's fragments. However, I am not sure how to implement this while keeping my fragments in the nav drawer functioning. A good example of what I am trying to achieve is shown below:
In this app (Google play music), only some of the navigation drawer's items have tabs while others do not. So my question is, how would I implement this? (Not looking for code, just an overview of how my layout should be organized)
To recap/clarify:
I have a main layout with a frameLayout (for my app's content), and a navigationView (for navigating the different item fragments). I then have a listener which replaces the main layout's frameLayout with the item's respective fragment. Now, I need to add tabs to just one of these fragments (to navigate between 4 other fragments). I am also using a toolbar which I include as a separate layout.
Any advice is appreciated. I'm sorry if my description is a little confusing; I will clarify any necessary details.
Ok suppose your NavigationView has two options, the first one displays the fragment with tabs (tab layout) and the second one displays just a fragment with a toolbar. You have two options then:
You can have a main layout with just a frame layout and replace it with all what you want
You can have a main layout with coordinator layout -> app bar -> toolbar -> tab layout and a frame layout to put content
I prefer the second option to avoid having to always configure the toolbar so this is what I did once:
<!-- layout_main -->
<android.support.v4.widget.DrawerLayout
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:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
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.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:visibility="gone"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<!-- The NavigationView -->
<fragment
android:id="#+id/navigation_fragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="some.path.to.the.NavigationViewFragment"
tools:layout="#layout/fragment_navigation_view" />
</android.support.v4.widget.DrawerLayout>
As you see I change the visibility of TabLayout to "gone" so that the fragment with tabs take care to set as visible. The Fragment with the tabs just have the ViewPager in the Layout:
<!-- fragment_with_tabs -->
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Now the fragment with tabs initialize the ViewPager with the fragments for each page:
#Override
public onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// The getChildFragmentManager() is Very important! Because fragments inside fragments are
// not supported with the tipical fragmentManager, it requires NestedFragments and those
// uses a childFragmentManager(). In other case a strange behaviour occurs
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new TabOneFragment(), "Tab 1");
adapter.addFragment(new TabTwoFragment(), "Tab 2");
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout);
tabLayout.setVisibility(View.VISIBLE);
tabLayout.setupWithViewPager(viewPager);
}
And finally do whatever you want in your TabFragments, this works fine for me and I hope this be useful for you too. Sorry for some problem with code syntax, I develop android with Kotlin and not with Java.
I can't recommend what Yiyo suggested. If you are going to have Fragments with different layouts, you should let the Fragments customize these layouts in the XML. This is why the introduction of Toolbar made so much sense for Android development. In the future, you might even have more requirements that differ between each Fragment. Some of them might not want a Toolbar, some of them might need another View above the Toolbar, some of them will have a RecyclerView that you would like to be accessible to the CoordinatorLayout and AppBar so that the scrolling behavior works properly.
I recommend you to put only a FrameLayout as the content of your DrawerLayout (as Yiyo mentioned in point 1). Here you will load each Fragment from the callbacks of the NavigationView.
<android.support.v4.widget.DrawerLayout
...
android:fitsSystemWindows="true"
>
<FrameLayout
android:id="#+id/drawer_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
...
/>
</android.support.v4.widget.DrawerLayout>
In each Fragment's XML you will put, if required by that Fragment, a Toolbar. In your tabbed Fragment's XML you will put the TabLayout, and if you so wish, the CoordinatorLayout and AppBarLayout. From each Fragment that has a Toolbar, you will set the Toolbar as the ActionBar:
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
That's all there is to it. Of course you don't want to repeat yourself in every Fragment, so you can, for example, put this code in a DrawerFragment and subclass it for fragments with a Toolbar. You will also want to put your Toolbar XML configuration in a single file and include it in the Fragment's XML <include layout="#layout/toolbar" />. Or you might want to remove the Toolbar from some fragments, or change its color, theme, etc.
You can do it like this. I have checked by doing it myself and it works very well
Step 1 : Create a layout of your main activity like this
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout" android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar" android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#color/blue"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:titleTextAppearance="#style/TextAppearance.AppCompat.Small"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout" style="#style/FreeWiFiTabLayout"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_gravity="center" android:background="#color/blue"
android:visibility="gone" android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/wifi_type_pager" android:layout_width="match_parent"
android:layout_height="match_parent" android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<fragment android:id="#+id/navigation_drawer"
android:name="com.easyway.freewifi.NavigationDrawerFragment"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start|bottom" android:layout_marginTop="?attr/actionBarSize"
tools:layout="#layout/navigation_drawer_fragment" />
Step 2 :
In your activity you need to set onpagechangelistener on your viewpager:
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position ==0){
tabLayout.setVisibility(View.GONE);
}else{
tabLayout.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
After this you need to add
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
Step 3 :
This is how you can make your adapter for viewpager :
public class WiFiPagerAdapter extends FragmentPagerAdapter {
private final List registeredFragments = new ArrayList<>();
public WiFiPagerAdapter(final FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(final int pos) {
Fragment fragment;
fragment = WiFiFragment.newInstance(pos);
registeredFragments.add(pos, fragment);
return fragment;
}
#Override
public int getCount() {
return tabLayout.getTabCount();
}
public List<Fragment> getRegisteredFragmentsList() {
return registeredFragments;
}
#Nullable
public Fragment getRegisteredFragment(final int position) {
final Fragment wr = registeredFragments.get(position);
if (wr != null) {
return wr;
} else {
return null;
}
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
for (int i = 0; i < registeredFragments.size(); i++) {
WiFiFragment wiFiFragment = ((WiFiFragment) registeredFragments.get(i));
wiFiFragment.setWiFiFragmentRecyclerViewAdapter();
}
}
}

Android ActionBar -- How to add logo in centrelly aligned

I am new in android development and I want make a navigation drawer for my client.
How can I add a logo to my action bar in navigation drawer and remove the title.
my code :
Activity
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#D5D2D4")));
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
actionBar.setDisplayUseLogoEnabled(true);
}
Manifest Activity
<activity
android:name=".activity.Grid_Home_Activity"
android:label="#string/title_activity_grid__home_"
android:theme="#style/Theme.ActionBarJp">
</activity>
Styles
<style name="Theme.ActionBarJp" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarSize">60dp</item>
<item name="logo">#drawable/logo</item>
<item name="displayOptions">useLogo|showHome</item>
</style>
Can anyone help ?
you can use https://github.com/jgilfelt/SystemBarTint this Library
and use as below.
public ActionBar enableCustomCenterTitle(Context context, boolean isShowBack, boolean isWhite) {
ActionBar actionBar = enableCustomTitle();
View actionbarLayout = LayoutInflater.from(context).inflate(
R.layout.actionbar_layout, (ViewGroup) actionBar.getCustomView(),false);
if (isWhite) {
actionbarLayout = LayoutInflater.from(context).inflate(
R.layout.actionbar_layout_white, (ViewGroup) actionBar.getCustomView(),false);
TextView back = (TextView) actionbarLayout.findViewById(R.id.action_bar_back);
back.setTextColor(getResources().getColor(R.color.orange));
mTintManager.setStatusBarTintColor(getResources().getColor(R.color.white));
setStatusBarDarkMode(true, this);
}
ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
View backView = ButterKnife.findById(actionbarLayout, R.id.action_bar_back);
if (isShowBack) {
backView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
backView.setVisibility(View.VISIBLE);
} else {
backView.setVisibility(View.GONE);
}
actionBar.setCustomView(actionbarLayout, params);
return actionBar;
}
ActionBar is deprecated. Use ToolBar .
Then at your Toolbar object center the ImageView with the logo.
Take a look at this
You can use setLogo to set a resDrawable or Drawable object. As to the logo size on the ActionBar, I believe it's 32x32 dp; Optical square, 24x24 dp according to this
To remove the title, just use actionBar.setTitle(""). This code makes it looks like there is no title.
One more thing, You should really change ActionBar to ToolBar.
As actionbar is deprecated, it is better to user ToolBar and can utilize material design concepts in your app.
Do according below:
1. create toolbar.xml in your res folder.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/grey"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv_Logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
Open the layout file of your main activity (activity_main.xml) and add the toolbar.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
In your MainActivity.java, add reference of Toolbar widget in onCreate() method :
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);

Spinner in the toolbar appears when I search - Android Lollipop

I have a drawer navigation and I switch between fragments, in the Fragment ONE I have a spinner in the toolbar. In the Fragment TWO, I don't want the spinner, so I remove the spinner with the property "visibility=GONE" and that works.
BUT, in the Fragment TWO, doing the following steps, something strange really happens
Tap in the search icon
The search view appears in the toolbar
I tap cancel
The search view collapse, and the spinner from the Fragment ONE shows up
I have tried to use this listener "OnActionExpandListener" in the search icon to hide the spinner again when user taps cancel, but it doesn't work.
any ideas?
Drawer navigation where I set up the toolbar
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/toolbar_dropdown"/>
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:fitsSystemWindows="true"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="56dp"
android:choiceMode="singleChoice"
android:divider="#color/gray_light_divider_list_drawerNav"
android:dividerHeight="1dp"
android:background="#color/gray_light_background_list_drawerNav"
/>
Drawer navigation Activity method OnCreate, where I setup the the toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
if(getSupportActionBar()!=null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
This is how I switch between fragments
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment);
transaction.addToBackStack(null);
transaction.commit();
Using the ActionBarDrawerToggle in the method onDrawerOpened, I remove the spinner, so that means that every time the Drawer Navigation is open I removed the spinner like this
Spinner spinner = (Spinner)findViewById(R.id.spinner_nav);
spinner.setVisibility(View.GONE);
This is the OnActionExpandListener in the Fragment TWO
MenuItemCompat.OnActionExpandListener searchOnActionExpandListener = new MenuItemCompat.OnActionExpandListener()
{
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
Spinner spinner = (Spinner)findViewById(R.id.spinner_nav);
spinner.setVisibility(View.GONE);
return true;
}
};
To resolve this issue, I did the following.
I removed the spinner in the toolbar so it looks like this
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
And I started to add the spinner in the fragment ONE programatically
Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
if(toolbar.findViewWithTag("spinner_nav")==null) {
Spinner spinner = new Spinner(getActivity());
spinner.setTag("spinner_nav");
//Setting up the adapter
AdapterFragmentOne spinnerAdapter = new AdapterFragmentOne(getActivity(), array);
spinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
if (spinner != null) {
spinner.setVisibility(View.VISIBLE);
spinner.setAdapter(spinnerAdapter);
}
spinnerAdapter.notifyDataSetChanged();
spinner.setOnItemSelectedListener(mOnNavigationListener);
toolbar.addView(spinner);
}
In my Drawer navigation Activity I remove the spinner when the drawer is opened, I execute
//Removing the spinner view
Spinner spinner = (Spinner) toolbar.findViewWithTag("spinner_nav");
toolbar.removeView(spinner);

ActionBar tabs with ViewPager and Frame Layout

I was wanting to use ActionBar tabs and ViewPager to implement swiping between tabs. I have successfully used the example here:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
However, I also wanted to add a Frame Layout outside of the ViewPager in the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<FrameLayout
android:id="#+id/frame_layout"
android:name="com.banagas.calculator.ButtonFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
If I set the setContentView() to my layout with both the ViewPager and Frame Layout, the ViewPager stops functioning:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity", "onCreate");
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.FrameLayout1);
ActionBar bar = getSupportActionBar();
mViewPager.setOffscreenPageLimit(4);
bar.setDisplayHomeAsUpEnabled(false);
bar.setDisplayShowTitleEnabled(false);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Display"), DisplayFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Y="), YFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Graph"), GraphFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Table"), TableFragment.class, null);
if (findViewById(R.id.frame_layout) != null)
{
if (savedInstanceState != null)
{
return;
}
getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, new ButtonFragment()).commit();
Log.i("MainActivity", "add.ButtonFragment");
}
}
I can see the tabs and nothing else within the ViewPager. My Frame Layout works perfectly fine though. How would I get ViewPager to work with this?
Why you do
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.FrameLayout1); ?
The ViewPager is already on your layout.
Shoud do something like
mViewPager = (ViewPager)findViewById(R.id.FrameLayout1);
From the sample code they are creating the controls on runtime, while you have them in the xml.

Categories

Resources