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?
Related
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
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 .
The doc says I should add something like this to my code:
// create a new layout
LinearLayout layout = new LinearLayout(this);
/*
Add any elements to your view
*/
// make the view visible
this.setContentView(layout);
final Activity act = this;
// now add the banner or overlay to the app
layout.post(new Runnable() { //what about this line?
public void run() {
myController = new AdController(act, MY_LB_SECTION_ID);
myController.loadAd();
}
});
My layout is in xml and I have already defined setContentView(R.layout.config); I mean the layout.post(new Runnable() { line. How should I modify the code?
For anyone who is interested I have found the obvious solution:
RelativeLayout rellaymain = (RelativeLayout)findViewById(R.id.rellaymain);
final Activity act = this;
rellaymain.post(new Runnable() {
public void run() {
myController = new AdController(act, MY_LB_SECTION_ID2);
myController.setAsynchTask(true);
myController.loadAd();
}
//});
});
rellaymain is the layout of the .xml file.
I am trying to add a textview inside a tab dynamically. using this code
Oncreate()
{
OA.loaderShow(this); //Loader display
new Thread(new Runnable(){
public void run()
{
Looper.prepare();
fetchDocs();
OA.loaderHide(); //Loader Hide
Looper.loop();
}
}).start();
}
fetchDocs()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
TextView text = new TextView(this);
text.setText(mytext);
layout.addView(text);
}
I am getting this error "Only the original thread that created a view hierarchy can touch its view".
Please Help.
Put above inside the following block
runOnUiThread(new Runnable() {#Overridepublic void run() {//your code here}}
Try using a handler like this:
EDIT:
protected static final int SET_TEXTVIEW = 0;
Oncreate()
{
OA.loaderShow(this); //Loader display
new Thread(new Runnable(){
public void run()
{
Looper.prepare();
handler.sendMessage(handler.obtainMessage(SET_TEXTVIEW));
OA.loaderHide(); //Loader Hide
Looper.loop();
}
}).start();
}
public Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SET_TEXTVIEW :
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
TextView text = new TextView(this);
text.setText(mytext);
layout.addView(text);
}
}
};
Add this in the onCreate method rather than adding from else where.
I'm new to android and Java. I want to make an onClick method which carries an int argument, so this is my attempt:
public void randomClick(final int randomIndex)
{
private OnClickListener top_listener = new OnClickListener() {
public void onClick(View v) {
Intent top = new Intent(Main.this, ProjectDetail.class);
top.putExtra("spendino.de.ProjectDetail.position", randomIndex);
startActivity(top);
}
};
}
but it still contains error, can anybody fix that for me?
Later I want set the method to an ImageView, it will look more or less like this image1.randomClick(randomIndex1);.
Currently in your implementation the OnClickListener is not bounded to any view, so it won't get fired.
You should create your own (might be inner but not anonymous) class implementing the OnClickListener interface:
public class RandomClickListener implements View.OnClickListener
{
private final int randomIndex;
public RandomClickListener(final int randomIndex)
{
this.randomIndex = randomIndex;
}
#Override
public void onClick(View v)
{
Intent top = new Intent(Main.this, ProjectDetail.class);
top.putExtra("spendino.de.ProjectDetail.position", randomIndex);
startActivity(top);
}
}
[...]
image1.setOnClickListener(new RandomClickListener(randomIndex));
This way when you click on the image1 it will start the ProjectDetail activity with the randomIndex set above.
If you'd like to explicitly start the ProjectDetails activity (without any user interactions such as a click), you don't need an OnClickListener at all.