Reputation: 7569
I have spent several hours on this, and I have made very little to no progress. I have asked a similar question, but over a 2 hour period, still have not succeeded. So I will ask this and be as specific as I can. I have put about three hours into this, so please do not write me off.
I want to simultaneously display text and a ToggleButton. I can display one or the other, but not both. Please help! Here is my code:
public class Counter extends Activity {
private int count = 5000;
private int hiCount = 0;
private boolean capCount = false;
@Override
protected void onCreate(Bundle instCounter) {
super.onCreate(instCounter);
setContentView(R.layout.activity_counter);
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 btnPause = new ToggleButton(this);
// if (btnPause == buttonOn) {
// Intent pause = new Intent(this, Pause.class);
// startActivity(pause);
// }
}
public void pauseCounter(View view) {
Intent pause = new Intent(this, Pause.class);
startActivity(pause);
}
// // Show the Up button in the action bar.
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
<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=".Counter" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="ToggleButton" />
</RelativeLayout>
Upvotes: 1
Views: 574
Reputation: 36449
You're setting the Activity to use the TextView as the whole view. Your button will never show like this(and neither really, will anything else you have in your layout)! In your R.layout.activity_counter
make a TextView and Button, and use
TextView tv = (TextView) findViewById (R.id.textView1);
This will allow you to have both UI elements as you are finding the TextView from your layout then working with it while leaving everything else alone.
Upvotes: 1
Reputation: 12642
just change onCreate
like
@Override
protected void onCreate(Bundle instCounter) {
super.onCreate(instCounter);
setContentView(R.layout.activity_counter);
TextView tv = (TextView)(findViewById(R.id.textView1));
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 btnPause = (ToggleButton)(findViewById(R.id.toggleButton1));
if (btnPause == buttonOn) { //Do not know what you want here
Intent pause = new Intent(this, Pause.class);
startActivity(pause);
}
}
Upvotes: 0