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/
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.
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.
I was recommended to extend my Activity class from ActionBarActivity
Here is the previous code:
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I wrote new application and followed advice.
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If I use ACtionBarActivity instead of Activity, I get the following error on the phone, when I try to run it:
The method getSupportActionBar() is undefined for the type TaskActivity
Your class needs to extend from ActionBarActivity, rather than a plain Activity in order to use the getSupport*() methods.
Update [2015/04/23]: With the release of Android Support Library 22.1, you should now extend AppCompatActivity. Also, you no longer have to extend ActionBarActivity or AppCompatActivity, as you can now incorporate an AppCompatDelegate instance in any activity.
Here is another solution you could have used. It is working in my app.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.support.v7.app.ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main)
Then you can get rid of that import for the one line ActionBar use.
If you are already extending from ActionBarActivity and you are trying to get the action bar from a fragment:
ActionBar mActionBar = (ActionBarActivity)getActivity()).getSupportActionBar();
Here is the answer of my question.
I've asked that again with some remarks.
How to add support libraries?
If you are extending from an AppCompatActivity and are trying to get the ActionBar from the Fragment, you can do this:
ActionBar mActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
You have to change the extends Activity to extends
AppCompactActivity then try set and getSupportActionBar()
Can you set the ActionBar before you set the Contient View? This order would be better:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
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.