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);
}
};
}
Related
I have Code Image with Pich zoom
package com.androidtutorialpoint;
import com.androidtutorialpoint.imageview.PhotoView;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
public class MainActivity extends Activity {
private ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new HackyViewPager(this);
setContentView(mViewPager);
mViewPager.setAdapter(new SamplePagerAdapter());
}
static class SamplePagerAdapter extends PagerAdapter {
private static int[] sDrawables = {
R.drawable.ic_launcher,R.drawable.ic_launcher,
};
#Override
public int getCount() {
return sDrawables.length;
}
#Override
public View instantiateItem(ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
photoView.setImageResource(sDrawables[position]);
// Now just add PhotoView to ViewPager and return it
container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
return photoView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
I have add imageButton listner ale marker with function switching to a new activity
My Image button lister have Code
public class MainActivity extends Activity {
private ImageView mainBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBtn = (ImageView) findViewById(R.id.button);
mainBtn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openAlert(v);
}
});
}
private void openAlert(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle(this.getTitle()+ " ");
// set positive button: Yes message
alertDialogBuilder.setPositiveButton("więcej",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// go to a new activity of the app
Intent positveActivity = new Intent(getApplicationContext(), PositiveActivity.class);
startActivity(positveActivity);
}
});
// set negative button: No message
alertDialogBuilder.setNegativeButton("wyjście",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// cancel the alert box and put a Toast to the user
dialog.cancel();
Toast.makeText(getApplicationContext(), "Mapa Budnik",
Toast.LENGTH_LONG).show();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show alert
alertDialog.show();
}
}
My Ask how to add a tag to the image Button and when you click the trigger Activity?
When my Image Button is in layaut.xml there is invisible.please help me
I made an application that checks for root using RootTools. Now, I added an option to check for BusyBox availability. When I click on "Check For BusyBox", nothing happens, though the "Check For Root" is working perfectly. I don't understand why is it happening. Please help!
package com.maverick.checkforroot;
import com.stericson.RootTools.RootTools;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Manufacturer = (TextView) findViewById(R.id.Manufacturer);
String Manu = android.os.Build.MANUFACTURER;;
Manufacturer.setText(Manu);
TextView tv1 = (TextView) findViewById(R.id.tv1);
String Model = android.os.Build.MODEL;
tv1.setText(Model);
TextView Product = (TextView) findViewById(R.id.Product);
String Pro = android.os.Build.PRODUCT;;
Product.setText(Pro);
Button Root = (Button) findViewById(R.id.Root);
Root.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (RootTools.isAccessGiven()) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Congratulations!");
builder.setMessage("You Have Root Access!");
builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Oops!");
builder.setMessage("No Root Access!");
builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
Button BusyBox = (Button) findViewById(R.id.BusyBox);
BusyBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (RootTools.isBusyboxAvailable()) {
Toast.makeText(MainActivity.this,"No BusyBox!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"BusyBox Is Available!", Toast.LENGTH_LONG).show();
}
}
});
}
});
}}
All the code concerning the BusyBox button (inclusive the definition of its OnClickListener) is written inside the Root buttons OnClickListener code. Move the code and it should work for you.
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 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/
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);