Folks,
I am new to android development so my question is probably very basic - I am using the tabbed pane from actionbarsherlock and want to have 3 different UI for 3 tabs. However I also need one textbox in common across all tabs. To achieve the first thing my idea was to have 3 different activity classes extending the ActionBar.TabListener but instantiating those classes (using new) from the setTabListener is not working. Any solution?
Regards,
The Main Activity Class -
ActionBar.Tab aTab = getSupportActionBar().newTab();
aTab.setText("A");
aTab.setTabListener(new AActivity());
getSupportActionBar().addTab(aTab);
ActionBar.Tab bTab = getSupportActionBar().newTab();
bTab.setText("B");
bTab.setTabListener(new MessageActivity());
getSupportActionBar().addTab(bTab);
ActionBar.Tab cTab = getSupportActionBar().newTab();
cTab .setText("C");
cTab .setTabListener(new DataActivity());
getSupportActionBar().addTab(cTab );
Now the AActivity Class -
public class AActivity extends Activity implements ActionBar.TabListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.call, menu);
return true;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
System.out.println("In A");
TextView txtView = (TextView) findViewById(R.id.aLog);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
It is giving me NullPointer at
TextView txtView = (TextView) findViewById(R.id.aLog)
so am assuming the new thing is not working at all
You're doing it wrong.
The TabListener
interface allows you to respond when a tab is selected or unselected. How you respond to it is completely up to you. It doesn't matter if the implementing class is an Activity
or any other particular class - as long as it implements the methods of TabListener
in some way or another.
You should never have to instantiate an Activity
class yourself through new MyActivity()
. Android needs to do that itself so it can set up the environment for the activity. Thus, if you want to launch another activity when a tab is selected, then you should be calling startActivity
inside onTabSelected
. However, launching an activity when selecting a tab is probably a bad idea. That will open up a completely separate activity (outside of your original tabbed activity) and it won't have the tabbed action bar.
You're looking for fragments: a single activity with a container holding a fragment which you can swap with other fragments. When selecting a tab, you want to replace the current fragment with another fragment specific to that tab. This is demonstrated in the ActionBarSherlock samples, FragmentTabs.TabManager
handles the binding between tabs and fragments. To use this in your code, you should:
MainActivity
extend SherlockFragmentActivity
.fragment_tabs.xml
.TabManager
class from FragmentTabs.java
and copy it to your codebase (as a separate class or as a static inner class, whatever works).Copy the setup code from FragmentTabs.onCreate
:
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);
Create your tabs with TabManager.addTab
, for example:
mTabManager.addTab(mTabHost.newTabSpec("tabA").setIndicator("A"),
AFragment.class, null);
Make Fragment
classes for each tab (e.g. AFragment
). Override Fragment.onCreateView
, inflate your UI and do some fancy stuff.
Also, read the fragments developer guide!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments