6

I am trying out on the android action bar for 3.0, where I refer to

http://www.youtube.com/watch?v=gMu8XhxUBl8

The code in the TabsActivity are as follow:

package com.test.actionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment; 
import android.app.FragmentTransaction;
import android.os.Bundle;

public class TabsActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tabA = bar.newTab().setText("A Tab");
    ActionBar.Tab tabB = bar.newTab().setText("B Tab");
    ActionBar.Tab tabC = bar.newTab().setText("C Tab");

    Fragment fragmentA = new AFragmentTab();
    Fragment fragmentB = new BFragmentTab();
    Fragment fragmentC = new CFragmentTab();

    tabA.setTabListener(new MyTabsListener(fragmentA));
    tabB.setTabListener(new MyTabsListener(fragmentB));
    tabC.setTabListener(new MyTabsListener(fragmentC));

    bar.addTab(tabA);
    bar.addTab(tabB);
    bar.addTab(tabC);

}

protected class MyTabsListener implements ActionBar.TabListener {

    private Fragment fragment;

    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.add(R.id.fragment_container, fragment, null);

    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
  }
}

for a step by step tutorial, however, after completing the tutorial, I realised that in the TabsActivity, in the onTabSelected method, it will require a variable which is the container_id, which i am not too sure how can i supply that even after looking at the api. I tried removing the line and ran it on the tablet but it throws me a runtimeexception.

Can anyone help me with this?

sorry, I am new to android programming, if the question sounds too simple.

Thanks in advance.

EDIT

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;

public class ActionBarTabs extends Activity {

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("A Tab");
    ActionBar.Tab tabB = bar.newTab().setText("B Tab");
    ActionBar.Tab tabC = bar.newTab().setText("C Tab");

    bar.addTab(tabA);
    bar.addTab(tabB);
    bar.addTab(tabC);
  }
}

UPDATE

package com.debug.actionbartabs;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;

public class TabsActivity extends Activity {
/** 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("A Tab");
   bar.addTab(tabA);

   }
}
Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
simplified.
  • 11,977
  • 5
  • 17
  • 8

1 Answers1

12

Each of the classes should look like this:

public class AFragmentTab extends Fragment
{
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
    return inflater.inflate(R.layout.fragment_a, container, false);
  }
}

And the main activity should look like this:

package com.test.actionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment; 
import android.app.FragmentTransaction;
import android.os.Bundle;

public class TabsActivity extends Activity{

@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("A Tab");
    ActionBar.Tab tabB = bar.newTab().setText("B Tab");
    ActionBar.Tab tabC = bar.newTab().setText("C Tab");

    Fragment fragmentA = new AFragmentTab();
    Fragment fragmentB = new BFragmentTab();
    Fragment fragmentC = new CFragmentTab();

    tabA.setTabListener(new MyTabsListener(fragmentA));
    tabB.setTabListener(new MyTabsListener(fragmentB));
    tabC.setTabListener(new MyTabsListener(fragmentC));

    bar.addTab(tabA);
    bar.addTab(tabB);
    bar.addTab(tabC);

}

protected class MyTabsListener implements ActionBar.TabListener {

    private Fragment fragment;

    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.add(R.id.fragment_container, fragment, null);
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // some people needed this line as well to make it work: 
        ft.remove(fragment);
    }
}

I just found the copy of his code here: http://www.abelski.com/courses/android3ui/actionbar.pdf >_< So in the main.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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_container"></LinearLayout>
</LinearLayout>
sfmirtalebi
  • 370
  • 7
  • 16
rawreth
  • 314
  • 1
  • 4
  • @Jonathon Ho thanks for your reply. am i suppose to declare the id somewhere? because i am not too sure where should i be doing it. i have read through the developer's guide, but still not being able to grasp the concept. would you mind if you can guide me to where i should add stuff such that i can have it working? – simplified. Jun 23 '11 at 21:05
  • Oh I kind of edited it just now oops. I'm pretty sure you put the id in your fragment xml file. Fragments can be read here: http://developer.android.com/reference/android/app/Fragment.html But yeah, it's in the whatever you name your id. It gets passed to the automatically generated R.java so you can use it in your Java code. Have you tried making android forms yet with XML Views? It should be similar to that. – rawreth Jun 23 '11 at 21:10
  • @Jonathan Ho i have tried using the xml layout before, but this time round, it keep getting errors like cannot reference to the class, which i changed to the correct one, but it gets overwritten everything i do it. – simplified. Jun 23 '11 at 21:51
  • For the R.java to generate properly, you can't have any errors in any of the XML files. So make sure the syntax and references are correct in your files including any images, then you can start using IDs from the R.java file. Also, make sure you're not using any Android 3.1 stuff in your Android 3.0 application since that's also incompatible. – rawreth Jun 23 '11 at 21:55
  • @Jonathon Ho yeah, all my stuff are on android 3.0 anyway i didn't duplicate it and just use all tabs to refer to fragment a where i am able to call the title id, but instead, it is now throwing me a runtimeexception. do you happen to know why? – simplified. Jun 23 '11 at 21:58
  • @Jonathan Ho what i used is exactly the same as those shown in the video, hence, shouldn't there be no problems? since the tutorial shows that it is able to be compiled and run. – simplified. Jun 23 '11 at 21:59
  • Can you post your main.xml? Oh, you didn't even write a setContentView(). That's what's missing in your code from the video. – rawreth Jun 23 '11 at 22:47
  • @Jonathan Ho maybe you can help me with this first... i simplified the code to just to create the action bar but it just throws me a runtimeexception. code included in EDIT. – simplified. Jun 23 '11 at 23:00
  • Yeah I know why now. It's because you didn't setContentView(). You gotta do setContentView(R.id.main) before your "ActionBar bar". You don't even have to edit it. My bad I should've just looked for it earlier. It basically makes it work. – rawreth Jun 23 '11 at 23:02
  • @Jonathan Ho i didn't actually modify the main.xml it is the one that is generated when the project is created. – simplified. Jun 23 '11 at 23:03
  • Yeah, it doesn't matter, just setContentView to it. ActionBar Tabs does everything else. I just tried the code myself. It's even in the video. Really sorry I mislead you all over the place >_ – rawreth Jun 23 '11 at 23:04
  • @Jonathan Ho but the issue with the layout xml still doesn't work. it just say that my fragment classes could not be found. – simplified. Jun 23 '11 at 23:13
  • You have to make 3 fragment classes like in the video – rawreth Jun 23 '11 at 23:15
  • @Jonathan Ho hmm, i am trying to redo the tutorial cos i messed up the code, but the thing here is like i said, i was trying to just get the actionbar up without any fragments or whatsoever, just one actionbar with a tab, but it throws me a runtimeeexception. – simplified. Jun 23 '11 at 23:18
  • @Jonathan Ho maybe you can help me with that first? i have updated the code with the stuff i have in tabsactivity. – simplified. Jun 23 '11 at 23:18
  • Okay here is what he did in the video: 1. Create the main activity with onCreate(); 2. Create each AFragmentTab, BFragmentTab, CFragmentTab, java classes. The code is in the video but I can post it. 3. Made 3 xml files fragment_a.xml, fragment_b.xml, fragment_c.xml and put them in the layout folder – rawreth Jun 23 '11 at 23:19
  • @Jonathan Ho the code in the update should by right, show a action bar with one tab right, without any listener or whatsoever. – simplified. Jun 23 '11 at 23:20
  • @Jonathan Ho sorry, i didn't really managed to catch what you are trying to say. – simplified. Jun 23 '11 at 23:21
  • @Jonathan Ho i am not getting the runtimeexception anymore!! but still having problems with the R.id.fragment_place. – simplified. Jun 23 '11 at 23:43
  • @Jonathan Ho I understand that I will have the put the id somewhere to reference it, but i can't really tell where to put it. – simplified. Jun 23 '11 at 23:44
  • @Jonathan Ho thanks.. i managed to get that, but the issue now is still the R.id.fragment_container. maybe you can show me where to add the id? – simplified. Jun 23 '11 at 23:52
  • I just found a copy of his code in his slides http://www.abelski.com/courses/android3ui/actionbar.pdf Just edited the code to fit yours and it should be good to go. hopefully there's the setContentView() and everything that it needs – rawreth Jun 23 '11 at 23:56
  • @Jonathan Ho thanks for all your help. I found out the reason for why it isn't working already. There was some problems with the fragment xml causing the runtime error. – simplified. Jun 27 '11 at 16:12
  • 4
    Don't you need an ft.remove() in onTabUnselected()? – Ed Burnette Jul 12 '11 at 20:39