The same code works in eclipse but doesn't work in Android Studio.
In Android Studio there is text through in many functions. Check Out in image in the link below http://postimg.org/image/d4sdh9t71/
package com.example.kiit_time_table_hello.kiittimetable;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("Monday").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Tuesday").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Wednesday").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Thursday").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Friday").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Saturday").setTabListener(this));
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
I applied the solution found in this topic: getActionBar() returns null.
And it works fine here.
You have to define window type as actionbar before activity render its view, so before calling setContentView() method use:
requestWindowFeature(Window.FEATURE_ACTION_BAR);
PS: The ActionBar.TabListener is Deprecated:
This interface was deprecated in API level 21.
Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead.
That is just showing you that the method or constant is deprecated.
Action bar tabs were deprecated in API 21:
http://developer.android.com/reference/android/app/ActionBar.html#NAVIGATION_MODE_TABS
Related
I was running the demo from Actionbersherlock samples and noticed the tabs were always inside the actionbar as in the picture. How do I fix this?
thnx.
public class TabNavigation extends SherlockActivity implements ActionBar.TabListener {
private TextView mSelected;
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_navigation);
mSelected = (TextView)findViewById(R.id.text);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
mSelected.setText("Selected: " + tab.getText());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
}
Code From here:
https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock-samples/demos/src/com/actionbarsherlock/sample/demos/TabNavigation.java
Per the Action Bar Tabs guide:
the system adapts the action bar tabs for different screen sizes—placing them in the main action bar when the screen is sufficiently wide, or in a separate bar (known as the "stacked action bar") when the screen is too narrow
As ActionBarSherlock mimics the platform behavior, tabs will appear in the Action Bar if there is enough space. You cannot force the stacked action bar pattern as per other answers.
actionbarsherlock has different modes use the correct one
I use this:
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
and the tabs are below the bar.
the different modes should be listed in the samples and documentation
I have a problem showing tabs with ActionBarSherlock. I have copied the example code of actionbarsherlock demos in my own aplication, if I run the application in a 7'' display like Nexus 7, tabs appear at the same bar as the title. But if I run the ABS sample in the same device tabs appears in a different bar.
What's wrong with my application?
Here is my code:
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.ResourcesCompat;
import com.mbal.misseries.R;
public class ProvaDeFragments extends SherlockActivity implements ActionBar.TabListener {
private TextView mSelected;
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock_Light); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_navigation);
mSelected = (TextView)findViewById(R.id.text);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
mSelected.setText("Selected: " + tab.getText());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
}
It's shown like this:
From my application
But if I run the sample ABS code appears like this:
ABS Sample Demo
Thanks in advance!
You aren't doing anything wrong, except testing it on a device with enough room. ;-)
Per the ActionBar documentation in the Google developer site:
When you want to provide navigation tabs in an activity, using the
action bar's tabs is a great option (instead of using TabWidget),
because the system adapts the action bar tabs for different screen
sizes—placing them in the main action bar when the screen is
sufficiently wide, or in a separate bar (known as the "stacked action
bar") when the screen is too narrow, as shown in figures 9 and 10.
If you make longer tabs, add more actions to the bar, and/or test it in a thinner view (portrait on a mobile phone, for example), it should break it out to a second bar automatically.
Note the view in their examples (copied below) the second one that stacks them is much more narrow than the first.
Figure 9
Figure 10
I have had some trouble finding an example I understand for using ActionBarSherlock. I can implement the TabNavigation.java example that comes with the download. I'm not sure how to extend this example so that it triggers a new activity but still keeps the tabs displayed. Here's the example that I've implemented:
package com.actionbarsherlock.sample.demos;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
public class TabNavigation extends SherlockActivity implements ActionBar.TabListener {
private TextView mSelected;
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_navigation);
mSelected = (TextView)findViewById(R.id.text);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
mSelected.setText("Selected: " + tab.getText());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
}
I believe I need to create a new "Listener" class and pass that to the .setTabListener() method. I want to stick with Activities (not Fragments) as I have a pretty complex implementation of ORMLite and I'm not sure how to use ORMLite with Fragments yet.
I want to add a new tab labeled "Profile" which triggers ProfileActivity.class.
Thanks in advance!
I guess that TabListener is supposed to work with Fragments and FragmentTransaction. You will have to move your code from ProfileActivity to a new ProfileFragment class and add it with the FragmentTransaction you receive by the listener.
I would suggest you to add ViewPagerIndicator library on your project, just like this answer. A ViewPager is just like tabs plus the swype movement to navigate between them.
It sounds like the reason I'm not finding the example I am looking for is because you just can't use the action bar persistently with Activities. Comments or a better answer would be much appreciated. :)
It is maybe quite a newbie question but anyway. Since Tabhost is depreciated I tried to switch to the action bar tabs but I have my problems using fragments. Is there a possibility to use activities within the action bar tabs anyway?
I would appreciate any help.
Thanks.
If you're set on using Activities over Fragments you could just use an intent to launch your activity from your ActionBar.TabListener
startActivity(new Intent(thisActivity(), thatActivity.class));
You should also check out this comment about using Fragments over Activities
Is there a possibility to use activities within the action bar tabs anyway?
Fortunately, no.
That does not mean you have to use fragments, though. Your TabListener can do whatever it wants to affect the change in your UI. A brute-force solution would be to call setContentView() again, to dump all your old widgets and lay down a brand-new (presumably different) set.
It's possible to use an Activity with the ActionBar. Beware this is not intended behaviour though, but that doesn't mean it doesn't work perfectly.
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
//#SuppressLint("NewApi")
public class ActionBarActivity extends Activity {
private String TAG = getClass().getName();
private Intent i = null;
private ActionBar actionBar;
private Tab one;
private Tab two;
private Tab three;
// create a tab listener that is called when the user changes tabs
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
if (tab.getTag().equals("one")){
Log.d(TAG, "tab one selected");
i = new Intent(getApplicationContext(), One.class);
determineRun();
}
if (tab.getTag().equals("two")){
Log.d(TAG, "tab two selected");
i = new Intent(getApplicationContext(), Two.class);
determineRun();
}
if (tab.getTag().equals("three")){
Log.d(TAG, "tab three selected");
i = new Intent(getApplicationContext(), Three.class);
determineRun();
}
}
#Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// we only need to start the Activity if it's not actually already the current Activity!
void determineRun(){
if (!TAG.equals(i.getComponent().getClassName())){
startActivity(i);
}
return;
}//end method
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setSubtitle(getResources().getString("subtitle"));
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
one = actionBar.newTab();
one.setText("Tab 1").setTag("one");
two = actionBar.newTab();
two.setText("Tab 2").setTag("two");
three = actionBar.newTab();
three.setText("Tab 3").setTag("three");
one.setTabListener(tabListener);
two.setTabListener(tabListener);
three.setTabListener(tabListener);
// You will have to set the selected Tab manually
// A good idea would be to create a subclass for each Tab based on this code
// Then, just create a new Activity which extends ActionBarActivity
actionBar.addTab(one, 0, false);
actionBar.addTab(two, 1, true); // selected Tab
actionBar.addTab(three, 2, false);
}//end method
#Override
public void onResume(){
super.onResume();
Log.d(TAG, "onResume()");
Log.d(TAG, ""+i.getComponent().getClassName());
// again, here you need to select the Tab manually
if (!TAG.equals(i.getComponent().getClassName())){
actionBar.selectTab(two); // selected Tab
}
}//end method
#Override
public void onPause(){
super.onPause();
Log.d(TAG, "onPause()");
}//end method
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}//end method
}//end class
You probably want to override the animation in your Activity so the change of tabs is seemless.
To do so, modify the onCreate() method of your Activity which extends ActionBarActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(0, 0);
}//end method
if my understanding is correct, you want to use action bar to swap activity instead of fragment. in this case, please continue to read.
from the official document you can see, the actionbar feature defines a set of ui, position. if you want to implement actionbar with activity, the most important thing to do is
1. to associate your tab(position) with your activity.
2. add tablistener callback(instantiate your new activity, stop the current activity) every time the tab is click
the best design is to have the tablistener implemented a seperate class, so that each of your activity could use this class.
I am trying to hide the title part of my actionbar using ActionBarSherlock like in the second picture:
Setting:
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
works for versions >3.0 but doesn't work on older versions. A black space remains over the tab bar.
Is there a workaround to solve that issue?
This feature is only available in ActionBarSherlock 4.0 which is currently in beta stage. You can find a link to the betas on actionbarsherlock.com.
There is a demo for precisely what you are trying to accomplish in the samples for 4.0.
public class TabNavigationCollapsed extends SherlockActivity implements ActionBar.TabListener {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar ab = getSupportActionBar();
//The following two options trigger the collapsing of the main action bar view.
ab.setDisplayShowHomeEnabled(false);
ab.setDisplayShowTitleEnabled(false);
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ab.addTab(ab.newTab().setText("Tab 1").setTabListener(this));
ab.addTab(ab.newTab().setText("Tab 2").setTabListener(this));
ab.addTab(ab.newTab().setText("Tab 3").setTabListener(this));
}
#Override public void onTabReselected(Tab tab) {}
#Override public void onTabSelected(Tab tab) {}
#Override public void onTabUnselected(Tab tab) {}
}
You can try with this, it worked for me
if (android.os.Build.VERSION.SDK_INT <= 10) {
setTheme(R.style.Theme_Mo);
}
or
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Hope that this helps