alsalimi alsalimi - 11 days ago 5
Android Question

Button listener doesn't work

I have two buttons one of them click listener works perfect and the other doesn't work I tried to delete code and rewrite it , clean ,rebuild and nothing solve the problem.
the Button doesn't work is pauseButton ,the others work perfect.
if any one has an idea please help me ..
Thanks in advance..

if (orderEndTrip != null && !orderEndTrip.equals("")) {

accept_order.setText("تم التوصيل ");
accept_order.setOnClickListener(new

View.OnClickListener() {
@Override
public void onClick(View v) {
amountdialog();
}
});
} else {
accept_order.setVisibility(View.INVISIBLE);
EndTrip.setVisibility(View.VISIBLE);
EndTrip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EndTripFun();
}
});
pauseBtn.setVisibility(View.VISIBLE);

pauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


Log.e("pause click", "true");
if (t == 1) {
pauseBtn.setText("توقف");
Log.e("stop ", "true");
starttime = SystemClock.uptimeMillis();
handler.postDelayed(updateTimer, 0);
pauseFun();
t = 0;
} else {
pauseBtn.setText("استمرار");
Log.e("resume", "true");
time.setTextColor(Color.BLUE);
timeSwapBuff += timeInMilliseconds;
handler.removeCallbacks(updateTimer);
resumeTracking();
t = 1;
}
}
});




}

Answer

Try something as below..

In your layout

<Button android:id="@+id/accept_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="تم التوصيل "
android:onClick="onAcceptOrderClicked" />

<Button android:id="@+id/endTrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Trip"
android:onClick="onEditTripClicked" />

<Button android:id="@+id/pauseorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="توقف"
android:onClick="onPauseOrderClicked" />

In your activity

On loading event onCreate() use your if logic..

Button acceptOrderBtn = (Button) findViewById(R.id.accept_order);
Button endTripBtn = (Button) findViewById(R.id.endTrip);
Button pauseOrderBtn = (Button) findViewById(R.id.pauseorder);

if (orderEndTrip != null && !orderEndTrip.equals("")) {
 acceptOrderBtn.setVisibility(View.VISIBLE);
 }else{
 endTripBtn.setVisibility(View.VISIBLE);
 pauseOrderBtn.setVisibility(View.VISIBLE);     
}

public void onAcceptOrderClicked(View v) {
// do your accept order logic here
// amountdialog();
}

public void onEditTripClicked(View v) {
// does something interesting
}

public void onPauseOrderClicked(View v) {
// does something interesting
}
Comments