1

I'm new to android programing and I'm having problems with OnClickListener for tabs in my app. I found on stack a solution how it should be done, but for some reason it's not working.

I'm trying to use the 2nd answer

For some reason I'm getting 2 errors.

First one is on the name on of my activity: The type DragonLords must implement the inherited abstract method View.OnClickListener.onClick(View).

Second one is on the OnClick method: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method.

Here is a part of my code:

public class DragonLords extends TabActivity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, Home.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("home").setIndicator("home",
                      res.getDrawable(R.drawable.hometab))
                  .setContent(intent);
    tabHost.addTab(spec);
    getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (getTabHost().getCurrentTab()==0) {
                getTabHost().setCurrentTab(0);
            }else
            {
                getTabHost().setCurrentTab(0);
            }
        }

    });

After that I'm creating more tabs. With out the onclicklistener it's working, the thing is I need to be able to reload the tabs when they are active.

Anyone have an idea what I'm doing wrong?

I added the necessary imports.

Gatz

Community
  • 1
  • 1
  • Also, the logic in your TabWidget click listener doesn't really register with me. The way I understand those statements is: `If the currentTab is 0, then set the tab to 0; else set the tab to 0`; is that correct? – hwrdprkns Jun 21 '11 at 21:26
  • Yes it is correct. I want to be able to reload the tab that is currently active. When I do an override on the on click method, I'm guessing that clicking the tab while a different one is active won't work unless I put it in the override. So we have a click on the tab. We check which tab is active. If it's this one we reload it, if it's not we just open it up. – Grzegorz 'Gatz' Siennicki Jun 21 '11 at 22:50
  • If what you are saying is correct, that is not how you want to refresh the tab. You refresh the tab by destroying what is inside it and recreating that. So if its re-starting an activity, just call its `finish()` method and then pass that tab a new intent to work with. – hwrdprkns Jun 22 '11 at 12:27
  • You're right my mistake. It wouldn't work the way I thought it would. Thanks for the idea. Still I can't get it to work, and a bunch of other problems appeared :( – Grzegorz 'Gatz' Siennicki Jun 22 '11 at 13:21
  • It sounds like you want [OnTabChangedListener](http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html) instead. Now you can use `getTabHost()` in the `TabActivity`. – hwrdprkns Jun 22 '11 at 13:29
  • Well, I had the wrong idea there and u corrected me. OnTabChangedListener won't do me any good since I'm trying to get a response to a click on an active tab. I guess it's not the best solution but when I click the active tab I will need to change tabs and then go back to the first one to get the refresh effect. I guess I will need to use the OnTabChangeListener in the end. – Grzegorz 'Gatz' Siennicki Jun 22 '11 at 22:35
  • I'll try to describe what I'm trying to do. My tabs are basically webviews. When I have an active tab that displays a page I want to do a refresh of the page by a click on the tab. You gave me a good idea with finishing the activity and starting it up again. The problem is to program the click on the active tab since it's not a standard feature. – Grzegorz 'Gatz' Siennicki Jun 22 '11 at 22:42

2 Answers2

1
  1. You must implement the onClick method not in an anonymous inner class like you have done in your code.
  2. Try using new TabWidget.OnClickListener instead of just the normal OnClickListener

Something akin to the following:

public class TestActivity extends TabActivity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getTabWidget().getChildAt(0).setOnClickListener(new TabWidget.OnClickListener() {

        public void onClick(View v) {
            if (getTabHost().getCurrentTab()==0) {
                getTabHost().setCurrentTab(0);
            }else
            {
                getTabHost().setCurrentTab(0);
            }
        }

    });
}

public void onClick(View theView) {
    // Do something with view here

}
}
hwrdprkns
  • 7,525
  • 12
  • 48
  • 69
  • When I tried using new TabWidget.OnClickListener, I have a 3rd error come up :/ The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){}) – Grzegorz 'Gatz' Siennicki Jun 21 '11 at 23:16
0

you are doing it pretty hard way.. i did it like this...

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.tablayout);

    res = getResources();
    tabHost=(TabHost)this.findViewById(android.R.id.tabhost);

    tabHost.getTabWidget().setDividerDrawable(R.drawable.vertical_seperator);

    setupTab(new TextView(this), "Login",new Intent().setClass(this,loginForm.class));
    setupTab(new TextView(this), "Can't Login",new Intent().setClass(this,ForgotPwd.class));
    setupTab(new TextView(this), "Register",new Intent().setClass(this,RegisterUser.class));
}

private void setupTab(final View view, final String tag,final Intent myIntent) 
{

        View tabview = createTabView(tabHost.getContext(), tag);


      TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(
       new TabContentFactory() 
        {
            public View createTabContent(String tag) 
            {return view;}
        }).setContent(myIntent);

      tabHost.addTab(setContent);


}

private static View createTabView(final Context context, final String text)
{
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}

hope this helps.... tabs_bg is just an xml with

<TextView android:id="@+id/tabsText" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Title"
    android:textSize="15dip" 
    android:textColor="@drawable/tab_text_selector" />

Shashank Degloorkar
  • 3,151
  • 4
  • 32
  • 50