I have developed an Android application which has 4 TabHost's which are in fragments.
I Know how to customize the ActionBar in MainActivity.
But problem is how can I customize my ActionBar according to the 4 different TabHost's in different Fragments?
Here is my tabHost code -
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/** Creating ANDROID Tab */
Tab tab = actionBar.newTab()
//.setText("Android")
.setTabListener(new CustomTabListener<PlayFragment01>(this, "play",
PlayFragment01.class))
.setIcon(R.drawable.playtabhosticon);
actionBar.addTab(tab);
/** Creating APPLE Tab */
Tab tab1 = actionBar.newTab()
//.setText("Apple")
.setTabListener(new CustomTabListener<VenueFragment01>(this, "venu",
VenueFragment01.class))
.setIcon(R.drawable.venutabhosticon);
actionBar.addTab(tab1);
/** Creating APPLE Tab */
Tab tab3 = actionBar.newTab()
//.setText("Apple")
.setTabListener(new CustomTabListener<SocialFragment01>(this, "social", SocialFragment01.class))
.setIcon(R.drawable.socialtabhosticon);
actionBar.addTab(tab3);
/** Creating APPLE Tab */
Tab tab4 = actionBar.newTab()
//.setText("Apple")
.setTabListener(new CustomTabListener<ActivityFragment01>(this, "activity",
ActivityFragment01.class))
.setIcon(R.drawable.actionbartabhosticon);
actionBar.addTab(tab4);
}
}
Here is my first fragment code -
package in.wptrafficanalyzer.actionbarnavtab;
/** This is a listfragment class */
public class PlayFragment01 extends Fragment {
/** An array of items to display in ArrayList */
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/** Creating array adapter to set data in listview */
View rootView = inflater.inflate(R.layout.fragment_play, container, false);
//new DownloadJSON().execute();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getActivity().getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#0077d1")));
ActionBar mActionBar = getActivity().getActionBar();
getActivity().getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar2, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
return rootView;
}
}
The question your asking is not very clear but you can get the ActionBar instance from within a Fragment using getActivity().getActionBar().
ActionBar.setNavigationMode() and TabHost is now deprecated.
The new ToolBar introduced in API-21 is more powerfull.
But since you asked for a solution, here is a simple one from Google SlidingTabLayout:
This is like a PagerTabStrip that you can easily customize and works like great.
It works with ViewPager and Fragments of course.
In my point of view ,you should read this ,so that you can get the basic idea for creating a fragment.
http://developer.android.com/guide/components/fragments.html
Try this sample code..this sample code includes 3 tab ,first try to understand this code ,then you can make your own app with 4 tabs
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
Also try to modify your question ,It is unclear and also it will be good that you post your errors
Related
I am currently working on an application with ActionBar tabs inside one of its fragments (main navigation is NavigationDrawer). The fragment's onCreate():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = activity.getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Resources r = getResources();
//(…) tabNames initialization
adapter = new CustomTabAdapter(getFragmentManager());
}
and onCreateView():
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(this);
for (String tabName : tabNames) {
actionBar.addTab(actionBar.newTab()
.setText(tabName)
.setTabListener(this));
}
return rootView;
}
There are 4 tabs, each of them with its own Fragment and layout. CustomTabAdapter returns new instance of proper fragment in its getItem(int). There are 4 tabs.
And now my problem is:
I start the application.
I choose this tab fragment from NavigationDrawer list. Everything is working just fine.
I choose another fragment from NavigationDrawer list which means that tab fragment is replaced with it.
I choose tab fragment again. And fragment related to tab that was selected before choosing another fragment from NavigationDrawer list, and one's adjacent to it are not recreated (blank screen under ActionBar tabs). I checked and onCreateView(…) methods of those fragment are not called. So after changing device orientation, or choosing tab not adjacent and this one again proper layout is shown.
How can I make it work as it should (showing proper layout on reentering tab fragment from NavigationDrawer list instead of blank space)? I run out of ideas.
Finally, I found a solution. My CustomTabAdapter was extension of FragmentPagerAdapter. I changed it to be extension of FragmentStatePageAdapter, and now fragments are recreated.
More details in this answer by #Louth.
It takes fragments from cache without recreation
I have create the sample activity using frame layout and fragment with tabs. However, when I switch to other activity/fragment and than comes back to same activity/fragment, it always create duplicate entry or view for tabs. for example, I have tab1 and tab2, when I view activity for first time it display two tabs, but when i switch to other activity and comes back to tabs activity it display four tabs, 'tab1, tab2, tab1, tab2'.
This is my code
public View onCreateView(LayoutInflater Inflater, ViewGroup Container,Bundle savedInstanceState) {
if(savedInstanceState==null) {
rootView = Inflater.inflate(R.layout.loanapplicationview, Container, false);
actionBar = getActivity().getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(true);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(true);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
return rootView;
}
}
You should check if the tabs exist before adding them.
if (actionBar.getTabCount() == 0) {
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
ActionBar().removeAllTabs() will remove all the tabs attached to your ActionBar.
So before addinf new tabs clear the previous ones with this method
actionBar.removeAllTabs();
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
I have a class that is SherlockFragmentActivity and inside it two SherlockFragment.
When I enter the class it automatically puts me inside the first tab (Tab1 "FragmentTab1"), and I can go to the other tab (Tab2 "FrgmentTab2") by clicking on the tab up. How can I disable a tab?
I want to disable Tab2(bbb) from Tab1(aaa).
my main class code:
public class MainClass extends SherlockFragmentActivity {
ActionBar.Tab Tab1,Tab2;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mm);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("aaa");
Tab2 = actionBar.newTab().setText("bbb");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
My tabs codes (both classes have same clean-empty code right now)
public class FragmentTab1 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab2, container, false);
return rootView;
}
}
For further explanation please ask.
For removing tab from tabbar, you can use
getActivity().getSupportActionBar().removeTabAt(index)
If you want to disable touch events on that particular tab, you need to create custom view for this Tab a disable focus on that view.
EDIT
Sory, forgot to mention. If you are using API version>=11, you can use
getActivity().getActionBar()
If you are using support lib for compatability, e.g. ActionBarSherlock or appCompat you need to cast it first, in your case
((SherlockFragmentActivity) getActivity()).getSupportActionBar()
I am studying on the ActionBar and saw this abstract method
public abstract Tab newTab();
However the implementation of newTab is only seen in ActionBarImpl.java.
From android http://developer.android.com/reference/android/support/v4/view/ViewPager.html
It is showed that
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Simple"), CountingFragment.class, null);
So, where is the bar.newTab() actually instantiated?
I saw a related post What class should I extend, AcionBar or ActionBarImpl? but it doesn't seem to answer my question directly.
The ActionBar class is an abstract class, like you already found out. It's actually implemented in the ActionBarImp class. Everything related to the ActionBar will be instantiated inside the Activity class. getActionBar() will return this implementation of the ActionBar:
/**
* Retrieve a reference to this activity's ActionBar.
*
* #return The Activity's ActionBar, or null if it does not have one.
*/
public ActionBar getActionBar() {
initActionBar();
return mActionBar;
}
/**
* Creates a new ActionBar, locates the inflated ActionBarView,
* initializes the ActionBar with the view, and sets mActionBar.
*/
private void initActionBar() {
[...]
mActionBar = new ActionBarImpl(this);
[...]
}
Source.
I have ActionBar with 3 tabs. Each tab has a Fragmemt. How can I assign each tab an activity (each fragment has a button, how to assign Event on that button)?
public class TMABActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab tab = actionBar
.newTab()
.setText("MyBooks")
.setTabListener(
new MyTabListener<NewsFragment>(this, "1",
NewsFragment.class, this));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("News")
.setTabListener(
new MyTabListener<MyBooksFragment>(this, "2",
MyBooksFragment.class, this));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("Account")
.setTabListener(
new MyTabListener<AccountFragment>(this, "3",
AccountFragment.class, this));
actionBar.addTab(tab);
}
You don't need to attach any Activity to your fragments. An instance of Activity is automatically created for each fragment.
To add a listener, for example, on a button of each Fragment, you can override the method
void onActivityCreated(Bundle savedInstanceState)
and get a reference to the Button with
Button button = (Button) getActivity().findViewById(R.id.example_button);
Fragments do not need explicit activity assignment.