I have an ImageButton in my application.
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="80dp"
android:layout_height="80dp"
android:clickable="true"
android:src="#drawable/button" />
I bind it to an onClickListener:
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
// Here I update the image source to a different image.
}
};
Now what happens is: when I click the imagebutton, the imagebutton changes to a
different image. But I want it to change back automatically after 0.5 sec (during the
same time the user should not be able to click anything). How do I
implement that? I tried to sleep in the onClick function in the listener, but it's
not working...
New edit:
The proposed answer will solve my problem if I only have one imagebutton. I tried it out
and both work like charm!
Actually it is not working as expected. During that 500ms, the user still could click!
It is not solving the problem...
Posting a delayed runnable might do the job.
public void onClick(View v) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// change the background of the image button
(ImageButton)v.setBackgroundResource(R.drawable.someimage);
}
}, 500);
}
EDIT:
In fact, I've ran the following code on an actual device with two ImageButton and it works fine.
BTW, if you want the buttons to be un-clickable during the 500ms, just set it as imgBtn1.setClickable(false); and set it back to be clickable in the runnable as imgBtn1.setClickable(true);
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_layout, container, false);
final ImageButton imgBtn1 = (ImageButton) view.findViewById(R.id.test_img_btn1);
final ImageButton imgBtn2 = (ImageButton) view.findViewById(R.id.test_img_btn2);
imgBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imgBtn1.setBackgroundResource(R.drawable.device_type_apple);
imgBtn1.setClickable(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// change the background of the image button
imgBtn1.setBackgroundResource(R.drawable.device_type_windows);
imgBtn1.setClickable(true);
}
}, 500);
}
});
imgBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imgBtn2.setBackgroundResource(R.drawable.device_type_apple);
imgBtn2.setClickable(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// change the background of the image button
imgBtn2.setBackgroundResource(R.drawable.device_type_android);
imgBtn2.setClickable(true);
}
}, 500);
}
});
return view;
}
}
You can use handle with runnable to auto update image
Handler mHandler = new Handler();
Runnable mUpdateTimer = new Runnable() {
#Override
public void run() {
// code update image here
// auto update after 0.5s
mHandler.postDelayed(mUpdateTimer, 500);
}
};
And when image button clicked:
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
mHandler.postDelayed(mUpdateTimer, 500);
}
};
Handler.postDelayed
this method is not good way ,see http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable,long)
it say
Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.
so this methed may never invoke,it may be make you image button status never come back ,so you must be care the return value or use other widget ViewFliper,it can set animation when image switch and you can set delpoy .
Related
I am using following code for using a button. I works.(sendBtn is a button in a fragment)
sendText = view.findViewById(R.id.send_text);
View sendBtn = view.findViewById(R.id.send_btn);
sendBtn.setOnClickListener(v -> send(sendText.getText().toString()));
Now i want to disable the button for 1 sec after a click
I found following solution but above code works "without onClick(View v)"method and without implementing View.OnClickListener in class. How to provide delay in such case..How code is working without onClick method.
#Override
public void onClick(View v) {
((Button) findViewById(R.id.click)).setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
((Button) findViewById(R.id.click))
.setEnabled(true);
}
}, 1000);
}
});
Use this method when you want to interrupt user clicks:
private static long mLastClickTime = 0L;
public static boolean isOpenRecently() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return true;
}
mLastClickTime = SystemClock.elapsedRealtime();
return false;
}
Usage:
if (v.getId() == R.id.sendBtn) {
if (isOpenRecently()) return;
// Your logic
}
Basically you are using lambda in your code, where v->{} represents the onCLick(View v) function.
sendBtn.setOnClickListener(v -> send(sendText.getText().toString()));
You can do the following to disable the button for 1 second
void doOnSendButtonClick(View v){
//Send the message (your logic here)
send(sendText.getText().toString());
//Disable button
sendBtn.setEnabled(false);
//enable button after 1000 millisecond
new Handler(Looper.getMainLooper()).postDelayed(() -> {
sendBtn.setEnabled(true);
}, 1000);
}
And call this method when user clicks on the button
sendButton.setOnClickListener(view -> doOnSendButtonClick(view));
You could use a CountDownTimer.
CountDownTimewr timer = new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// enable button
((Button) findViewById(R.id.click)).setEnabled(true);
}
}
#Override
public void onClick(View v) {
// disable btn and start timer of one second in millis
((Button) findViewById(R.id.click)).setEnabled(false);
timer.start();
}
});
Please try this way
Handler(Looper.getmainLooper()) and remove callback after you done your task inside runnable thread...also try to put this logic in any method outside in this class and call from setonclicklistener
Try the Timer.shedule with timertask
you can use runOnUIThread method inside where you can update UI.
you can also try to check the time difference of click and enable again in looping like while.
I have a custom dialog box that I am displaying in the following way:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dict_add_word_dialog_box);
ok = findViewById(R.id.dictDialog_confirmButton);
cancel = (Button) findViewById(R.id.dictDialog_cancelButton);
ok.setOnClickListener(this);
cancel.setOnClickListener(this);
}
This displays when tapping the Floating Action Button, via:
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this);
customDialog.show();
refreshRecyclerView();
}
});
I'd like the refreshRecyclerView(); to run only once the user has pressed the OK button on the dialog box. How would I go about doing this?
In addition, how would I go about running it only if the user has pressed OK, and not Cancel?
Create a runnable with your refreshRecyclerView method in it:
Runnable r = new Runnable() {
#Override
public void run() {
refreshRecyclerView();
}
}
then create a handler for that runnable:
Handler handler = new Handler();
inside your onClickListener for the ok button trigger the runnable by calling the following:
handler.post(r);
Why not add a listener to your custom dialog?
var listener: Listener
public interface Listener {
void onPositiveActionPressed();
}
Then in your custom dialog's ok.onClickListener you'd have the below;
if(listener != null) {
listener.onPositiveActionPressed();
}
Finally;
DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this)
customDialog.listener = self
Of course having implemented DictCustomDialogBoxClass.Listener
In my testing code app I have a problem with setting if statement with getVisibility in it.
Basicaly, app should reveal (from GONE to VISIBLE) 1 TextView tv1, and then reveal 1 LinearLayout LL1 with 2 Buttons inside it btn1 & btn2. When either is pressed, LL1 should disappear (from VISIBLE back to GONE) and in its place, new tv2 or tv3 should appear (from GONE to VISIBLE) depending on which button is pressed. tv4 should wait until LL1 is GONE, and then appear. A 1s interval is set between appearances.
Everything runs ok, until IF part which doesnt work. I have also tried
if (btn1.isPressed() || btn2.isPressed())
and
if (tv2.getVisibility()==View.VISIBLE || tv3.getVisibility()==View.VISIBLE)
but it didnt work. I guess the problem is the way I acces getVisibility.
I even tryed defining global boolean x=false;, and then in OnClickListener setting x=true; but then if (x) {... returns false!?
HOW TO MAKE IF STATEMENT WORK!?
Everything (tv1..tv4, btn1, btn2, LL1) in layout file is predefined and inittialy set to GONE.
This is my activity>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
Thread mainThread = new Thread(
new Runnable() {
#Override
public void run() {
startApp();
}
}
);
mainThread.start();
}
public void startApp() {
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
TextView tv1 = (TextView) findViewById(R.id.line1);
TextView tv2 = (TextView) findViewById(R.id.line2);
TextView tv3 = (TextView) findViewById(R.id.line3);
TextView tv4 = (TextView) findViewById(R.id.line4);
LinearLayout LL1 = (LinearLayout) findViewById(R.id.LL1);
show_line(tv1, 1000);
btnLL(LL1, btn1, btn2, tv2, tv3);
//THIS IS THE PROBLEM IT ALWAYS RETURNS TRUE
if (LL1.getVisibility()==View.GONE) show_line(tv4, 1000);
}
public void show_line (final TextView tv, int duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
tv.getHandler().post(new Runnable() {
public void run() {
tv.setVisibility(View.VISIBLE);
}
});
/*tv.setVisibility(View.VISIBLE); CANT USE THIS BECAUSE IM NOT IN UI THREAD*/
}
public void btnLL (final LinearLayout LL, Button btnLeft, Button btnRight, final TextView tvLeft, final TextView tvRight) {
//LL.setVisibility(View.VISIBLE); AGAIN NOT IN UI THREAD
LL.getHandler().post(new Runnable() {
public void run() {
LL.setVisibility(View.VISIBLE);
}
});
btnLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LL.getHandler().post(new Runnable() {
public void run() {
LL.setVisibility(View.GONE);
}
});
tvLeft.getHandler().post(new Runnable() {
public void run() {
tvLeft.setVisibility(View.VISIBLE);
}
});
}
});
btnRight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LL.getHandler().post(new Runnable() {
public void run() {
LL.setVisibility(View.GONE);
}
});
tvRight.getHandler().post(new Runnable() {
public void run() {
tvRight.setVisibility(View.VISIBLE);
}
});
}
});
}
//THIS IS THE PROBLEM IT ALWAYS RETURNS TRUE
if (LL1.getVisibility()==View.GONE) show_line(tv4, 1000);
most likely because it has not been rendered yet, thus not visible yet.
You will need to place that check in something like this:
LL1.getHandler().post(new Runnable() {
public void run() {
if (LL1.getVisibility()==View.GONE) show_line(tv4, 1000);
}
});
Which will wait for LL1 to be rendered/visible, before checking for getVisibility().
note: like GVillani82 said, you are unnecessarily using threads and posts. You don't really need Thread mainThread = new Thread() (and along with those view.getHandler().post()), only some view.post().
OK, just as I wrote a comment with my "primitive" solution, I realized the problem, tested it and confirmed :) Why that IF didn't work...
execution steps:
1) show_line() gets from GONE to VISIBLE
2) btnLL() gets from GONE to VISIBLE and sets up 2 buttons with their onClick()
3) this is the important part
this is where the user should interact with app and make a choice so onClick could execute and then 4) IF statement should execute.
What actually happened is that IF statement executed before user interaction so, the LL1 was still VISIBLE, therefore if (LL1.getVisible()==View.GONE) returned false, and by the time user presses the button and LL1 switches to GONE, IF is already executed and doesn't check any more.
I have a Handler in my Loading Activity that delays my Intent to next Activity (which works perfectly).
What I want to do is, after the delay ends I want to have an on Click Listener that covers all the screen, but it's not working!
I've tried public void OnClick and View.onClickListener none of them work inside the Handler
How can i fix this?
My Handler code:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
overridePendingTransition(R.anim.animin, R.anim.animout);
final Intent mainIntent = new Intent(LoadingActivity.this, StartActivity.class);
LoadingActivity.this.startActivity(mainIntent);
LoadingActivity.this.finish();
}
}, 6000);
}
That what you want is to start the activity in the click of the screen but just after th delay??
Then you should set the listener in the handler :
setContentView(R.layout.mylayout);
final View mScreen = findViewById(R.id.whole_layout);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
overridePendingTransition(R.anim.animin, R.anim.animout);
final Intent mainIntent = new Intent(LoadingActivity.this, StartActivity.class);
LoadingActivity.this.startActivity(mainIntent);
LoadingActivity.this.finish();
}
});
}
}, 6000);
mylayout.xml:
<LinearLayout
android:id="#+id/whole_layout"
android:clickeable="true"
...>
<!-- your content -->
</LinearLayout>
You can use the layout's type that you wish
Create an empty view that matches the layout of the screen and then setup logic to show/hide the view when appropiate?
In my Activity I have one ImageView
XML Code:
<!-- FOTO LATERAL! -->
<ImageView
android:id="#+id/simbolo_raca"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="#+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok. Seguinte Pergunta"
android:layout_gravity="right"
android:gravity="right"
android:paddingRight="30dip">
</LinearLayout>
This is the Activity:
public class QuestionarioActivityRaca extends Activity{
ImageView simbolo;
int position;
Button B_Save;
List<Drawable> List_imagens = new ArrayList<Drawable>();
#Override
public void onCreate(Bundle savedInstanceState) {
simbolo = (ImageView) findViewById(R.id.simbolo_raca);
B_Save = (Button) findViewById(R.id.button_save);
position = 0;
List_imagens.add(getResources().getDrawable(R.drawable.p1));
List_imagens.add(getResources().getDrawable(R.drawable.p2));
List_imagens.add(getResources().getDrawable(R.drawable.p3));
List_imagens.add(getResources().getDrawable(R.drawable.p4));
loadNewImage(position);
B_Save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loadNewImage(position);
position++;
}
});
}
// I Use this method to load the Image
public loadNewImage(int Page)
{
new Thread(new Runnable(){
#Override
public void run(){
simbolo.post(new Runnable(){
#Override
public void run()
{
simbolo.setImageDrawable(List_imagens.get(Page));
}
});
}
}).start();
}
The first time I call this method, (position = 0) the image doesnt loads. After that, the image is loading correctly.
How I have to do to load the image the first time?
(I can not load the image in the XML using android:src="#drawable/x") because the image could be different anytime.
EDITED!
There are a number of issues with the code sample you posted.
like writing int with a capital I.
Int position;
Not sending in a parameter with your method in the onClick:
public void onClick(View v) {
loadNewImage();
}
And several more in both your XML and code.
Is there a spesific reason you want to run a new thread every time for this task?
If you really need a thread, you have to declare your int as final in the method.
However, you should get your desired result with the code-sample below.
You have to modify your onClick to send the appropriate int for whatever drawable you want to use.
Good luck.
public class testactivity extends ActionBarActivity {
ImageView simbolo;
int position;
Button B_Save;
List<Drawable> List_imagens = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testactivity);
simbolo = (ImageView) findViewById(R.id.simbolo_raca);
B_Save = (Button) findViewById(R.id.button_save);
position = 0;
List_imagens.add(getResources().getDrawable(R.drawable.ic_drawer));
List_imagens.add(getResources().getDrawable(R.drawable.ic_check));
List_imagens.add(getResources().getDrawable(R.drawable.ic_action_add_package));
List_imagens.add(getResources().getDrawable(R.drawable.ic_action_exception));
loadNewImage(position);
final Random rand = new Random();
B_Save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loadNewImage(rand.nextInt(4));
}
});
}
public void loadNewImage(final int page)
{
simbolo.setImageDrawable(List_imagens.get(page));
}
}
first of all before the button gets clicked you call this function to load the initial image with loadNewImage(position); when the button is clicked you call this function loadNewImage(); i am guessing it works when the button is clicked because this loadNewImage(); method is ok and loadNewImage(int page); is not ok, because they are two different function.
Solving your problem if you want an integer object use Integer position or else go with int position and please let go of the thread. now your code will work