android java setOnClickListener error - java

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);

Related

How to increase value of a number on button press?

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 + "");
}
});
}
}

Button's onClick property does not show my action

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);
}
}}

Simple onclick button not functioning in android

I have a following snippet of codes. What I want to do is that when I click button1 just show the text. But eclipse suggest me to add onClick(DialogInterface dialog, int which) but then at btnOk.setOnClickListener(oclBtnOk); it gives me this error:
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)
Here is my code:
TextView tvOut;
Button btnOk;
Button btnCancel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
tvOut = (TextView) findViewById(R.id.textView1);
btnOk = (Button) findViewById(R.id.button1);
btnCancel = (Button) findViewById(R.id.button2);
// create click listener
OnClickListener oclBtnOk = new OnClickListener() {
public void onClick(View v) {
// change text of the TextView (tvOut)
tvOut.setText("Button OK clicked");
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
};
// assign click listener to the OK button (btnOK)
btnOk.setOnClickListener(oclBtnOk);
}
You are importing the wrong OnClickListener class.
View.setOnClickListener() Takes a View.OnClickListener, not a DialogInterface.OnClickListener, which is what you have imported.
If you don't use the DialogInterface.OnClickListener elsewhere in this class, simply change your import statement to import android.view.View.OnClickListener.
If you do also use the DialogInterface.OnClickListener interface in your class, you will need to further qualify the class name here, like so:
View.OnClickListener oclBtnOk = new View.OnClickListener() {
public void onClick(View v) {
// change text of the TextView (tvOut)
tvOut.setText("Button OK clicked");
}
}
You should also remove the onClick(DialogInterface dialog, int which) method, as that is only defined for DialogInterface.OnClickListener.
Same problem :
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)
calButton.setOnClickListener(addClick);
Source:
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {
LinearLayout layout1;
EditText no1Text,no2Text;
Button calButton;
TextView answerText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout1=new LinearLayout(this);
no1Text=new EditText(this);
no2Text=new EditText(this);
calButton=new Button(this);
answerText=new TextView(this);
answerText.setText("0");
calButton.setText("Calculate");
layout1.setOrientation(LinearLayout.VERTICAL);
calButton.setOnClickListener(addClick);
layout1.addView(no1Text);
layout1.addView(no2Text);
layout1.addView(calButton);
layout1.addView(answerText);
setContentView(layout1);
}
private OnClickListener addClick=new OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
String firstStr=no1Text.getText().toString();
String secondStr=no2Text.getText().toString();
double firstNo=Double.parseDouble(firstStr);
double secondNo=Double.parseDouble(secondStr);
double sumNo=firstNo+secondNo;
String sumStr=String.valueOf(sumNo);
answerText.setText(sumStr);
}
};
}

chronometer for counting seconds

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/

Dismiss activity in android

I am using the following code to show “Hello” message with ok button when the user clicks a button. In some applications this is working fine. ie while clicking the ok button, the activity is dismissed. But in one application, this is not getting dismissed after clicking the ok button. What to do? Please help.
public class MyClass extends Activity {
private TextView labelTxt;
private Button okBtn;
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.message);
labelTxt = (TextView) findViewById(R.id.txt);
labelTxt.setText("Hello");
okBtn = (Button) findViewById(R.id.okBtn);
okBtn.setOnClickListener(okBtnClickListener);
}
private final OnClickListener okBtnClickListener = new OnClickListener() {
public void onClick(View v) {
finish();
}
};
}
I have tested your code and modified little. Please check below
package test.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btnOK);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
Try like this....
okBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();// Closing Activity
}
});
In your layout make sure your Button is named:
android:id="#+id/okBtn"
.....
Did you import:
import android.view.View.OnClickListener;
Please look at this tutorial, it should help:
http://martin.cubeactive.com/android-onclicklitener-tutorial/

Categories

Resources