Reputation: 83
Hy,
I'm new to Android developing, and reading books to get my self familiar with it. This code was in a book too, but it doesn't work so far. The code is simple: Here is the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello"/>
<Button
android:id="@+id/trigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"/>
</LinearLayout>
And here is the Java code:
private int buttonPress = 0;
TextView mButtonLabel;
private long mStartTime = 0L;
TextView mTimeLabel;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(mStartTime == 0L) {
mStartTime = SystemClock.uptimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
mTimeLabel = (TextView)findViewById(R.id.text);
mButtonLabel = (TextView)findViewById(R.id.trigger);
Button startButton = (Button)findViewById(R.id.trigger);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mButtonLabel.setText("Megnyomva: " + ++buttonPress + " alkalommal");
}
});
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
long milis = SystemClock.uptimeMillis() - start;
int seconds = (int) (milis/1000);
int minutes = (seconds/60);
seconds = seconds % 60;
mTimeLabel.setText("" + minutes + ":" + String.format("%02d", seconds));
mHandler.postDelayed(this, 200);
}
};
@Override
protected void onPause() {
mHandler.removeCallbacks(mUpdateTimeTask);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
mHandler.postDelayed(mUpdateTimeTask, 100);
}
My problem is, that the button isn't visible somehow. Any suggestions?
Thanks, Dave.
Upvotes: 0
Views: 69
Reputation: 2362
From
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello"/>
<Button
android:id="@+id/trigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"/>
</LinearLayout>
To
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello"/>
<Button
android:id="@+id/trigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"/>
</RelativeLayout >
Upvotes: 1
Reputation: 13520
Try changing
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello"/>
to
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>
This will give the TextView
only the required space and nothing more.
Upvotes: 0