Get a text value of the button dynamically - android

My View
<Button android:id="#+id/button1" android:text="Text 1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></Button>
<Button android:id="#+id/button2" android:text="Text 2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></Button>
<Button android:id="#+id/button3" android:text="Text 3"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></Button>
<Button android:id="#+id/button4" android:text="Text 4"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></Button>
I have an undefined amount of buttons and need get you text in a touch event.
How i do it?
Tnks a lot.
EDIT:
Thanks for all, this is result of the your Answers => https://play.google.com/store/apps/details?id=br.com.redrails.torpedos
My App :D

If you name all of your buttons "button1, button2, button3..." you can do this:
for(int i = 1; i <= buttonCount; i++) {
int id = getResources().getIdentifier("button" + i, "id", getPackageName() );
findViewById(id).setOnClickListener(this);
}
Where buttonCount is the amount of buttons you have. This method would be placed in onCreate().
Then for your onClick:
public void onClick(View v) {
if (v instanceof TextView) {
CharSequence text = ((TextView) v).getText();
// Do fun stuff with text
}
}
Your Activity will need to implement OnClickListener

You can get the text of a button by using the getText() method.

You can do :
Button button4= (Button) findViewById(R.id.button4);
text = button4.getText();

Here is a complete example:
import android.os.Bundle;
import android.widget.Toast;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener
{
private Button btn1, btn2, btn3, btn4; // You can add more if you like
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewByID(R.id.button1);
btn2 = (Button) findViewByID(R.id.button2);
btn3 = (Button) findViewByID(R.id.button3);
btn4 = (Button) findViewByID(R.id.button4);
// declare the other buttons if any
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
// register the other buttons as well if any
}
#Override
public void onClick(View v)
{
String text = ((Button) v).getText();
Toast.makeText(this, "You clicked " + text , Toast.LENGTH_SHORT).show();
}
}

Related

My simple app closes on button click [duplicate]

This question already has an answer here:
"Unfortunately My App has stopped" [closed]
(1 answer)
Closed 4 years ago.
The app is expected to accept two numbers, and on button click, print the sum in the textView.
The layout appears as expected as this as desired :
Layout
But on button click, the app 'stops like this : Stopping message.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/editText1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="46dp"
android:layout_marginTop="88dp"
android:ems="10"
android:inputType="number"
android:layout_alignParentLeft="true"
android:layout_marginLeft="46dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/editText1"
android:layout_marginEnd="57dp"
android:ems="10"
android:inputType="number"
android:layout_alignParentRight="true"
android:layout_marginRight="57dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="192dp"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="187dp"
android:text="Button" />
</RelativeLayout>
and the java file is.
package com.example.home.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextClock;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText et1, et2;
TextView tv1;
Button bt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
tv1 = (TextView) findViewById(R.id.textView1);
bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int num1, num2, res;
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());
res = num1 + num2;
tv1.setText(res);
}
});
}
}
When I run the app, it shows the layout as desired on the mobile and also on the emulator. It also accepts the two numbers properly. But when the button is clicked, the app stops as shown. I am using Android 3.1.3.
Kindly advise where I am going wrong ?
change this line : tv1.setText(res);
into this tv1.setText(String.valueOf(res));
The cause is tv1.setText(res). There are two methods with one parameter, which are setText(Int) and setText(CharSequence). As in your code, you accidentally called the first method as you declared res as an Integer, which is interpreted as a resource id. You should call the second one by converting res to String.
Just change the line tv1.setText(res); to tv1.setText(Integer.toString(res));
package com.example.home.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextClock;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText et1, et2;
TextView tv1;
Button bt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
tv1 = (TextView) findViewById(R.id.textView1);
bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int num1, num2, res;
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());
res = num1 + num2;
tv1.setText(Integer.toString(res));
}
});
}
}
You can read up the TextView documentation to learn more about its public methods.
You are setting an integer value in textfield, which is not allowed. So you are getting an exception. In order to solve convert integer to string.
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int num1, num2, res;
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());
res = num1 + num2;
tv1.setText("" + res);
}
});
Also you should apply validation. As if user clicks the button without entering any number, the app will stop. So you should add -
if(! et1.getText().toString().equals("") && ! et2.getText().toString().equals("") ) {
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());
res = num1 + num2;
tv1.setText("" + res);
}
else {
Toast.makeText(this, "Enter the numbers", Toast.LENGTH_LONG).show();
}
The res variable should be String type.

Button onClick, Cycle through Colors/Backgrounds

I am using Android Studio
I'm wanting to change the color of my button, with each click.
To cycle it through around 10 colors and start over again in a continuous loop.
For example I'm using setBackgroundResource(#drawable/oval)
oval = blue circle button
oval2 = red circle button
oval3 = green circle button and so on.
So far I have it that button1 starts as oval(blue) and onClick turns into oval2(red)
So my question is, how to add another click to change it to oval3(green) and then cycle it back to the start oval(blue)?
MainActivity.java
package com.example.shadowz.buttononclick;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
private Button colorChangeButton;
private TextView basicText;
private RelativeLayout background;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Drawable oval1;
Drawable oval2;
Drawable oval3;
Drawable oval4;
Drawable oval5;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background = (RelativeLayout) findViewById(R.id.backgroundLayout);
basicText = (TextView) findViewById(R.id.button1);
colorChangeButton = (Button) findViewById(R.id.button1);
// Code Break
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
// Code Break
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button1) {
button1.setBackgroundResource(R.drawable.oval2);
}
}
});
// Code Break
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button2) {
button2.setBackgroundResource(R.drawable.oval3);
}
}
});
// Code Break
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button3) {
button3.setBackgroundResource(R.drawable.oval4);
}
}
});
// Code Break
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button4) {
button4.setBackgroundResource(R.drawable.oval5);
}
}
});
// Code Break
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button5) {
button5.setBackgroundResource(R.drawable.oval6);
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.shadowz.buttononclick.MainActivity"
android:id="#+id/backgroundLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="#+id/button1"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button1"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button2"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button2"
android:background="#drawable/oval"
android:layout_alignTop="#+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="#+id/button3"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button3"
android:background="#drawable/oval"
android:singleLine="false"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button4"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button4"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
android:id="#+id/button5"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button5"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_alignBottom="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="78dp" />
</RelativeLayout>
You can do this by following:
Create integer-array in resources:
<integer-array name="backgrounds">
<item>#drawable/oval1</item>
<item>#drawable/oval2</item>
<item>#drawable/oval3</item>
<item>#drawable/oval4</item>
<item>#drawable/oval5</item>
</integer-array>
Then create special OnClickListener class inside your class code
private static class MyClickListener implements View.OnClickListener {
private int mBackgroundIndex = 0;
private final TypedArray mBackgrounds;
public MyClickListener(Context context) {
mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
}
#Override
public void onClick(View v) {
mBackgroundIndex++;
if (mBackgroundIndex >= mBackgrounds.length()) {
mBackgroundIndex = 0;
}
v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
}
#Override
protected void finalize() throws Throwable {
mBackgrounds.recycle();
super.finalize();
}
}
And then set that listener to each button:
button1.setOnClickListener(new MyClickListener(this));
button2.setOnClickListener(new MyClickListener(this));
button3.setOnClickListener(new MyClickListener(this));
button4.setOnClickListener(new MyClickListener(this));
button5.setOnClickListener(new MyClickListener(this));
This should result in the following MainActivity code:
package com.example.shadowz.buttononclick;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button colorChangeButton;
private TextView basicText;
private RelativeLayout background;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Drawable oval1;
Drawable oval2;
Drawable oval3;
Drawable oval4;
Drawable oval5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background = (RelativeLayout) findViewById(R.id.backgroundLayout);
basicText = (TextView) findViewById(R.id.button1);
colorChangeButton = (Button) findViewById(R.id.button1);
// Code Break
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button1.setOnClickListener(new MyClickListener(this));
button2.setOnClickListener(new MyClickListener(this));
button3.setOnClickListener(new MyClickListener(this));
button4.setOnClickListener(new MyClickListener(this));
button5.setOnClickListener(new MyClickListener(this));
}
private static class MyClickListener implements View.OnClickListener {
private int mBackgroundIndex = 0;
private final TypedArray mBackgrounds;
public MyClickListener(Context context) {
mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
}
#Override
public void onClick(View v) {
mBackgroundIndex++;
if (mBackgroundIndex >= mBackgrounds.length()) {
mBackgroundIndex = 0;
}
v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
}
#Override
protected void finalize() throws Throwable {
mBackgrounds.recycle();
super.finalize();
}
}
}

Radio OK Button Click

How to get value from selected RadioButton when I click OK button? I want to work RadioButton function when I click OK button. NOT isChecked() function. This is my Custom RadioButton Dialog Code.
txt_language.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_language_dialog);
tha = (RadioButton) dialog.findViewById(R.id.tha);
en = (RadioButton) dialog.findViewById(R.id.en);
btnOK = (Button) dialog.findViewById(R.id.btn_ok);
if (tha.isChecked()) {
StoreUtil.getInstance().saveTo("languages", "tha");
closeAllActivities();
}
if (en.isChecked()) {
StoreUtil.getInstance().saveTo("languages", "en");
closeAllActivities();
}
// btnOK.setOnClickListener(this);
dialog.show();
}
});
Method 1 : Selected Data display in Toast.
Tested and working. Check this
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MyAndroidAppActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
xml
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_male"
android:checked="true" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>
for more detail visit this :Android getting value from selected radiobutton
Method : 2 Store selected data into String.
RadioGroup rg = (RadioGroup)findViewById(R.id.radioSex);
String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();

Android Event Handling - Multiple Buttons

Can we create a centralized event handler for multiple buttons in Android?
If so, how can we do that?
Yonatan Nir's solution is the easiest but if you're already using Butterknife you can do it like this:
#OnClick({ R.id.button1, R.id.button2, R.id.button3})
public void onButtonClicked(View theViewThatIsClicked) {
Toast.makeText(this, "Clicked!", LENGTH_SHORT).show();
}
You can do it by using the View parameter you are given:
#Override
public void onClick(final View v)
{
switch (v.getId())
{
case R.id.Button1: ....
case R.id.Button2: ...
}
}
For example:
class MyActivity extends Activity implements View.OnClickListener
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
Button button1 = (Button)findViewById(R.id.Button1);
Button button2 = (Button)findViewById(R.id.Button2);
button1 .setOnClickListener(this);
button2 .setOnClickListener(this);
}
#Override
public void onClick(final View v)
{
switch (v.getId())
{
case R.id.Button1: ....
case R.id.Button2: ...
}
}
}
Here is a pseudo code for this:
class FooActivity extends Activity implements View.OnClickListener{
//inside onCreate(), find buttons with their ids, For ex;
Button btn1 = (Button)findViewById(R.id.btn1);
Buttn btn2 = (Button)findViewById(R.id.btn2)
// and register listeners for appropriate Buttons
btn1.setOnClickListener(this);
btn2.setOnClickListener(this)
//then implement the listener interface method
#Override
public void onClick(View v){
int id = v.getId();
if(id == R.id.btn1 || id==R.id.btn2){
doSameForBothButtons();
}
}
}
}
Here is how I centralize my Click handlers.
Note: It's useful for different types of Views as well, not only Buttons.
Even mixed types (i.e.: you can handle an ImageButton, a TextView and a Button using the same method for all).
Example layout (note the android:onClick="clickHandler" attribute on each Button):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f000"
>
<Button
android:id="#+id/Button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:text="1"
android:onClick="clickHandler"
/>
<Button
android:id="#+id/Button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:text="2"
android:onClick="clickHandler"
/>
<Button
android:id="#+id/Button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:text="3"
android:onClick="clickHandler"
/>
</LinearLayout>
Accompanying code (note the clickHandler method):
package com.dergolem.abc_2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MultiClick
extends Activity
{
Button btn1 = null;
Button btn2 = null;
Button btn3 = null;
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.multi_click);
btn1 = (Button) findViewById(R.id.Button1);
btn2 = (Button) findViewById(R.id.Button2);
btn3 = (Button) findViewById(R.id.Button3);
}
public void clickHandler(final View v)
{
String str = "";
switch (v.getId())
{
case R.id.Button1:
{
str = "1";
break;
}
case R.id.Button2:
{
str = "2";
break;
}
case R.id.Button3:
{
str = "3";
break;
}
}
Toast.makeText(this, "Button " + str, Toast.LENGTH_SHORT).show();
}
}

Resource cannot be resolved or is not a field?

I copy pasted everything from a tutorial, 3 of them saved, but weird thing happened.
It said my Chronometer and button cannot be resolved or is not a field.
StopWatch.java
package com.example.stopwatch2;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class StopWatch extends Activity {
Chronometer mChronometer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_watch);Button button;
mChronometer = (Chronometer) findViewById(R.id.chronometer);
// Watch for button clicks.
button = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
button = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
button = (Button) findViewById(R.id.reset);
button.setOnClickListener(mResetListener);
}
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_stop_watch, menu);
return true;
}
}
activity_stop_watch.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Chronometer android:id="#+id/chronometer"
android:format="#string/chronometer_initial_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="30dip"
android:paddingTop="30dip"
/>
<Button android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start">
<requestFocus />
</Button>
<Button android:id="#+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop">
</Button>
<Button android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset">
</Button>
</LinearLayout>
string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="hello">Hello World, StopWatch!</string>
<string name="app_name">StopWatch</string>
<string name="chronometer_initial_format">Initial format:<xliff:g id="initial-format">%</xliff:g></string>
</resources>
Can anyone tell me what is wrong here?
Try cleaning your project. Right click on your project and say 'clean'. Then try running your app again.
Also, another tip. Don't use the same variable names for all the buttons. Use different names like this:
// Watch for button clicks.
buttonStart = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
buttonStop = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
buttonReset = (Button) findViewById(R.id.reset);
button.setOnClickListener(mResetListener);
Your code will be easier to manage and debug in the long run.

Categories

Resources