Button Listener not registering on first click in android
Since I am new to Android I got a problem for button listeners I am using OnClickListener for busttons but it fails after the first click when I click more than one which it does well, but how to make it possible on the first click here is my code :
public class DashbordActivity extends Activity implements OnClickListener{
ImageButton btnLogout, btnSearch, btnOENew, btnAENew,btnSync;
// Session Manager Class
SessionManager session = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashbord_activity);
// Session Manager
session = new SessionManager(getApplicationContext());
/* Action Bar Color change on create*/
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#FF7F24"));
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(colorDrawable);
/* get Resources from Xml file */
btnOENew = (ImageButton) findViewById(R.id.btnOENew);
btnAENew = (ImageButton) findViewById(R.id.btnAENew);
btnSearch = (ImageButton) findViewById(R.id.btnSearch);
btnLogout = (ImageButton) findViewById(R.id.btnLogout);
btnSync = (ImageButton)findViewById(R.id.btnSync);
addButtonListener();// on click any button
}
// on click any button
private void addButtonListener() {
// Find our button and hook-up the click routine
btnOENew.setOnClickListener(this);
btnSearch.setOnClickListener(this);
btnAENew.setOnClickListener(this);
btnLogout.setOnClickListener(this);
btnSync.setOnClickListener(this);
}
// on click any button
@Override
public void onClick(View v) {
btnOENew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), OceanSalesActivity.class);
startActivity(intent);
}
});
btnAENew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), AirSalesActivity.class);
startActivity(intent);
}
});
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), SearchActivity.class);
startActivity(intent);
}
});
btnLogout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onLogout();
}
});
btnSync.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent( getBaseContext() , SyncActivity.class);
startActivity(intent);
}
});
}
+2
source to share
3 answers
Why do you need an anonymous inner class if you have
btnOENew.setOnClickListener(this);
and your class implements OnClickListener
All you need is a switch enclosure in onClick
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnOENew :
// button btnOENew clicked
break;
case R.id.btnAENew :
// button btnAENew clicked
break;
... // similar for other buttons
}
}
+8
source to share
You register listeners for the button two times. After pressing the button for the first time, you register the button again with the original listener. Remove all buttons onClick methods and change your code as follows.
// on click any button
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnOENew) {
Intent intent = new Intent(getBaseContext(),
OceanSalesActivity.class);
startActivity(intent);
} else if (v.getId() == R.id.btnAENew) {
Intent intent = new Intent(getBaseContext(), AirSalesActivity.class);
startActivity(intent);
} else if (v.getId() == R.id.btnSearch) {
Intent intent = new Intent(getBaseContext(), SearchActivity.class);
startActivity(intent);
} else if (v.getId() == R.id.btnLogout) {
onLogout();
} else if (v.getId() == R.id.btnSync) {
Intent intent = new Intent(getBaseContext(), SyncActivity.class);
startActivity(intent);
}
}
+1
source to share
**
try it
**
public class DashbordActivity extends Activity {
ImageButton btnLogout, btnSearch, btnOENew, btnAENew,btnSync;
// Session Manager Class
SessionManager session = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashbord_activity);
// Session Manager
session = new SessionManager(getApplicationContext());
/* Action Bar Color change on create*/
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#FF7F24"));
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(colorDrawable);
/* get Resources from Xml file */
btnOENew = (ImageButton) findViewById(R.id.btnOENew);
btnAENew = (ImageButton) findViewById(R.id.btnAENew);
btnSearch = (ImageButton) findViewById(R.id.btnSearch);
btnLogout = (ImageButton) findViewById(R.id.btnLogout);
btnSync = (ImageButton)findViewById(R.id.btnSync);
btnOENew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), OceanSalesActivity.class);
startActivity(intent);
}
});
btnAENew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), AirSalesActivity.class);
startActivity(intent);
}
});
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), SearchActivity.class);
startActivity(intent);
}
});
btnLogout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onLogout();
}
});
btnSync.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent( getBaseContext() , SyncActivity.class);
startActivity(intent);
}
});
}
+1
source to share