[Android newbie] Help required in adding 4 math operations as options to a radion button.
Also I need to perform respective options on selecting radio button option.
package com.sivaneshsg.wallet;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
int cashamount = 0;
int cardamount = 0;
int walletamount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText) findViewById(R.id.inputamount);
final RadioButton card1 = (RadioButton) findViewById(R.id.card1);
final RadioButton cash1 = (RadioButton) findViewById(R.id.cash1);
final RadioButton card2 = (RadioButton) findViewById(R.id.card2);
final RadioButton cash2 = (RadioButton) findViewById(R.id.cash2);
final RadioGroup rg =(RadioGroup) findViewById(R.id.rgroup);
final TextView t1 = (TextView) findViewById(R.id.amountcard);
final TextView t2 = (TextView) findViewById(R.id.amountcash);
final TextView t3 = (TextView) findViewById(R.id.amountwallet);
Button but = (Button) findViewById(R.id.button);
final int amount = Integer.parseInt(et.getText().toString());
cash1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash2.setChecked(false);
card1.setChecked(false);
card2.setChecked(false);
cashamount = cashamount + amount;
}
});
card1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash2.setChecked(false);
cash1.setChecked(false);
card2.setChecked(false);
cardamount=cardamount+amount;
}
});
cash2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash1.setChecked(false);
card1.setChecked(false);
card2.setChecked(false);
cashamount = cashamount - amount;
}
});
card2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash2.setChecked(false);
cash1.setChecked(false);
card2.setChecked(false);
cardamount=cardamount-amount;
}
});
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
t1.setText("Amount in Card : RS. " + cardamount);
t2.setText("Amount in Cash : Rs. " + cashamount);
walletamount = cardamount + cashamount;
t3.setText("Total Amount in Wallet : RS. " + walletamount);
}
});
}
}
your amount variable taking value at onCreate() method which is initially 0
I got this code from a website for chronometer i want to run the chronometer when my activity starts and i want to recive the time running in seconds in a integer value like
myclock = mChronometer.get vaule (example idea);
or anyother methods that can get value from chronometer directly
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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());
}
};
}
Thank u all in advance for helping me
here is the link to the orginal site http://www.edumobile.org/android/android-development/stop-watch-example/
I made an Android Application. Eclipse isn't reporting me what errors are there in my code.
But if I run my project then emulator displays:
emulator error http://s2.ipicture.ru/uploads/20121106/466GCZH9.png
My Java code is (MainActivity.java):
package ru.startandroid.develop.AppLog;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
TextView myText = (TextView) findViewById(R.id.myText);
Button myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
Button myBtnOK = (Button) findViewById(R.id.myBtnOK);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.myBtnCancel:
myText.setText("Нажата кнопка Cancel");
break;
case R.id.myBtnOK:
myText.setText("Нажата кнопка ОК");
break;
}
}
}
You can only use findViewById() after calling setContentView():
public class MainActivity extends Activity implements OnClickListener{
TextView myText;
Button myBtnCancel;
Button myBtnOK;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}
You need to bind the Widgets (TextView and Buttons) calling findById after the setContentView method, because depends of it:
TextView myText;
Button myBtnCancel;
Button myBtnOK;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}
Change your code like below:
Declare variables at instance level and assign values inside onCreate()
public class MainActivity extends Activity implements OnClickListener{
TextView myText = null;
Button myBtnCancel = null;
Button myBtnOK = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}
package walmart.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class WalmartActivity extends Activity {
/** Called when the activity is first created. */
EditText name;
Button search;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
name = (EditText)findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
display = (TextView) findViewById(R.id.tvdisplay);
name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
name.setText("");
}
});
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (name.equals("Electronics")) {
display.setText ('5');
} else if (name.equals("candy")) {
display.setText ('1');
} else if (name.equals("Tobacco")) {
display.setText ("1");
}
}});
};}
No matter what i do, nothing shows up in my output. I'm very, very new to JAVA, so I can't seem to find out why nothing's showing up on my display.
edit Edited my code to what i currently have. Still not working.
Looking at that code, the issue might be with how you are initializing name and search.
Change your code:
name = (EditText) findViewById(getResources().getIdentifier("etName", "id", getPackageName()));
search = (Button) findViewById(getResources().getIdentifier("btnSearch", "id", getPackageName()));
to this:
name = (EditText) findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
Also, add a TextView to display your answer into your main.xml file, give it an id (let's say display for example) android:id="#+id/display" and then add it to your activity so you can change it:
display = (TextView) findViewById(R.id.display);
and then you can update it by calling:
display.setText('some text');
Thus the start of your file should look like this:
public class WalmartActivity extends Activity {
/** Called when the activity is first created. */
EditText name;
Button search;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
display = (TextView) findViewById(R.id.display);
....
Next update your if statements from:
if (name.equals("Electronics"))
to:
if (name.getText().toString().equals("Electronics")) {
display.setText("something");
}
An example piece of code:
Demo2Activity.java
package com.demo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Demo2Activity extends Activity {
private Button button;
private EditText editText;
private TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textText);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString().equals("Electronics")) {
textView.setText("found");
}
}
});
}
}
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="main2.xml"
android:id="#+id/textText" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"/>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>
I am trying to perform a routine when the btnExecute is pressed. I get an error at each onClick btnxxx.setOnClickListener stating "The type new View.OnClickListener(){} must implement the inherited abstract method View.OnClickListener.onClick(View). Please help me understand what's wrong.
package com.androidbook.triviaquiz8;
import android.os.Bundle;
import android.os.Environment;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
public class QuizHelpActivity extends QuizActivity {
private EditText m1_sqs1;
private EditText m1_rs1;
private EditText m1_rs2;
private EditText m1_cd;
private EditText m1_els1;
private EditText m1_els2;
private EditText m1_sa_in;
private EditText m1_sa_ft;
private EditText m1_sa_mm;
private EditText m1_sa_m;
private EditText m1_ed_in;
private EditText m1_ed_ft;
private EditText m1_ed_mm;
private EditText m1_ed_m;
private Spinner m1_sqs1_spinner;
private Spinner m1_rs1_unit;
private Spinner m1_rs2_unit;
private Spinner m1_cd_unit;
private Spinner m1_els1_unit;
private Spinner m1_els2_unit;
private QuizHelpActivity mContext;
private Button btnSave;
private Button btnClear;
private Button btnExecute;
//
// onCreate - let the fun begin!
//
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.help);
m1_sqs1 = (EditText) findViewById(R.id.m1_sqs1);
m1_rs1 = (EditText) findViewById(R.id.m1_rs1);
m1_rs2 = (EditText) findViewById(R.id.m1_rs2);
m1_cd = (EditText) findViewById(R.id.m1_cd);
m1_els1 = (EditText) findViewById(R.id.m1_els1);
m1_els2 = (EditText) findViewById(R.id.m1_els2);
m1_sa_in = (EditText) findViewById(R.id.m1_sa_in);
m1_sa_ft = (EditText) findViewById(R.id.m1_sa_ft);
m1_sa_mm = (EditText) findViewById(R.id.m1_sa_mm);
m1_sa_m = (EditText) findViewById(R.id.m1_sa_m);
m1_ed_in = (EditText) findViewById(R.id.m1_ed_in);
m1_ed_ft = (EditText) findViewById(R.id.m1_ed_ft);
m1_ed_mm = (EditText) findViewById(R.id.m1_ed_mm);
m1_ed_m = (EditText) findViewById(R.id.m1_ed_m);
// Create an OnClick Event in each button.
Button btnExecute = (Button) findViewById(R.id.btnExecute);
Button btnSave = (Button) findViewById(R.id.btnSave);
Button btnClear = (Button) findViewById(R.id.btnClear);
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
btnExecute.setOnClickListener(new OnClickListener() {
private AlertDialog show;
public void onClick(View arg0) {
if ((m1_sqs1.getText().length() == 0)
|| (m1_sqs1.getText().toString() == " ")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("The Square Side length is empty")
.setPositiveButton("OK", null).show();
} else if (operator.getText().equals("")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage(" is null").setPositiveButton(
"OK", null).show();
}
{
double m1_sa_in = new Double(m1_sqs1.getText().toString())
* new Double(m1_sqs1.getText().toString());
m1_sa_in.setText(Double.toString(m1_sa_in));
}
}
}
Where you have
btnSave.setOnClickListener(new OnClickListener() {
Change that for
btnSave.setOnClickListener(new View.OnClickListener() {
It's just the newer way, so if you are following an older tutorial or blog post, that's why it would be outdated.
Edit:
import android.view.View.OnClickListener;
View view = findViewById(android.R.id.content);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
});
Your imports list doesn't include android.view.View, just android.view.View.OnClickListener. Without knowledge of the View object that is a parameter of onClick(), the compiler can't resolve that the implementation is complete. Personally, I would fix it by:
Change (don't add) the import from android.view.View.OnClickListener to android.view.View
Update your declaration as citizen conn mentioned.
I believe you can also do this:
public class QuizHelpActivity extends QuizActivity implements OnClickListener {
adding implements OnClickListener will let you use this. For example, in your onCreate, you can have:
Button btnExecute = (Button) findViewById(R.id.btnExecute);
Button btnSave = (Button) findViewById(R.id.btnSave);
Button btnClear = (Button) findViewById(R.id.btnClear);
btnSave.setOnClickListener(this);
btnClear.setOnClickListener(this);
btnExecute.setOnClickListener(this);
Then you can implement the onClick function after onCreate, like so:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// the rest of your onCreate, including buttons
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnExecute:
// reference a function that would house your current onClick behavior, such as:
execute();
break;
case R.id.btnSave:
break;
case R.id.btnClear:
break;
}
}
I think this is a cleaner approach with fewer nested functions.
try this
btnSave.setOnClickListener(new OnClickListener() {
public void View.OnclickListener.onClick(View arg0) {
}
});
In this case it's maybe a better way to set an OnClick in your XML-file.
Select your 3 buttons, which have an onClickListener, in your XML-layout file. Right click on it > Properties > OnClick and for the 3 buttons give the OnClick a name (ex. onClickButtons). So, in your java code you can do like this:
public void onClickButtons(View v) {
switch (v.getId()) {
case R.id.R.id.btnExecute:
...
break;
case R.id.btnSave:
...
break;
case R.id.btnClear:
...
break;
}
}
And let your class implement the OnClickListener.
For your AlerDialog, this is a better way to handle this:
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("is null")
.setCancelable(false)
.setPositiveButton(
"OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
Greets and have nice development!
First import the following package:
import android.view.View.OnClickListener;
Then change the following like this:
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
You can put seem This
public class MainActivity2 extends Activity implements View.OnClickListener{
Button B2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
B2=(Button) findViewById(R.id.BM2);
B2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Do anything
}
Use this:
button1.setOnClickListener((OnClickListener) this);
Do not use this:
button1.setOnClickListener(this);