How to display activity in the ActionBar's tab? - android

I'm using SupportLibrary. Here is my MainActivity code:
public class MainActivity extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.support.v7.app.ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
android.support.v7.app.ActionBar.Tab tab = bar.newTab();
tab.setText("Tab 1");
tab.setTabListener(this);
bar.addTab(tab);
}
How can I display some activity in the tab?
Thanks.

How can I display some activity in the tab?
You can't. First, that technique was deprecated three years ago. Second, ActionBarActivity does not extend the deprecated ActivityGroup.
You are welcome to use fragments for your tabs, though.

Related

How to show tabs and action bar in the same activity of android application?

i am developing android application. i extend my class to Tab Activity.
while i am using getActionBar() or getSupportActionBar(), the activity stops working. when i extend my activity to Activity or ActionBarActivity getTabHost() will not work. Any one pls help me. thanks in advance.
Here is my coding.
public class CommonTabActivity extends TabActivity
implements OnTabChangeListener {
TabHost tabHost;
Bundle response = new Bundle();
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_common_tab);
....
tabHost = getTabHost();
....
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
you can extend your activity to AppCompatActivity (ActionbarActivity is deprecated now), use tablayout from android design library to show tabs and associate viewpager to tablayout to switch between fragments corresponding to each tab.
your activity layout would be something like this.
android.support.v7.widget.Toolbar
android.support.design.widget.TabLayout
android.support.v4.view.ViewPager
There are plenty of examples available to implement this.
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/

Android TabActivity with Toolbar - setSupportActionBar() unknow

Is it possible to use setSupportActionBar() in an TabActivity?
Extending with AppCompatActivity is not possible...
public class TabHost extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // this is unkown
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //also
Do I have to switch from TabActivity to FragmentTabHost?
Thanks
no you can't. You have to extend AppCompatActivity, and you shouldn't use TabActivity in the first place. It was deprecated long time ago. You should use a solution based on a ViewPager and Fragments to achieve the same behavior

Remove just the TitleBar from an ActionBar

The red rectangle (The bar with the title of the activity) it is what I want to remove (see the picture):
http://subefotos.com/ver/?8e6468bdbc19b0864d5090fa79e54120o.jpg
What I have in my code:
public class HomeActivity extends ActionBarActivity implements ActionBar.TabListener, OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_home);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
} }
I can remove all the bar, including the HOME-STATISTIC-CLASSIFICATION, but I don't want to remove this.
I think that what I want to do it is impossible if we take into account this link: http://developer.android.com/guide/topics/ui/actionbar.html
Although if somebody knows how can be done, please post it.
Thank you for your time.

sherlock action bar not working

I am making a action bar using sherlock action bar but it is not shown when i run it.This is the code:
public class NaseebactionbarActivity extends SherlockActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");
actionbar.addTab(PlayerTab);
actionbar.addTab(StationsTab);
actionbar.show();
}
please tell me what is the problem and how to solve it.
Extend your Activity from SherlockFragmentActivity instead of SherlockActivity:
public class MyActivity extends SherlockFragmentActivity {
// ...
}
In your AndroidManifest, be sure to set the theme of your application to :
android:theme="#style/Theme.Sherlock"

Add FragmentActivity in TabHost

actually I have a FragmentActivity with a Tabhost, some Fragment inside and you can slide between the different Fragment thanks to a ViewPager.
With this FragmentActivity, I would like to incorporate it into a other Tabhost, and so have the one below the other.
For the moment, I have this solution :
public class TabsViewPagerFragmentActivity extends ActivityGroup {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the layout
setContentView(R.layout.main);
TabHost mTabHost = (TabHost)findViewById(R.id.testtabhost1);
mTabHost.setup(getLocalActivityManager());
TabSpec ts = mTabHost.newTabSpec("tab_test1").setIndicator("TAB1");
ts.setContent(new Intent(this,PageGaucheFragment.class)); <--- PageGaucheFragment is the FragmentAtivity with the ViewPager that I would like to add into the TabHost
mTabHost.addTab(ts);
mTabHost.setCurrentTab(0);
}
It works, but ActivityGroup was deprecated :( And I don't find any other solution to solve this problem.How can I have 2 TabHost and that the second can slide between the different Fragment ?
Thanks for your answer, and sorry for my English ;)
Look here: http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html
I've used it successfully.

Categories

Resources