Onclicklistener Not Responding

Android: Button OnClickListener does not working

Yes, The Problem is in Declaration of button, write below code instead of your code, it will solve your problem.

public class Menu extends Activity implements OnClickListener{

Button loginbutton, recordbutton, viewbutton, projectsbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);

loginbutton = (Button) findViewById(R.id.butlogin);
loginbutton.setOnClickListener(this);

recordbutton = (Button) findViewById(R.id.butrecordts);
recordbutton.setOnClickListener(this);

viewbutton = (Button) findViewById(R.id.butviewts);
viewbutton.setOnClickListener(this);

projectsbutton = (Button) findViewById(R.id.butprojects);
projectsbutton.setOnClickListener(this);

}

public void onClick(View v){

switch(v.getId())
{
case R.id.butlogin:
{
//open login screen
Intent i = new Intent(this, login.class);
startActivity(i);
break;
}
case R.id.butrecordts:
{
break;
}
case R.id.butviewts:
{
break;
}
case R.id.butprojects:
{
break;
}

}
}
}

android - OnClickListener not working for first click in recyclerview

Make sure you have both focusableInTouchMode & focusable disabled on the button. The first click will get the focus and the second click executes the onClickListener.

.

JavaScript button onclick not working

How about this?

<button id="hellobutton">Hello</button>
<script>
function hello() {
alert('Hello');
}
document.getElementById("hellobutton").addEventListener("click", hello);
</script>

P.S. You should place hello() above of the button.

OnClickListener not responding

When using a DrawerLayout, there should be only one main content View, with the drawer View - in this case, your ListView - listed after it. Using a DrawerLayout in any other way will result in incorrect, unpredictable behavior, often preventing normal interaction with other layout elements.

A tutorial with links to a sample and docs can be found on this developer page.

Android OnClickListener not firing on first click

While it's an old question, I've experienced it too.
Main problem was focusableInTouchMode property. When it was set to true, clicks weren't firing on 1st click, because element element received focus.

Button not responding to OnClick

Make sure you are importing import android.view.View.OnClickListener;

or you can also try like this:

B1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Log.d(TAG, "clicked");

}
});

OnClickListener is not working inside my adapter class

adapterPosition in the ViewHolder's init block is returning -1. So we just need to call adapterPosition in the listener which will solve the issue, since it would retrieve the position when the view holder is created and attached to the recyclerview. Setting any kind of listeners in the init block is a good approach since the onCreateViewHolder called only once to create a view holder but the onBindViewHolder called multiple times for the same view holder, so setting listeners in the onBindViewHolder would be redundant.

   init {

thumbsUp.setOnClickListener {
val tweetId=snapshots.getSnapshot(adapterPosition).get("tweetId")
clickInterface.clickLike(tweetId.toString())
}

}

onClickListener not working (properly) after ConfigurationChange in fragments

Firstly, well done for writing a detailed description of the problem. Many questioners on StackOverflow can learn from the ability of this question to articulate the problem.

This error would seem to come from not accounting for Fragment lifecycle changes. While inside a ViewPager, Fragments will pass through various states including hidden and shown when they are temporarily offscreen, and paused and resumed if they are offscreen and the system frees memory, and finally created and destroyed if the Android system decides to save the instance state of your Activity (such as from a configuration change). In the latter case, Android will try and restore the state of your Fragments from saved instance state in your ViewPager. Since your Fragments have no way to save the View.OnClickedListener they were passed in the arguments, they end up with a null pointer when they are restored which is causing your error.

To fix the error, I suggest that you do not pass the View.OnClickedListener as a parameter to the Fragments. Rather, expose the OnClickListener to your Fragments via a public method in your Activity and have the Fragments get it themselves in their onResume(). This way you can guarantee that the Fragments will have a reference to the View.OnClickedListener whenever they are in a resumed state. So your onResume() might look something like this:

@Override
public void onResume() {
OnClickListener onClickListener = ((MyActivity) getActivity).getOnClickListener();
putIndicatorTabIndicatorOnClick(onClickListener);

}

Since you have set it in onResume(), you should remove it in onPause():

@Override
public void onPause() {
//set the onClickListener to null to free up the resource
}

Using onResume() and onPause() like this to free up resources from listeners is the approach recommended in the developer guide.

Of course, you will have to make sure your Activity also handles its own lifecycle by checking for saved state in onCreate(Bundle savedInstanceState) and restoring it appropriately.

Please note that saving instance state and restoring can be triggered in a number of ways:

  1. Configuration state change (such as rotating the phone and causing the Activity to display in landscape rather than portrait
  2. The device becoming low on memory (for example, if a large number of Activities are in recent apps)
  3. Turning on Developer Options / Don't keep activities and using home to put your application in recent apps and then navigating back to your app.

You will be able to use one of these methods to determine if you have correctly handled the lifecycles of your Activities and Fragments.



Related Topics



Leave a reply



Submit