Search code examples
androideclipsetogglebutton

Button Display Android Eclipse


I have added a toggleButton, but it is not showing up and I cannot figure out why. I have completed the Android tutorial, and have looked back at the code, as well as many other sources. The objective is to have the program pause when the button is ON. At the moment, the button will not even show up on the user interface. Any suggestions?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:onClick="pauseCounter" />

</RelativeLayout>

package com.evorlor.counter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent counter = new Intent(MainActivity.this, Counter.class);

        startActivity(counter);

    }

    public void pauseCounter(View view) {
        Intent pause = new Intent(this, Pause.class);
        startActivity(pause);
    }

    // @Override
    // public boolean onCreateOptionsMenu(Menu menu) {
    // // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.activity_main, menu);
    // return true;
    // }

}

package com.evorlor.counter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Pause extends Activity{

    public void onCreate(Bundle instPause) {
        super.onCreate(instPause);

        TextView tv = new TextView(this);

        tv.setTextSize(250);
        tv.setText("PAUSE");
        setContentView(tv);

    }
}

Here is my Counter class. It is messy. Sorry. This is as close as I have come so far. Help would be very appreciated! I have spent a lot of time on this measly button! Thanks!

package com.evorlor.counter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.ToggleButton;

public class Counter extends Activity {

    private int count = 0;
    private int hiCount = 0;
    private boolean capCount = false;

    public void onCreate(Bundle instCounter) {
        super.onCreate(instCounter);

        TextView tv = new TextView(this);

        tv.setTextSize(250);
        if (count < 10000 && capCount == false) {

            tv.setText(Integer.toString(count));
        } else {
            capCount = true;
            if (count >= 10000) {
                hiCount += 10;
                count -= 10000;
            }
            if (hiCount < 100) {

                tv.setText(hiCount + "k+" + count);
            } else {
                tv.setText("Over\n100k");
            }
        }
        tv.setGravity(Gravity.CENTER);

        setContentView(tv);

        ToggleButton butPause = new ToggleButton(this);

        if (butPause == null) {
            Intent pause = new Intent(this, Pause.class);
            startActivity(pause);
        }

    }
    // public void pauseCounter(View view) {
    // Intent pause = new Intent(this, Pause.class);
    // startActivity(pause);
    // }

}

Solution

  • protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Intent counter = new Intent(MainActivity.this, Counter.class);
    
        startActivity(counter);
    
    }
    

    You are starting a new activity right away. thats not a good idea.
    You are going to see what is in your Counter activity, not what is in your main activity, reason you don't see your toggle button. Comment out startActivity() just to check.