I created my Sherlock fragment activity and implemented tabs:
public class Home extends SherlockFragmentActivity
{
ActionBar actionBar;
TabHost myTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
actionBar=getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1= actionBar.newTab();
ActionBar.Tab tab2= actionBar.newTab();
//ActionBar.Tab tab3 = actionBar.newTab();
tab1.setText("Contacts");
tab2.setText("Inbox");
// tab3.setText("Outbox");
tab1.setTabListener(new MyTabListener());
tab2.setTabListener(new MyTabListener());
actionBar.addTab(tab1, true);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
And this is my tab change listener:
private class MyTabListener implements TabListener
{
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition()==0)
{
FragmentA frag = new FragmentA ();
ft.replace(android.R.id.content, frag );
}
else
{
FragmentB frag = new FragmentB ();
ft.replace(android.R.id.content,frag );
}
}
And this is my FragmentA:
public class FragmentA extends Fragment
{
TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.activity_list, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
}
Here under my first tab, there is a list that is displaying. When I click any one of the list items, I need to load another fragment under the same activity. And when back is pressed, the old fragment needs to be loaded under the same tab.
Is this possible in actionbarsherlock tabs? If so, how do I do this?
Related
Hi i have an an ActionBar that contains a TabBar and on each tab i want to change the actionbar title/icon based on which tab is selected. How do i detect which tab i'm on and run an if statement for example if tab = tab1 then set actionbar title?
heres my activity
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getActionBar().setTitle("");
getActionBar().setIcon(R.drawable.ab_logo);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));
actionbar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#eeeeee")));
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View tabView = inflater.inflate(R.layout.tab_3, null);
ActionBar.Tab Tab1 = actionbar.newTab().setIcon(R.drawable.ic_tab_1);
ActionBar.Tab Tab2 = actionbar.newTab().setIcon(R.drawable.ic_tab_2);
ActionBar.Tab Tab3 = actionbar.newTab().setCustomView(tabView);
ActionBar.Tab Tab4 = actionbar.newTab().setIcon(R.drawable.ic_tab_4);
ActionBar.Tab Tab5 = actionbar.newTab().setIcon(R.drawable.ic_tab_5);
Fragment Fragment1 = new Fragment1();
Fragment Fragment2 = new Fragment2();
Fragment Fragment3 = new Fragment3();
Fragment Fragment4 = new Fragment4();
Fragment Fragment3 = new Fragment5();
Tab1.setTabListener(new MyTabsListener(Fragment1));
Tab2.setTabListener(new MyTabsListener(Fragment2));
Tab3.setTabListener(new MyTabsListener(Fragment3));
Tab4.setTabListener(new MyTabsListener(Fragment4));
Tab5.setTabListener(new MyTabsListener(Fragment5));
actionbar.addTab(Tab1);
actionbar.addTab(Tab2);
actionbar.addTab(Tab3);
actionbar.addTab(Tab4);
actionbar.addTab(Tab5);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
}
heres my TabListener Class
public class TabListener implements ActionBar.TabListener {
public Fragment fragment;
public Context c;
public ActionBar actionbar;
public TabListener(Fragment fragment, Context con) {
this.fragment = fragment;
this.c = con;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
You could also do this in your onTabSelected method
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
switch (tab.getPosition()) {
case 1:
actionbar.setTitle("new title");
actionbar.setIcon(iconDrawable);
break;
case 2:
actionbar.setTitle("new title");
actionbar.setIcon(iconDrawable);
break;
}
}
You can do this in each fragment:
private ActionBar actionBar;
in onActivityCreated:
actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
in fragment onCreateView method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
setHasOptionsMenu(true);
return rootView;
}
then in onCreateOptionsMenu(Menu menu, MenuInflater inflater) method
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
// Clear old menu.
menu.clear();
// Inflate new menu.
inflater.inflate(R.menu.your_fragment_menu, menu);
// Set actionbar title and icon.
actionBar.setTitle("your fragment title");
actionBar.setIcon(R.drawable.ic_fragment);
}
Hope this answer help you.
I have actionbar tabs and when a tab is clicked I want to add new button to my fragment.
This is my fragment code where I am adding buttons:
Button btn;
View myView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int numberOfButtons= getArguments().getInt("someInt",0);
LinearLayout view = new LinearLayout(getActivity());
// Inflate the layout for this fragment
view.setOrientation(LinearLayout.VERTICAL);
view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int i = 0;i<numberOfButtons;i++)
{
btn = new Button(getActivity());
view.addView(new Button(getActivity()));
}
myView = view;
return myView;
}
This my MainActivity code where I am sending number of buttons to the fragment:
int numberOfButtons=0;
public static FragmentA newInstance(int someInt) {
FragmentA myFragment = new FragmentA();
Bundle args = new Bundle();
args.putInt("someInt", someInt);
myFragment.setArguments(args);
return myFragment;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i=0;i<10;i++)
{
ActionBar.Tab tab = actionBar.newTab().setText("Tab"+i).setTabListener(new ActionBar.TabListener() {
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
String tabText = (String)tab.getText();
String asd = (String)(tabText.substring(3,tabText.length()));
numberOfButtons = Integer.parseInt(asd);
FragmentA fragmentA = newInstance(numberOfButtons);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.mainLayout,fragmentA,"fragA");
transaction.commit();
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
});
actionBar.addTab(tab);
}
}
This code adds buttons but there is a problem here. When Tab1 is clicked, one button is added to the fragment. When Tab2 is clicked, two buttons are added to the fragment but the first button that added by Tab1 is not removed. One of the new buttons is placed over it.
Is there any way to reset the fragment layout or remove old items of fragment before adding new ones?
From your code it seems like you're just adding the fragments' instances on top of each other.
The button added by Tab1 is not removed because Tab1 is still there in the background...
Try using the transaction.remove() method to remove the previous fragment before calling transaction.add() to add a new one...
There's also the transaction.replace() method that does both operations at the same time. Perhaps it's also worth a try.
I'm implementing ActionBar tabs according to official guide.
I have a fragment that is added to activity. In that fragment I'm creating tabs:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab tab = actionBar.newTab()
.setText("One")
.setTabListener(new TabListener<DemoFragment>(
this, "1", DemoFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Two")
.setTabListener(new TabListener<DemoFragment>(
this, "2", DemoFragment.class));
actionBar.addTab(tab);
}
But these tabs are never displayed. Nor content of DemoFragment. Even though at runtime getActionBar().getTabCount() returns correct count of tabs. And I see that DemoFragment is initialized. All I see is ActionBar.
What am I doing wrong?
I'm not using any support libraries as I'm developing for minSdk=14.
Update
IF YOUR NOT USING ANY SUPPORT LIBRARY THIS SHOULD WORK
otherwise you will have to use the getSupportActionBar() and above mentioned dependencies.
Try something more like this:
// Get the Instance of the Action Bar, set Navigation Mode, remove title
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
// One tab
actionBar.addTab(actionBar.newTab() .setText("One")
.setTabListener(new TabListener<DemoFragment>(
this, "1", DemoFragment.class)));
// Two tab
actionBar.addTab(actionBar.newTab()
.setText("Two")
.setTabListener(new TabListener<DemoFragment>(
this, "2", DemoFragment.class)));
Edit to Post - Working Code that I have used for a demo
Here is code from a demo Application That i wrote using Android sdk 14 Just like you are.
public class MainActivity extends Activity {
// String Titles
static String [] titles = {"Fragment 1", "Fragment 2"};
static String [] fragmentClasses = {Fragment1.class.getName(), Fragment2.class.getName()};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleTabListener.SetUpTabNavigation(this, fragmentClasses, titles);
}
And here is the SimpleTabListener Class
public class SimpleTabListener implements TabListener {
Context m_context;
String m_fragmentClassName ;
Fragment m_fragment = null;
public SimpleTabListener(Context context, String tabFragmentClassName)
{
m_context = context;
m_fragmentClassName = tabFragmentClassName;
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
if(m_fragment == null)
{
m_fragment = Fragment.instantiate(m_context, m_fragmentClassName);
ft.add(android.R.id.content, m_fragment);
}else
{
ft.attach(m_fragment);
}
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
if(m_fragment != null){
// TODO Auto-generated method stub
ft.detach(m_fragment);
}
}
public static void SetUpTabNavigation(Activity activity, String [] classNames, String [] tabTitles){
ActionBar actionBar = activity.getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener listener;
for(int i = 0; i < tabTitles.length; i++)
{
listener = new SimpleTabListener(activity, classNames[i]);
actionBar.addTab(actionBar.newTab().setText(tabTitles[i]).setTabListener(listener));
}
}
}
Frament1.class
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_1, container, false);
}
}
Fragment 2.class
public class Fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment2, container, false);
}
}
it's a multi tap activity >>
I'm trying to set a layout for each tab but it doesnt work !
it simply shows nothing in both tabs !
Here's the code
public class Game extends Activity {
public static Context appContext;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
//ActionBar gets initiated
ActionBar actionbar = getActionBar();
//Tell the ActionBar we want to use Tabs.
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//initiating both tabs and set text to it.
ActionBar.Tab roundTab = actionbar.newTab().setText("Round 55");
ActionBar.Tab scoreTab = actionbar.newTab().setText("Score 55");
//create the two fragments we want to use for display content
Fragment roundFragment = new roundFragment();
Fragment scoreFragment = new scoreFragment();
//set the Tab listener. Now we can listen for clicks.
roundTab.setTabListener(new MyTabsListener(roundFragment));
scoreTab.setTabListener(new MyTabsListener(scoreFragment));
//add the two tabs to the actionbar
actionbar.addTab(roundTab);
actionbar.addTab(scoreTab);
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
public class roundFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.round, container, false);
}
}
public class scoreFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.score, container, false);
}
}
}
Your onTabSelected() is empty. that's why it shows nothing on selection.
Add this code to your onTabSelected() of your MyTabsListener .
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add([id the container /viewgroup in which you have to load your fragment],fragment)
}
It's not the way to do it, and it seems over complicated...for UI with Tabs, I suggest you to use the ViewPagerIndicator library, it's well known, reliable and easy to use.
Is it possible to display tabs without using fragments in android 3.0?I have created actionbar with tabs extending fragments.But without using fragments and by extending activity will i be able to display tabs.I need to achieve that.pls help.I have posted my code also.
Java Code :
public class ActionbarActivity extends Activity {
ActionBar bar;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabA = bar.newTab().setText("Home");
ActionBar.Tab tabB = bar.newTab().setText("Listings");
ActionBar.Tab tabC = bar.newTab().setText("Remote");
Fragment fragmentA = new ATab();
Fragment fragmentB = new BTab();
Fragment fragmentC = new CTab();
bar.setDisplayShowHomeEnabled(true);
tabA.setTabListener(new MyTabsListener(fragmentA));
tabB.setTabListener(new MyTabsListener(fragmentB));
tabC.setTabListener(new MyTabsListener(fragmentC));
bar.addTab(tabA);
bar.addTab(tabB);
bar.addTab(tabC);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option, menu);
return true;
}
protected class MyTabsListener implements ActionBar.TabListener {
private Fragment mfragment;
public MyTabsListener(Fragment fragment) {
this.mfragment = fragment;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment_place, mfragment, null);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(mfragment);
}
}
}
public class ATab extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.atab, container, false);
}
}
public class BTab extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.btab, container, false);
}
}
public class CTab extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.ctab, container, false);
}
}
xml.code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/fragment_place"></LinearLayout>
</RelativeLayout>
You don't have to use fragments to use tabs and I've successfully done this within one of my apps, just changing content on tab selection.
public class Main extends Activity implements ActionBar.TabListener {
int mDisplayMode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
mDisplayMode = settings.getInt("displayMode", 0);
ActionBar actionBar = getActionBar();
actionBar.addTab(
actionBar.newTab().setText("Decimal").setTabListener(this)
);
actionBar.addTab(
actionBar.newTab().setText("Hexadecimal").setTabListener(this)
);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.selectTab(actionBar.getTabAt(mDisplayMode));
// Other code
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mDisplayMode = tab.getPosition();
// do stuff based on new tab selected
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// do nothing
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do nothing
}
// ..
}