Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8295415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:34:43+00:00 2026-06-08T14:34:43+00:00

For some reason setCurrentTab is not switching tabs after the first time. This is

  • 0

For some reason setCurrentTab is not switching tabs after the first time.
This is the code that I use.

private OnClickListener buttonListener = new OnClickListener(){

    @Override
    public void onClick(View v) {
        tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));

    }
};

It is connected to buttons each of which have a tag that equals the number of of tab to be shown.
When the button is clicked the first time and this method is called then the tab shows up. I can also see that it executes the code that creates the tab contents.

However, once a tab has been shown once and I have moved to another one, clicking back to it doesn’t work.
The method is definitely called and the tag is also correct. I put in commands to print to the log to confirm that. Also, it works first time round so must be okay.

Any idea?

Full code:

public class TestTabs extends TabbedScreen implements TabHost.TabContentFactory{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTabs();
    }

    @Override
    protected void initTabs() {

        addTab("0",this);
        addTab("1",this);
        addTab("2",this);
        addTab("3",this);

    }

    /*
     * 
     * @param extras The Bundle received in onCreate when this class is first created, and which contains the initial set of objects to be displayed.
     * @return An array containing all of the objects in the list.
     */
    protected Serializable[] getDataArray(Bundle extras) {
        int size = extras.size();
        Serializable[] data = new Serializable[size];
        for (int i = 0; i < size; i++) {
            data[i] = extras.getSerializable(Integer.toString(i));
        }

        return data;
    }

    @Override
    public View createTabContent(String tag) {


        // Get the data returned from the servelet and display it in the ListView
        Log.d("TestTabs","createTabContent");
         ListView lv = (ListView) findViewById(R.id.list_view);
         List<String> list1Strings = new ArrayList<String>();
         switch (Integer.parseInt(tag)){
            case 0:
                Log.d("TestTabs","Case 0");
                lv.setAdapter(new MyAdapter(this,lv,null));
                break;

            case 1:

                Log.d("TestTabs","Case 1");
                list1Strings.add("Item 21");
                list1Strings.add("Item 22");
                list1Strings.add("Item 23");
                list1Strings.add("Item 24");
                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
                break;

            case 2:

                Log.d("TestTabs","Case 2");
                list1Strings.add("Item 31");
                list1Strings.add("Item 32");
                list1Strings.add("Item 33");
                list1Strings.add("Item 34");
                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
                break;      

            case 3:

                Log.d("TestTabs","Case 3");
                list1Strings.add("Item 41");
                list1Strings.add("Item 42");
                list1Strings.add("Item 43");
                list1Strings.add("Item 44");
                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
                break;      
        }

        return lv;
    }


    protected void addTab(String tabName, TabHost.TabContentFactory tabFactory){
        TabSpec tabSpec = tabHost.newTabSpec(tabName);
        tabSpec.setIndicator(tabName);                // Don't set tab layout since we are going to make it invisible
        tabSpec.setContent(tabFactory);
        tabHost.addTab(tabSpec);

        addButton(tabName);


    }

    protected void addButton(String tabName){
        Button button = (Button) buttonHolder.getChildAt(nextChild);
        button.setText(tabName);
        button.setVisibility(View.VISIBLE);
        button.setOnClickListener(buttonListener);
        nextChild--;
    }


    private OnClickListener buttonListener = new OnClickListener(){

        @Override
        public void onClick(View v) {
            v.setBackgroundResource(R.drawable.tab_selected);
            tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));
            Log.d("TabbedScreen","Set tab to " + v.getTag());
            View view;
            for (int i=0; i< childCount; i++)
                if ((view = buttonHolder.getChildAt(i)) != v){
                    view.setBackgroundResource(R.drawable.button_tab);
                    view.invalidate();
                }

        }
    };
}

XML:

       <?xml version="1.0" encoding="utf-8"?>


  <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:roundedListView="http://schemas.android.com/apk/res/com.applicat.meuchedet"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
       android:layout_below="@id/content_screen_user_details">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
            <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:visibility="gone"/>            
            <LinearLayout android:id="@+id/tabButtons" android:layout_width="wrap_content" android:layout_height="20dip">
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button3" 
                    android:background="@drawable/button_tab" android:visibility="invisible"
                    android:tag="3"/>
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button2" 
                    android:background="@drawable/button_tab" android:visibility="invisible"
                     android:tag="2"/>
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button1" 
                    android:background="@drawable/button_tab" android:visibility="invisible"
                    android:tag="1"/>
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button0" 
                    android:background="@drawable/tab_selected" android:visibility="invisible"
                    android:tag="0"/>
            </LinearLayout> 
        </FrameLayout>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <com.applicat.meuchedet.views.RoundedListView
        android:id="@+id/list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:paddingLeft="2dip" 
        android:paddingRight="2dip" 
        android:dividerHeight="1dip"
        android:footerDividersEnabled="true"
        android:listSelector="@drawable/listview_selected_item_background"
        android:fadingEdge="none"
        android:cacheColorHint = "#00000000"
        roundedListView:radius="20"
        roundedListView:border="2"
        />
        </FrameLayout>
    </LinearLayout>
</TabHost>

Edit:

I was wondering if the problem is in the row:

ListView lv = (ListView) findViewById(R.id.list_view);

Am I getting back the same object each time?
If so, how do I get a new one each time based on the one defined in the FrameLayout.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:34:47+00:00Added an answer on June 8, 2026 at 2:34 pm

    How can you be sure that the value assigned to button in your xml is the same you get with only nextChild– .

    not so good solution, you can test it.

    void addButton(String tabName){ 
    Button button = (Button) buttonHolder.getChildAt(nextChild);
    button.getTag() !=  String.valueOf(nextChild); ERROR;  
    

    but It’s better to set it to the same as you always get with buttonHolder.getChildAt()

        Button button = (Button) buttonHolder.getChildAt(nextChild);
        button.setTag(String.valueOf(nextChild));
    

    modified:

    protected void addButton(String tabName){
        Button button = (Button) buttonHolder.getChildAt(nextChild);
        // ----------- modified -----------
        button.setTag(String.valueOf(nextChild));
        button.setText(tabName);
        button.setVisibility(View.VISIBLE);
        button.setOnClickListener(buttonListener);
        nextChild--;
    }
    
    
    private OnClickListener buttonListener = new OnClickListener(){
    
        @Override
        public void onClick(View v) {
            v.setBackgroundResource(R.drawable.tab_selected);
            // ----------- modified -----------
            Button btc = (Button)v;
            int idx;
            try {
              idx = Integer.parseInt((String) btc.getTag());
                } catch(NumberFormatException exx) {
                System.out.println("Could not parse " + exx);
                } 
            if ( idx < childCount) {
               Log.d("TabbedScreen","Set tab to " + String.valueOf(idx);
               Button butv;
               for (int i=0; i< childCount; i++) 
                if ( i != idx){
                butv = (Button)  buttonHolder.getChildAt(i);    
                butv.setBackgroundResource(R.drawable.button_tab);
                butv.invalidate();
                }
               tabHost.setCurrentTab(idx);
               tabHost.focusCurrentTab(idx);
            }
            // ----------- modified ----------- 
    
        }
    };
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason this code is not checking the current password with the database
For some reason, this line of code is returning undefined for $(this).attr(href) $(a).attr(href, javascript:page('
For some reason there's a variable called d that is defined immediately after I
For some reason, I can not get this dialog to show up on the
For some reason my code works for all the 10 digit numbers but not
For some reason the Joomla docs are not very friendly to people that don't
For some reason this is not working. Instead it thinks it should check the
For some reason, Firefox is throwing function not defined errors at this piece of
For some reason I never see this done. Is there a reason why not?
For some reason the code is behaving strangely. This happens in the If Else

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.