error in OnLongClickListener() method - android

akashButton.setOnLongClickListener(
new Button.OnLongClickListener(){
public boolean onClick(View v){
TextView akashHacked = (TextView)findViewById(R.id.akashHacked);
akashHacked.setText("Wow, that was a long one");
return true;
}
}
);
On writing this code in android studio I am getting a red line under Button.OnLongClickListener() and the error says
" class'Anonymous class derived from OnLongClickListener' must either be declared abstract or implement abstract method 'OnLongClick(View)' in 'OnLongClickListener' "`
Please help me in fixing this error.

First you will have to retrieve your button from your layout :
Button myButton = (Button) findViewById(R.id.myButtonId);
then, you will have to set your button's listener :
myButton.setOnLongClickListener(new Button.OnLongClickListener(){
public boolean onLongClick(View v){
// do whatever you want here
}
});

Have you implemented OnItemClickListener (or AdapterView.OnItemClickListener)? If so, remove it.

Related

OnClickListener onLongClickListener logic

Recently I started proggraming from scratch and I'm learning from thenewboston's tutorials and I dont really get how it works and I cant find any answers. Thats the code Im struggling with, it works but i dont really get why there is beetwen () brackets an new Button.OnClick listener and then OnLongClickListener where does the View v come from in these methods ?? Ye my knowledge about object programming may be a bit smaller than its required but I dont really like learning other way than using this.
Button przycisk = (Button) findViewById(R.id.mojprzycisk);
przycisk.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) { WHERE THIS V IS FROM ?
TextView mojtekst = (TextView) findViewById(R.id.mojtekst);
mojtekst.setText("Good job Boss");
}
}
);
przycisk.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v){ <-----WHERE IS IT FROM?
TextView mojtekst = (TextView) findViewById(R.id.mojtekst);
mojtekst.setText("HOLY CARP THAT WAS A LONG ONE");
return true;}}
);
You should look into interfaces for java. A great tutorial for the same is also given here. Once you understand the concepts of interfaces, you can see that OnClickListener and OnLongClickListener are interfaces, and they have methods like onClick(View v) and onLongClick(View v), that are invoked when a particular view has been clicked or long clicked. They return back the view that was clicked and held.
When you say
przycisk.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) { WHERE THIS V IS FROM ?
TextView mojtekst = (TextView) findViewById(R.id.mojtekst);
mojtekst.setText("Good job Boss");
}
}
);
You are creating a new instance of the interface. The above code for easy readability can be also be written as follows.
Button.OnClickListener onClickListener = new Button.OnClickListener() {
#Override
public void onClick(View v) {
TextView mojtekst = (TextView) findViewById(R.id.mojtekst);
mojtekst.setText("Good job Boss");
}
};
przycisk.setOnClickListener(onClickListener);
So basically you're creating an instance of the interface, and since its an interface you need to override the method onClick() and give its definition.
In short to whichever view you set the onClickListener or the onLongClickListener, that view would be returned in the onClick(View v) and onLongClick(View v) methods. That view would be the v.

TextView onclickListener

In my application i have added textviews dynamically using array,and given id's for each with a count variable, which increment's count according to the addition of textviews.In onClickListener of each textview i want to perform some oparations,but when i'm trying to do this operation is getting performed on all textviews.
Below is the code,i'm not getting what's wrong.please help me.
// here i have added textview dynamically
mtxtview[colTextCount]=new TextView(this);
mtxtview[colTextCount].setId(colTextCount);
mtxtview[colTextCount].setLayoutParams(new LayoutParams(20,20));
And In onclickListener-
#Override
public void onClick(View v) {
System.out.println("onclick...");
for(jj=0;jj<_mTextViewId;jj++){
String hh=mtxtview[jj].getText().toString();
System.out.println("................................."+hh);
System.out.println("id is...."+_mTextHeight[jj].getId());
//if i added 3 textview.its giving me all 3 textview's text(getText())
}
}
use switch-case and View.getId() to check which TextView is Clicked before starting for loop . try it as :
#Override
public void onClick(View v) {
System.out.println("onclick...");
switch(v.getId())
{
case _mTextViewId:
for(jj=0;jj<_mTextViewId;jj++){
String hh=mtxtview[jj].getText().toString();
System.out.println("................................."+hh);
System.out.println("id is...."+_mTextHeight[jj].getId());
}
break ;
// same for others....
}
}
see TextView onClick() not working
You should do setOnClickListener for each TextView Object.

android multiply button case switch

I have been looking at some posts and still I cannot get mine code to work (I am a beginner) .. I am just tring to use the toast with my two buttons with a case switch .. When compiling it just crashes .. one something has an idea ?
Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
TextView et = (TextView) findViewById(R.id.txtHeader);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
Button btnDis = (Button) findViewById(R.id.btnDisplay);
btnAdd.setOnClickListener((OnClickListener) this);
btnDis.setOnClickListener((OnClickListener) this);
}
public void OnClick(View v) {
switch (v.getId()) {
case R.id.btnAdd:
// Toast msg = Toast.makeText(getBaseContext(), "Torben", Toast.LENGTH_LONG);
// msg.show();
break;
case R.id.btnDisplay:
// Toast msg1 = Toast.makeText(getBaseContext(), "Henriksen", Toast.LENGTH_LONG);
// msg1.show();
break;
default:
break;
}
}
I see two main problems:
((OnClickListener) this
Make sure your class implements OnClickListener because you never need to cast if you are actually implementing the interface.'
The declaration of the class should be something like:
public class MyActivity extends Activity implements OnClickListener
Then change OnClick to lowercase o.
#Override
public void onClick(View v) {
some log output would be helpful!
a wild guess is that your activiy does not implement OnClickListener, why else would you cast this to OnClickListener?
just check in your layout manifest that whether the buttons id are correct and given the same id which your are using and if it is then please update the question with the LogCat output.
And also check that the activity is defined in manifest because there is no mistake in your code to implemts the onclick listener for multiple buttons.
Enjoy!!
Example adding a button listener:
Button b = ((Button)findViewById(R.id.button_name));
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//do something
}
});
and make sure the button is defined in your xml file with the id #+id/button_name or #id/button_name (just make sure they match)

Keeping multiple OnClickListeners in one class

I am kinda new to Android and it would improve my application a lof if I coul keep several OnClickListenres in one class. What I am thiking of is something like this :
Public class OnClickListeners {
public Button.OnClickListener open;
public Button.OnClickListener doSomethingElse;
public Button.OnClickListener etc;
public OnClickListeners() {
open = new Button.OnClickListener()
{
public void onClick(View view)
{
DetailList.SetId(view.getId());
Intent intent = new Intent(view.getContext(), DetailList.class);
startActivityForResult(intent, 100);
}
};
}
}
So I can then reference it in other class B like this
button1.setOnClickListener(OnClickListeners.open);
Any though how to do it?
Android SDK seems to be against me as I can figure it out now for about 2 days now...
Thanks for any advices and help
There is a sleek way to consolidate all your anonymous classes into one and switch on the view. This works best if you know ahead of time which buttons will be using the clicklistener :
public class AndroidTestClickListenerActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new MyClickListener());
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new MyClickListener());
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new MyClickListener());
}
}
class MyClickListener implements View.OnClickListener {
#Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button1:
// do soemthign for button1
break;
case R.id.button2:
// do something for button2
break;
case R.id.button3:
// do something for button3
break;
default:
// do something for any other button
}
}
}
You can write
button1.setOnClickListener(new OnClickListeners().open);
instead, but this seems an odd architecture for me. I'd suggest you to keep 1 listener in 1 file, having all of them in 1 package, and use like
button1.setOnClickListener(new OpenListener());
The problem of you approach is that usually listeners have to manipulate some data that is part of the class where UI elements are.
If you take listeners out and put them in a separate class, you will also have to provide a lot of references to objects where data to be manipulated is. This will create a lot of interdependent classes, which will not be nice.
IMHO the cleanest way is to use anonymous inner classes.
You can, but you have to declare the OnClickListener as static if you would like to use it in this manner.
public static Button.OnClickListener openListener = new Button.OnClickListener() {
public void onClick(View view) {
}
};
Then you can use:
button1.setOnClickListener(OnClickListeners.openListener);
As noted by other user - this approach is most like bad. You should handle view listeners on the same view and then maybe call another method like openActivity(). I would not do this - you are also openning an activity from another activity, this will probably don't work at all or will mess up the activity history stack
As none of the solutions actually did what I wanted to achieve - I needed a second (or multiple) onClickListener that did not override the onClickListeners that were already assigned to the control.
Here is the java class that I wrote for that purpose:
https://gist.github.com/kosiara/c090dcd684ec6fb2ac42#file-doubleclicklistenerimagebutton-java
public class DoubleClickListenerImageButton extends ImageButton {
View.OnClickListener mSecondOnClickListener;
public DoubleClickListenerImageButton(Context context) {
super(context);
}
[...]
public void setSecondOnClickListener(View.OnClickListener l) {
mSecondOnClickListener = l;
}
#Override
public boolean performClick() {
if (mSecondOnClickListener != null)
mSecondOnClickListener.onClick(this);
return super.performClick();
}
#Override
public boolean performContextClick() {
if (mSecondOnClickListener != null)
mSecondOnClickListener.onClick(this);
return super.performContextClick();
}
}

Variable OnClick Listener Android

Is there a way to have 1 onClick Lister for many buttons where I can toss a case statement to do things based on what buttons were clicked.
I know I can make 100 different listeners for 100 buttons but I have to think I can create some nifty variables to do it in less lines of code.
Button btn1, btn2;
public void onCreate(Bundle b)
{
// here you do normal things like assigning a
// content view to the activity, initiate buttons, etc.
// then you assign the same listener to both buttons
btn1.setOnClickListener(yourListener);
btn2.setOnClickListener(yourListener);
}
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener yourListener = new View.OnClickListener(){
public void onClick (View v){
if( v == btn1 ){
// do something
}
elseif( v == btn1 ){
// do another thing
}
}
};
If you are using 1.6+ version of the SDK you can use android:onClick to set the onClick handler of a view. In your activity you must have a method with the following signature. The view is the view that was clicked.
void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
//do something fantastic;
break;
}
}
public class MainActivity extends Activity implements View.OnClickListener{
btnXXX.setOnClickListener(this);
public void onClick(View v) {
if (v.getId()==R.id.btnXXX){
dialog.show();
} else {
handleOtherViews(v);
}
}
Alternatively, you can specify the method to call in xml:
<Button android:id="#id/button" android:text="#string/button" android:onClick="someMethod" />

Categories

Resources