OnTabSelected not being called at all when using actionbarsherlock - android

I've done stuff like this before and this is a super simple example but I can't seem to get it working using actionbarsherlock v4.1.
This is the main activity
public class VanityActivity extends SherlockFragmentActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
//restore
}
com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
com.actionbarsherlock.app.ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Website");
tab1.setTabListener(new HeaderTabListener(getApplicationContext()));
tab1.setTag(1);
actionBar.addTab(tab1);
com.actionbarsherlock.app.ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Portfolio");
tab2.setTabListener(new HeaderTabListener(getApplicationContext()));
tab2.setTag(2);
actionBar.addTab(tab2);
com.actionbarsherlock.app.ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Team");
tab3.setTabListener(new HeaderTabListener(getApplicationContext()));
tab3.setTag(3);
actionBar.addTab(tab3);
}
}
And my HeaderTabListener class:
public class HeaderTabListener implements
com.actionbarsherlock.app.ActionBar.TabListener {
private Context context;
public HeaderTabListener(Context context){
this.context = context;
}
#Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
int i=((Integer)tab.getTag()).intValue();
if(tab.getPosition()==0){
ft.replace(android.R.id.content, CompanyFragment.newInstance(i));
}else if(tab.getPosition()==1){
ft.replace(android.R.id.content, PortfolioFragment.newInstance(i));
}
}
#Override
public void onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
Log.v("Tab selected", tab.getText().toString());
}
#Override
public void onTabReselected(com.actionbarsherlock.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
Log.v("Tab selected2", tab.getText().toString());
}
}
And finally one of the fragments (both fragments are the same but the text in each layout is different)
public class PortfolioFragment extends SherlockFragment {
private static final String KEY_POSITION="position";
private static final String KEY_TEXT="text";
static PortfolioFragment newInstance(int position) {
PortfolioFragment frag=new PortfolioFragment();
Bundle args=new Bundle();
args.putInt(KEY_POSITION, position);
frag.setArguments(args);
return(frag);
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.fragment_portfolio, container, false);
int position=getArguments().getInt(KEY_POSITION, -1);
return(result);
}
}
So when it first loads the the tabs are all displayed correctly, the first one his highlighted and the text in CompanyFragment layout displays. But then I am unable to click any of the other tabs to change the content and my logs inside of onTabSelected just never gets called. Any thoughts? Thanks for reading.

First off, let me tell you that I have no experience with ActionBarSherlock. I looked at your code to get a few ideas for my application. Seeing as it hasn't been answered since you posted it a few month ago, I would like to share my first impression.
As far as I know, ActionBarSherlock exposes the same functionality as the most recent Android APIs. Therefore, I do think that you are missing a simple method call. You would need to call commit() (Android Developer Guide reference) after you replace the fragment. Therefore, change the line, and all similar lines from;
ft.replace(android.R.id.content, PortfolioFragment.newInstance(i));
to
ft.replace(android.R.id.content, PortfolioFragment.newInstance(i)).commit();.
I hope this will solve your problem although I do realise you state that the ònTabSelected()` method is never called. Without debugging the difference between the problems would, however, seem transparent.

Related

Support Library with ActionBar and Tabs

I have a project where i use Android Support Library to include the ActionBar for pre 3.0-devices.
I also have Tabs on my project. The Tabs are Fragments. This way, I extend every Tab from the Class android.support.v4.app.Fragment from Support Library.
However, I have a problem with pre 3.0-devices. There are no action bar being displayed. So, there are also no tabs.
I can't find the solution....
So my "main-activity" (which handles the ActionBar with the tabs) looks this like:
public class Hauptmenue_extended extends ActionBarActivity implements
OnClickListener {
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tabB = actionBar.newTab();
tabB.setText("Home");
tabB.setIcon(R.drawable.icon_home);
tabB.setTabListener(new TabListener<Startmenue_activity>(this, "Start",
Startmenue_activity.class));
actionBar.addTab(tabB);
Tab tabA = actionBar.newTab();
tabA.setText("");
tabA.setIcon(R.drawable.icon_nachrichten_sel);
tabA.setTabListener(new TabListener<Nachrichten_activity>(this,
"Nachrichten", Nachrichten_activity.class));
actionBar.addTab(tabA);
Tab tabC = actionBar.newTab();
tabC.setText("");
tabC.setIcon(R.drawable.icon_favoriten);
tabC.setTabListener(new TabListener<Favoriten_activity>(this,
"Favoriten", Favoriten_activity.class));
actionBar.addTab(tabC);
And my TabListener looks like this:
public static class TabListener<T extends Fragment> implements
ActionBar.TabListener {
private final FragmentActivity myActivity;
private final String myTag;
private final Class<T> myClass;
public TabListener(FragmentActivity activity, String tag, Class<T> cls) {
myActivity = activity;
myTag = tag;
myClass = cls;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getSupportFragmentManager()
.findFragmentByTag(myTag);
// Check if the fragment is already initialized
if (myFragment == null) {
// If not, instantiate and add it to the activity
myFragment = Fragment
.instantiate(myActivity, myClass.getName());
ft.add(android.R.id.content, myFragment, myTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(myFragment);
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getSupportFragmentManager()
.findFragmentByTag(myTag);
if (myFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(myFragment);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
And every Fragment or Tab looks like this:
public class Nachrichten_activity extends Fragment implements
OnChildClickListener, OnClickListener { ...
Does anyone have an idea? :)
Some pointers, can't know for sure what will fix your problem
use #style/Theme.AppCompat.Light (what is .Base in your example?)
Don't name your fragments "_activity", it is only confusing.
Due to a bug in support lib, pre ICS use R.id.action_bar_activity_content instead of android.R.id.content . This will be fixed in later versions.
use ft.replace() in onTabSelected

Tabs content is visible on top of another after screen rotation

I have read alot on stackoverflow on fragments issues but I cant find a solution to my problem.
I have a Tabhost and when I change rotation of the device and then select another tab, the view from the first tab is also visible. So both tabs content is on top of each other.
I'm using a a custom tablistener and every tab is a fragment. I could bypass this with android:configChanges="keyboardHidden|orientation|screenSize"but this solution gave me a list of other problems and I read that this is a bad solution.
public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment fragment;
private final FragmentActivity activity;
private final String tag;
private final Class<T> myClass;
private long id;
public TabListener(FragmentActivity a, String t, Class<T> c, long id) {
tag = t;
myClass = c;
activity = a;
this.id = id;
}
/** The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (fragment == null) {
fragment = Fragment.instantiate(activity, myClass.getName());
// Sends stored TimerClass id to fragment
if(id != 0) {
Bundle b = new Bundle();
b.putLong("id", id);
fragment.setArguments(b);
}
ft.add(android.R.id.content, fragment, tag);
} else { // If it exists, simply attach it in order to show it
ft.attach(fragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null)
ft.detach(fragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
editNameDialog();
}
}
I dont know it this should be handled in the fragment, the activity or in the TabListener. The tabs content is viewd correctly until I change the screens orientation.
I found parts of the answer here on stackoverflow.
Solution
This is how I solved the problem. In the tab listeners I put:
fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
in both onTabSelect() and onTabUnselect()
Select correct tab after orientation change
When the screen rotates the activity is recreated(?) we need to store the last selected tab index.
Save last selected tab (this goes in the activity):
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("lastTab", actionBar.getSelectedNavigationIndex());
}
To load tab index, put this in onCreate() of the activity, this way we retreive the last tab index and selects it:
if (savedInstanceState != null) {
actionBar.selectTab(actionBar.getTabAt(savedInstanceState.getInt("lastTab")));
}
Initialize controllers in the fragment
To prevent the controls to diplay and behave odd I moved all controls inits in the fragment to onCreateView() like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.timer, container, false);
initializeControls(v);
setSeekBars(v);
return v;
}
I hope this will help someone else out there.

Implementing action bar tabs with fragments

So recently I needed to implement action bar tabs that would swap out the current fragment with a new fragment. Despite hours of intensive searching I was unable to find a clear solution, so I thought I would present how I solved the problem here. Two of the fragments contained list views, which turned out to be a major complicating factor.
First create an Activity to which you want to attach the fragments. In the XML file for that Activity add a linear layout like so:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".WoodenSideProject" >
<LinearLayout
android:id="#+id/fragment_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
Do not add anything else to the XML file. Not even the tab host that is preloaded with Eclipse.
Next create your fragments. First build the UI for your fragment how you want it using the fragment's XML file. I will show how to create a fragment with a List View:
public class Fragment1Name extends Fragment
{
public static String TAG="DirectionsFragment";
private String[] list_items = {"Put the list of Strings you want here"};
ListView lView1;
/*#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list_items);
setListAdapter(adapter);
}*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_XML_title, container, false);
createListView(view);
// Inflate the layout for this fragment
return view;
}
private void createListView(View view)
{
lView1 = (ListView) view.findViewById(R.id.ListViewID);
//Set option as Multiple Choice. So that user can able to select more the one option from list
lView1.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list_items));
}
}
Some people suggest extending ListFragment for a fragment that uses a List View. My experience has been that is more trouble than it's worth.
Set up the Activity's java file as follows:
public class ActivityName extends FragmentActivity {
public static Context appContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
setTitle("WoodenSideProject");
ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1");
ActionBar.Tab tab2 = actionBar.newTab().setText("Tab2");
ActionBar.Tab tab3 = actionBar.newTab().setText("Tab3");
Fragment Fragment1Name = new Fragment1Name();
Fragment Fragment2Name = new Fragment2Name();
Fragment Fragment3Name = new Fragment3Name();
tab1.setTabListener(new MyTabsListener(Fragment1Name));
tab2.setTabListener(new MyTabsListener(Fragment2Name));
tab3.setTabListener(new MyTabsListener(Fragment3Name));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
Within the same Activity create the following class:
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
//do what you want when tab is reselected, I do nothing
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_placeholder, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
I don't know if others have had as much trouble as I implementing action bar tabs with fragments, but if they are I hope this helps. Any suggestions for better implementations would be greatly appreciated.

Android Actionbar Sherlock with Tabs

I am trying to implement ActionBar Sherlock with Tabs below that as shown in the above wire-frame.
Should i use TabActivity ? - since i saw that it is deprecated. Which is the best way to achieve the same.
I implemented this functionality with a SherlockFragmentActivity as tabview container and with SherlockFragment as tabs. Here is a sketch (I omitted the usual Android activity stuff):
This is the tabview activity with two tabs:
public class TabViewActivity extends SherlockFragmentActivity {
// store the active tab here
public static String ACTIVE_TAB = "activeTab";
#Override
public void onCreate(Bundle savedInstanceState) {
..
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// add the first tab which is an instance of TabFragment1
Tab tab1 = actionBar.newTab()
.setText("TabTitle1")
.setTabListener(new TabListener<TabFragment1>(
this, "tab1", TabFragment1.class));
actionBar.addTab(tab1);
// add the second tab which is an instance of TabFragment2
Tab tab2 = actionBar.newTab()
.setText("TabTitle2")
.setTabListener(new TabListener<TabFragment2>(
this, "tab2", TabFragment2.class));
actionBar.addTab(tab2);
// check if there is a saved state to select active tab
if( savedInstanceState != null ){
getSupportActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(ACTIVE_TAB));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// save active tab
outState.putInt(ACTIVE_TAB,
getSupportActionBar().getSelectedNavigationIndex());
super.onSaveInstanceState(outState);
}
}
And this is the TabFragment that holds a tab's content:
public class TabFragment extends SherlockFragment {
// your member variables here
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_va_esh, container, false);
... // do your view initialization here
return view;
}
}
And finally this is the TabListener that handles tab switches:
public class TabListener<T extends Fragment> implements ActionBar.TabListener{
private TabFragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = (TabFragment) Fragment.instantiate(
mActivity, mClass.getName());
mFragment.setProviderId(mTag); // id for event provider
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
I believe TabActivity is deprecated in favor of using Fragments -- not because tab navigation is a deprecated concept. Simply use Fragments and a TabWidget.
Also, here's a similar question.
Edit:
Here's an example courtesy of Google: Android Tabs the Fragment Way

Actionbar with Fragment tabs and AdMob

I have an app that uses the ActionBar with tabs in combination with Fragments.
Now I would like to separate the screen into the normal screen at the top, and a small bar at the bottom for the ads:
Left is the normal screen, the tabs and their Fragments take up the whole screen.
What I want is the situation on the right. The tabs and Fragments take up the red part, the green part is for ads.
So the red part should make room for the ads, I don't want to overlay the ads.
As the Activity which sets up the ActionBar and tabs has no layout, I'm not able to add the AdView.
How can I do this?
Edit
This is how I implemented my app. The actionbar with tabs takes care of showing the fragments, so no xml layout file is used in the main Activity.
My code:
TestActivity.java
public class TestActivity extends SherlockFragmentActivity {
private ActionBar actionBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupTabs(savedInstanceState);
initAds();
}
private void setupTabs(Bundle savedInstanceState) {
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
addTab1();
addTab2();
}
private void addTab1() {
Tab tab1 = actionBar.newTab();
tab1.setTag("1");
String tabText = "1";
tab1.setText(tabText);
tab1.setTabListener(new TabListener<MyFragment>(TestActivity.this, "1", MyFragment.class));
actionBar.addTab(tab1);
}
private void addTab2() {
Tab tab1 = actionBar.newTab();
tab1.setTag("2");
String tabText = "2";
tab1.setText(tabText);
tab1.setTabListener(new TabListener<MyFragment>(TestActivity.this, "2", MyFragment.class));
actionBar.addTab(tab1);
}
private void initAds(){
//Here I want to display the ad, only loading once, Just like Davek804 said
}
}
TabListener.java
public class TabListener<T extends SherlockFragment> implements com.actionbarsherlock.app.ActionBar.TabListener {
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(Tab tab, FragmentTransaction ft) {
SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
// Check if the fragment is already initialized
if (preInitializedFragment == null) {
// If not, instantiate and add it to the activity
SherlockFragment mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(preInitializedFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (preInitializedFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(preInitializedFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
MyFragment.java
public class MyFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.myfragment, container, false);
}
}
Create an XML file containing the AdView definition and use <include> to include it at the bottom of each of your fragments.
Alternatively create a layout and your tabs to it. Cf. the reference dos:
To get started, your layout must include a ViewGroup in which you
place each Fragment associated with a tab. Be sure the ViewGroup has a
resource ID so you can reference it from your tab-swapping code.
Alternatively, if the tab content will fill the activity layout
(excluding the action bar), then your activity doesn't need a layout
at all (you don't even need to call setContentView()). Instead, you
can place each fragment in the default root ViewGroup, which you can
refer to with the android.R.id.content ID (you can see this ID used in
the sample code below, during fragment transactions).
http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html
http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html
The second link details how to do things like this picture:

Categories

Resources