I was trying to program a simple calculator in Android Studio, only with plus, minus, times and divide functions. I am half-code in programming of "plus" function, which is triggered by clicking a button. Here is my code in MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class Arithmetic {
public void plus(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
TextView textView = (TextView) findViewById(R.id.textView);
String numberOne = editText.getText().toString();
String numberTwo = editText2.getText().toString();
String result = numberOne + numberTwo;
}
}}
Now I have two questions. Firstly, how do I make it to show result in textView? Secondly, public void plus is not showing in button's box onClick. Why? And how do I make it to show there?
I will be really thankful for every useful tip.
1.Firstly, how do I make it to show result in textView
int sum = (Integer.parseInt(numberOne)) + (Integer.parseInt(numberTwo));
String result = sum + "";
textView.setText(result);
//or
textView.setText(sum + "");
2.Why public void plus is not showing in button's box onClick
<button
----
android:onClick = "plus"/>
and
public class MainActivity extends AppCompatActivity{
onCreate().....
public void plus(View view){
//your button code here
}
No need to create a nested class for that!
This is what you are looking for, but you should learn some Android and Java basics before starting a project:
Remove the onClick property of your button in your xml file, it's better to use a OnClickListener (particularly if you are planning to use multiple buttons)
package com.example.arnaudpradier.calculator;
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.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button calculBtn;
EditText inputFieldOne;
EditText inputFieldTwo;
TextView showResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculBtn = (Button) findViewById(R.id.calculateBtn);
inputFieldOne = (EditText) findViewById(R.id.inputFieldOne);
inputFieldTwo = (EditText) findViewById(R.id.inputFieldTwo);
showResult = (TextView) findViewById(R.id.resultText);
calculBtn.setOnClickListener(this);
}
private void makeAddition() {
int firstValue = (Integer.parseInt(inputFieldOne.getText().toString()));
int secondValue = (Integer.parseInt(inputFieldTwo.getText().toString()));
String result = (firstValue + secondValue) + "";
showResult.setText(result);
}
#Override
public void onClick(View view) {
int i = view.getId();
if (i == R.id.calculateBtn) {
makeAddition();
}
}
}
public class MainActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
textView = (TextView) findViewById(R.id.textView);
Arithmetic a = new Arithmetic();
Arithmetic.plus();
}
public class Arithmetic {
public void plus() {
String numberOne = editText.getText().toString();
String numberTwo = editText2.getText().toString();
String result = numberOne + numberTwo;
textView.setText(result);
}
}}
Related
I am trying to create an app to increase the value of a number on screen to the next number when the button is pressed. It is not working. I have given my Java and XML code below.
XML code:
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:text="+" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:paddingLeft="200dp"
android:textSize="25sp"
android:text="0" />
Java code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
}
public void display(final int n) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText(n);
}
});
}
public void increment(View view) {
a = a + 1;
display(a);
}
}
The button is unreactive.
When you click the button, increase the value of a. Then pass the a to display method. Finally display the value to textView.
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a++;
display(a);
}
});
}
public void display(int n) {
txt.setText("" + n);
//txt.setText(n);
}
}
You have to increment your counter and set new text
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText(String.valueOf(++a));
}
});
}
}
if I am not mistake, we can not call `onClickListener in some method.
In any case, change the code in your button to the next one and it should work.
int a = 0;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
txt.setText(a);
}
});
in this case, that code helps. becose it increases value a on 1, all time when we click on the button.
When you click the on the button, increase the value of a and display to the textView
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a++;
txt.setText(a + "");
}
});
}
}
I have recently started working in android studio, and I am trying to make the UI of an app before I add any SQL, because I don't want to learn a language for a program that won't work, but any way, I tried to use a TableLayout with text boxes, and take user input and insert it into that table. It does not work. I keep getting an error shown in the picture.[error shown][1]
package com.example.tj_n126.firstappattempt;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView lastNameDisplay = findViewById(R.id.lNameTxt);
TextView firstNameDisplay = findViewById(R.id.nameTxt);
TextView phoneNumDisplay = findViewById(R.id.phoneTxt);
TextView emailDisplay = findViewById(R.id.emailTxt);
TextView accBalDisplay = findViewById(R.id.accBal);
firstNameDisplay.setText("First");
lastNameDisplay.setText("Last");
phoneNumDisplay.setText("Phone");
emailDisplay.setText("Email");
accBalDisplay.setText("Balance");
final Button newUserButton = findViewById(R.id.newUserButton);
newUserButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
//this is for inputs on a separate page
setContentView(R.layout.layout2);
final TextView lastName = findViewById(R.id.lastName);
final TextView firstName = findViewById(R.id.firstName);
final TextView phoneNumber = findViewById(R.id.phoneNumber);
final TextView email = findViewById(R.id.emailAddress);
final TextView accountBalance = findViewById(R.id.accountAmount);
Button submitBtn = findViewById(R.id.submitBtn);
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main);
//These are the textboxes in the table
TextView fName = findViewById(R.id.fName);
TextView lName = findViewById(R.id.lName);
TextView phone = findViewById(R.id.phone);
TextView emailAdd = findViewById(R.id.email);
TextView balance = findViewById(R.id.Balance);
fName.setText(firstName + "");
lName.setText(lastName + "");
phone.setText(phoneNumber + "");
emailAdd.setText(email + "");
balance.setText(accountBalance + "");
}
});
}
});
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
[1]: https://i.stack.imgur.com/8CfWD.png
In this line of your code:
fName.setText(firstName + "");
the variable firstName is actually a TextView, so the compiler will implicitly call firstName.toString() which will give you the string that you see (it will NOT give you the text in the TextView). The line should probably be:
fName.setText(firstName.getText());
Ditto for the other places where you use the TextView + "" pattern (which I would discourage from using for any reason).
[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 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);
}
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);