I am using Actionbarsherlock for tab layout. In some conditions I want to set the tab at index 4 as the default tab. I mean the tab should stay at 5th position but it should be the default one. Is there any way to do that?
My class definition looks like this:
public class CalendarActivity extends SherlockFragmentActivity implements ActionBar.TabListener
and the onCreate method looks like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabMonthly = bar.newTab();
tabMonthly.setText("Monthly").setTabListener(this);
bar.addTab(tabMonthly);
ActionBar.Tab tabWeekly = bar.newTab();
tabWeekly.setText("Weekly").setTabListener(this);
bar.addTab(tabWeekly);
ActionBar.Tab tabDaily = bar.newTab();
tabDaily.setText("Day").setTabListener(this);
bar.addTab(tabDaily);
ActionBar.Tab tabList = bar.newTab();
tabList.setText("List").setTabListener(this);
bar.addTab(tabList);
ActionBar.Tab addEvent = bar.newTab();
addEvent.setText("Unread").setTabListener(this);
bar.addTab(addEvent);
//String callerActivity = getIntent().getStringExtra("activityCaller");
//if( callerActivity!= null && callerActivity.equalsIgnoreCase("notification") ){
//bar.setSelectedNavigationItem(4);
//}
}
I tried the code commented at the end of onCreate() method above. But the problem with this code is that it first loads the tab at index zero and then suddenly goes to tab at index4.
Related
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 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
I have an activity with two fragments added to a tabAction. The first one is the gridview of applications, the second one is a gridview for documents. When I rotate the tablet my fragments are not up to date. I would like to instantiate my fragment after a rotation so the Activity restarts; but the problem that I don't use viewpager. And with the part of code given below:
// ActionBar
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayUseLogoEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
// create new tabs and set up the titles of the tabs
ActionBar.Tab mFindTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_find));
ActionBar.Tab mChatTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_chat));
ActionBar.Tab mMeetTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_meet));
ActionBar.Tab mPartyTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_find));
// create the fragments
Fragment mMeetFragment = new ApplicatinFragment();
Fragment mFindFragment = new MatFragment();
Fragment mChatFragment = new DocumentFragment1();
Fragment mPartyFragment = new PartyFragment();
// bind the fragments to the tabs - set up tabListeners for each tab
mFindTab.setTabListener(new MyTabsListener(mFindFragment,
this.getBaseContext()));
mChatTab.setTabListener(new MyTabsListener(mChatFragment,
getApplicationContext()));
mMeetTab.setTabListener(new MyTabsListener(mMeetFragment,
getApplicationContext()));
actionbar.addTab(mMeetTab);
actionbar.addTab(mFindTab);
my onSaveInstanceState is given by the code below:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
/*
Toast.makeText(
this,
"onSaveInstanceState: tab is"
+ getActionBar().getSelectedNavigationIndex(),
Toast.LENGTH_SHORT).show(); */
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
outState.putStringArrayList("List", LaunchActivity.myList);
outState.putString("ListActivity", LaunchActivity.myList.toString());
}
then in my Oncreat I call :
if (savedInstanceState != null) {
savedUser = savedInstanceState.getString("TEXT");
savedMode = savedInstanceState.getString("MODE");
String str;
str = savedInstanceState.getString("ListActivity");
test = savedInstanceState.getStringArrayList("List");
//actionbar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 1));
// myList = (ArrayList<String>) Arrays.asList(str.split("\\s*,\\s*"));
actionbar.setSelectedNavigationItem(savedInstanceState.getInt(
TAB_KEY_INDEX, 0));
} else {
savedUser = "eleve";
savedMode = "normal";
}
I would like to use the solution given in what's the right way to store fragment's state
ViewPager and fragments — what's the right way to store fragment's state ]1
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.
I have noticed that when using
actionBar.setSelectedNavigationItem(x)
in the onCreate() method of my Activity, the tab item at position 0 is always selected first and then the tab item at position x is loaded. This means that (since I'm using Fragments) 2 Fragments are loaded. One of them being unnecessary...
Here's my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Determine which bundle to use; either the saved instance or a bundle
// that has been passed in through an intent.
Bundle bundle = getIntent().getExtras();
if (bundle == null) {
bundle = savedInstanceState;
}
// Initialize members with bundle or default values.
int position;
if (bundle != null) {
position = bundle.getInt("selected_tab");
} else {
position = 0;
}
// Set the tabs.
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar
.newTab()
.setText("Tab 1")
.setTabListener(
new TabListener<RendersGridFragment>(this, "1",
RendersGridFragment.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("Tab 2")
.setTabListener(
new TabListener<RendersGridFragment>(this, "2",
RendersGridFragment.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("Tab 3")
.setTabListener(
new TabListener<RendersGridFragment>(this, "3",
RendersGridFragment.class));
actionBar.addTab(tab);
actionBar.setSelectedNavigationItem(position);
}
It seems that the tab at position 0 is selected initially by default. But, as you can see, I'm passing bundles to make sure the last selected tab is still selected when the activity onCreate() method is run again.
For example,
if the last selected tab is at position 2, the onCreate() runs and the tab at position is 0 is loaded, then the tab at position 2 is loaded.
How can I make sure the ActionBar doesn't select tab at position 0 first when using actionBar.setSelectedNavigationItem(position).
Use the other addTab calls to override this behaviour. You'll need to add the tab you want to be selected first (in your case, the tab at position 2). Relevant Javadoc
actionBar.addTab(tab2);
actionBar.addTab(tab0, 0, false);
actionBar.addTab(tab1, 1, false);
For any others looking to do this you can also set the tab to selected by setting the position and then set true or false to indicate which tab should be selected
actionBar.addTab(tab1, 0, false);
actionBar.addTab(tab2, 1, true);
actionBar.addTab(tab3, 2, false);
Here are the docs on this approach: http://developer.android.com/reference/android/app/ActionBar.html#addTab(android.app.ActionBar.Tab, int, boolean)
you can use below statment in activtiy onStart method:
protected void onStart() {
super.onStart();
actionBar.selectTab(mainTab);
}
which mainTab variable here is of type Tab.
this way you need to define tabs as class-wide variables like this:
Tab mainTab, tab2,tab3;
#Override
protected void onCreate(Bundle savedInstanceState) {
//add tabs to action bar
....
}
If you have 3 tabs (i.e. tab 0, tab 1, tab 2) and want tab 1 to be preselected. Do this:
for (int i = 0; i < mFragmentPagerAdapter.getCount(); i++) {
boolean preselected = (i == 1);
actionBar.addTab(actionBar.newTab().setText(
mFragmentPagerAdapter.getPageTitle(i)).setTabListener(this), preselected);
}
You will be using:
public abstract void addTab (ActionBar.Tab tab, boolean setSelected)
as per this API specification.
bkurzius' answer helped me to fix a problem I was having with the same issue.
What I did was:
private final String TAB_SELECTED = "tab_selected"
...
private int mTabSelected;
...
mTabSelected = savedInstanceState.getInt(TAB_SELECTED);
...
final ActionBar actionbar = getActionBar();
...
actionbar.addTab(tab1, mTabSelected == 0);
actionbar.addTab(tab2, mTabSelected == 1);
actionbar.addTab(tab3, mTabSelected == 2);
...
outState.putInt(TAB_SELECTED, getActionBar().getSelectedNavigationIndex());
This way, the setSelected parameter is true only if mTabSelected is equal to the tab's index.
Percy Vega's reply seems to be the best working solution.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
boolean preselected = (i == ErrorDetails.tab_id);
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this),preselected);
}