Creating a toast in android and specifying the location - android

I am working on my first Android app and I am trying to create a toast and specify the location where I want it to appear. When I was using the code that is now commented (it didn't customize the toast's location) the app ran perfectly on the emulator, but now that I am trying to customize the location and using the code below the comments, the app stops running when I click in the UI on the button meant to show the toast.
Do you have any idea of why this is happening? Because I have seen very similar codes (almost the same) presented as correct, so this should be working. I know this is a very simple code and a simple question, but still, I cannot figure out what is wrong with my code.
This is the code on my onButtonClick:
public void onClick(View V) {
// Toast.makeText(QuizActivity.this,
// R.string.correct_toast,
// Toast.LENGTH_SHORT).show();
Toast t = new Toast(QuizActivity.this);
t.setText(R.string.correct_toast);
t.setDuration(Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP|Gravity.LEFT,0,0);
t.show();
}

From https://developer.android.com/reference/android/widget/Toast#Toast(android.content.Context) :
Construct an empty Toast object. You must call setView(View) before you can call show().
Your code crashes because now you are using the Toast() constructor. And you need to setView(View) before show(). While this is not necessary with makeText().
As I understand, makeText() creates a View for you setting the text from the second parameter. But the constructor doesn't, and the setText() method does not either. setText() only update the an existing text:
From https://developer.android.com/reference/android/widget/Toast#setText(int) :
Update the text in a Toast that was previously created using one of the makeText() methods.
Here is an example of how to do that:
Custom toast on Android: a simple example
Edit
In any case, you can create the Toast with your initial code, without calling show().
public void onClick(View V) {
Toast t = Toast.makeText(QuizActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP|Gravity.LEFT,0,0);
t.show();
}
Edit 2
Now you have edited your code, but you are passing the Button View.

Related

how to implement text with reference in Android

I want to create a book in android. It's text come from database and every section of this text has a reference.I need to put clickable image inside text to show reference and when I click on image something like Toast which contain reference list will appear.
I didn't see something like that in typical app. any body have any suggestion to implement it ?
If I understand the question correctly, you will have a clickable image inside the activity. Next add a onClickListener to the image, inside of this display the toast with the reference.
ImageView img = (ImageView) findViewById(R.id.img);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "https://www.google.com/", Toast.LENGTH_SHORT).show();
});
}
If you have multiple clickable images then you might be better off implementing the interface View.OnClickListener and adding the onClick method.
I think the best way is to use Linkify class provided by Android Framework - see http://developer.android.com/reference/android/text/util/Linkify.html for details.
Also you can play with google sample - https://github.com/googlesamples/android-TextLinkify

How do I pass arguments to a Dialog box in Android?

Google decided to make a single-threaded user interface that doesn't have modal dialogs. I'm sure most of you have found that nothing updates until your function returns because everything is event driven on a single thread (by "law").
If I have a simple alert-box, such as "Are You Sure?" (example only), with a Yes and No button, then I have to assign callbacks to the buttons rather than having a simple return value (no modal dialogs). That's fine, even though a return value would vastly simplify my problem (arguments stay local to the caller), although this would stop the calling activity from responding (modal).
Imagine now if I have a list of items and the user attempts to perform some operation. The dialog must now have some way to pass WHICH item I want to perform the operation on to the button's callback, but I can't seem to find any mechanism in the API for passing this along to the onclick handler. Using non-local variables is a work-around, but messy.
How can I pass this information along cleanly? Does anyone have some sort of hack that would somehow "fake" a modal dialog that can return a value (I'm not seeing how).
Create a custom dialog that extends the default android Dialog and add the information you need and pass on the constructor.
See more here: How can I pass values between a Dialog and an Activity?
I am not sure what exactly what do you want to achieve. Not sure if your problems is in the communication between the activity to the dialog or dialog to the activity or both.
Anyway, I have some experience on Android and I really recommend you to achieve the communication between activities, fragments, even dialog (DialogFragments) to use one of these libraries. At the beggining could be a little bit hard to understand how work, but the result is faster and cleaner code, of course offers you more flexibility.
Take a look to:
https://github.com/beworker/tinybus --> less used but it is awesome
https://github.com/greenrobot/EventBus --> more extended and used for the community
Hope to help you!
In a situation like this, I Created a new string array entry in the strings.xml in values folder like this:
<string-array name="array">
<item>1</item>
<item>2</item>
</string-array>
And then create a dialog using Dialog builder like this:
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
LayoutInflater infl=this.getLayoutInflater();
Resources res=getResources();
dialog.setSingleChoiceItems(R.array.alphabets, 0,new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mReturnVariable=which;
}
});
dialog.create().show();
So the mReturnVariable contains the user selected item index .Hope that solves the problem
I passed the required arguments to the Alert Dialog using View Binding in Android Latest version.
private ConnectDialogBinding connectDialogBinding;
private String chargerID;
private void connectDialog() {
// Create the object of
// AlertDialog Builder class
AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this);
connectDialogBinding = ConnectDialogBinding.inflate(getLayoutInflater());
builder.setView(connectDialogBinding.getRoot());
connectDialogBinding.txtID.setText(chargerID);
builder.setCancelable(false);
// Create the Alert dialog
AlertDialog alertDialog = builder.create();
// Show the Alert Dialog box
alertDialog.show();
connectDialogBinding.cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.cancel();
}
});
}
enter image description here

Android Toast Accumulation Testing

Like many others, i've been trying to solve the problem of toast accumulation.
How to prevent Multiple Toast Overlaps
toast issue in android
Cancelling an already open toast in Android
Best way to avoid Toast accumulation in Android
I finally decided to keep track of the current displayed toast and cancel it when another arrives (there's some more logic involved), but i could have use only one toast and change it message. What i want to know is this... Is there a way to TEST this behaviour? Im currently using Robotium and tried different things, but unfortunately the methods to check for toasts (solo.waitForText and solo.searchForText) aren't helping me as i can't make something like
assertTrue(solo.waitForText(text));
//maybe even some sleep here
assertFalse(solo.searchText(text);
Has anyone done something like this? Is there a way to test this using Robotium? using somethig else?
You can use a robotium condition to wait for the text to disappear. Here's a method I use for that.
private void waitForTextToDisappear(final String text, int wait) {
Condition textNotFound = new Condition() {
#Override
public boolean isSatisfied() {
return !solo.searchText(text);
}
};
assertTrue("Text gone: " + text, solo.waitForCondition(textNotFound, wait));
}

android show toast in surfacview onDraw

In my game i use a surfaceview. There i update my game logic and draw on an ondraw loop.
I want to show a toast like "good catch" but it doesn't show. I think its because its in a loop. Even if i put it in an if block that limit it to show once, it doesn't work.
void onDraw()
{
if(isGood)
Toast.makeText((Activity)getContext(), adMessage, Toast.LENGTH_SHORT).show();
}
Thanks
That code should work if isGood is true.
BTW - you dont need to cast with (Activity)

How do I use Toast notification properly on android?

I can't seem to be able to use the toast notification properly. In all of my other apps it worked great but in this one it doesn't. In this app I started using openGL with a framework from a book named "Beginning Android Games" and now I don't seem to be able to use the toast notification. I have no idea what to do... It fails because of the context. How can I make a context that will work? Please help me! this is part of my code because the code is too long:
private void updateReady() {
Coin.number = 0;
if (game.getInput().getTouchEvents().size() > 0) {
state = GAME_RUNNING;
Coin.number = 0;
Num.number = 0;
Toast.makeText(this, "Start!", Toast.LENGTH_SHORT).show();
}
}
When I put the line:
Toast.makeText(this, "Start!", Toast.LENGTH_SHORT).show();
in the class that extends Activity and run it it just doesn't do anything... I tried to make it into a method and call it from other classes but it got a force close...
You can try using getApplicationContext() to get a reference to the current Activity context
You could always make your application context, or the context of whatever activity you're starting your GameScreen from, available statically or by passing it as an argument from whatever creates your instance.
Having said that, though, beware of memory leaks!
Maybe this helps.
I had class defined like this
public class tutorialThree extends Activity implements View.OnClickListener
I tried to use Toast like this
Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();
Did not work, because my class implements that interface "View.OnClickListener" (or whatever it is :))
So toast gets confused with "this" reference, you have to be more precise, so add name of your class before "this" keyword, like this:
Toast.makeText(tutorialThree.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
This solved my problem, now i can see toast.
I assume you're working with this framework called "Beginning Android Games 2".
According to this code, the instance variable you need here is glGame, which is a GLGame object. It extends Activity, so you can just do this:
Toast.makeText(glGame, "Start!", Toast.LENGTH_SHORT).show();

Categories

Resources