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 8035719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:23:20+00:00 2026-06-05T02:23:20+00:00

I’m trying to write a simple timer app. I’m creating a counter class that

  • 0

I’m trying to write a simple timer app. I’m creating a counter class that extends chronometer. Anyway, when I add the custom chronometer to my layout in onCreate(), it doesn’t get drawn. It only gets drawn later when I switch to another tab, and then come back to the “counter up tab.” After that it stays. Basically, how do I get it to draw? I tried calling invalidate() but nothing seems to work.

Here’s the code in question:

This is inside my onCreate(). I’m adding the custom Chronometer to a linear layout called linLay.

    public class TimerMain extends Activity {

    // Handle to the tabhost
    public TabHost tabHost;

    // Handle to the tab widget (this is the view that has the two tabs)
    public TabWidget tabWidget;

    // Handle to the LinearLayout inside the ScrollView
    public LinearLayout linLay;

    // Spec used to create the tabs
    public TabSpec spec;

    // Up Counter handle
    public UpCounter myUpCounter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the view to the main layout (see main.xml)
        setContentView(R.layout.main);

        // Get the handle to the tabhost in the layout and set it up
        tabHost = (TabHost) this.findViewById(R.id.my_tabhost);
        tabHost.setup();

        // Get the handle to the TabWidget that is in the 
        // xml file through the TabHost handle
        tabWidget = tabHost.getTabWidget();

        // Get the handle to the relative layout that is inside of the frame
        // that is used for the tab contents
        linLay = (LinearLayout) this.findViewById(R.id.LinLay);

        // Create the Count Up tab and leave it blank
        spec = tabHost.newTabSpec("CU");
        spec.setIndicator("Count Up");
        spec.setContent(R.id.ScrLay);
        tabHost.addTab(spec);

        // Create the Count Down tab and leave it blank
        spec = tabHost.newTabSpec("CD");
        spec.setIndicator("Count Down");
        spec.setContent(R.id.ScrLay);
        tabHost.addTab(spec);

        // Set up the click listeners for each tab
        initTabClickListen();

        // Start with the Count Up tab selected
        tabHost.setCurrentTab(0);

        // Initialize the first counters
        myUpCounter = initFirstCounters(this.getApplicationContext());        
        myUpCounter.start();
        myUpCounter.setBase(SystemClock.elapsedRealtime());
        //Add the first UpCounter to the scroll view
        linLay.addView(myUpCounter);

    }

    @Override
    protected void onResume(){
        super.onResume();
    }

    private UpCounter initFirstCounters(Context context) {
        UpCounter tempCt;
        // Create the first counter
        tempCt = new UpCounter(context);
        tempCt.setFormat("Initial Format: %s");
        return tempCt;

    }

    // Set up the click listeners for each tab
    private void initTabClickListen() {

        // Set up the listener for the Count Up Tab
        tabWidget.getChildAt(0).setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                // Detach all views from the layout
                // and add the views for this tab.
                linLay.removeAllViews();
                linLay.addView(myUpCounter);


                // Set the current tab to Count Up
                tabHost.setCurrentTab(0);
            }
        });

        // Set up the listener for the Count Down Tab
        tabWidget.getChildAt(1).setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                // Detach all views from the layout
                // and add the views for this tab.
                linLay.removeAllViews();
                //scrLay.addView(v2);

                // Set the current tab to Count Down
                tabHost.setCurrentTab(1);
            }
        });

    }
}

// Custom Counter Class
class UpCounter extends Chronometer{

    public UpCounter(Context context) {
        super(context);

    }

}

and last, here is the .xml in case you want to see it. I’ve also tried this by removing the linearlayout and using the scrollview instead to holst the chronometer, I have the same issue.

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
         <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ScrollView 
                android:id="@+id/ScrLay"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <LinearLayout 
                    android:id="@+id/LinLay"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                </LinearLayout>

             </ScrollView>

        </FrameLayout>
    </LinearLayout>
</TabHost>

How can I get this counter to appear upon start of the application?

  • 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-05T02:23:22+00:00Added an answer on June 5, 2026 at 2:23 am

    I figured it out.

    For some reason if I just add this to my code in onCreate():

    // Start with the Count Up tab selected
    tabHost.setCurrentTab(1);
    tabHost.setCurrentTab(0);
    

    It all works right now!

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.