I am inherited "AppCompatActivity" activity in my custom activity as shown in code below :
public class MyHomeActivity : AppCompatActivity
{
Toast.MakeText(this, "Email & Message successfully sent at SOS contacts.", ToastLength.Long).Show();
}
The toast message is not showing well in this activity as shown in the picture below.. please help if anybody have any solution for it....
Create a custom view for your Toast with a TextView that support multiple lines, this will create a custom view Toast with text that fits in it.
View view = LayoutInflater.Inflate(Resource.Layout.custom_toast, null);
var txt = view.FindViewById<TextView>(Resource.Id.txtCustomToast);
txt.Text = "your toast message";
var toast = new Toast(this)
{
Duration = ToastLength.Short,
View = view
};
toast.Show();
Also inserting a new line character (i.e \n) into the toast message in your current code, will show the toast message in two lines and also the background will be proper.
Related
I trying to make a custom toast with the background "Orange color", however, in the attempt I am able to get it but the field also becomes white and the text is not visible, I have attached the image
You can see that the text is not visible.
here is my code:
String g= "+";
Toast toast = Toast.makeText(getActivity(), "Click and hold on '"+g+"' icon",
Toast.LENGTH_SHORT);
View view= toast.getView();
view.setBackgroundColor(Color.parseColor("#FF791B"));
View t = toast.getView().findViewById(android.R.id.message);
t.setBackgroundColor(Color.parseColor("#FFFFFF"));
toast.show();
I just want the text to be in white.
Is something I am missing in the code, I am not able to get it right.
Also, is there a way to make the "+" sign bold g
Just Change this line
t.setBackgroundColor(Color.parseColor("#FFFFFF"));
To
t.setTextColor(Color.parseColor("#FFFFFF"));
I have mode some changes here:
TextView t = toast.getView().findViewById(android.R.id.message);
t.setTextColor(Color.WHITE);
I have saved it as textView and I was able to get the setTextColor method and I was able to change the color
Try this - customttoast.xml is your custom toast xml and custom_toast_layout can be your activity layout xml
//Creating the LayoutInflater instance
LayoutInflater li = getLayoutInflater();
//Getting the View object as defined in the customtoast.xml file
View layout = li.inflate(R.layout.customtoast,(ViewGroup) findViewById(R.id.custom_toast_layout));
//Creating the Toast object
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);//setting the view of custom toast layout
toast.show();
My question, in particular, is in reference to the refreshing of toasts that have already been created.
For example i have a toast that has been created:
Toast myToast;
// ...
protected void onCreate(Bundle savedInstanceState) {
// ...
myToast = Toast.makeText(getApplicationContext(), "Welcome", Toast.LENGTH_SHORT);
And i update it differently upon two actions:
if (answerGiven != correctAnswer) {
myToast.setText("Wrong");
View view = myToast.getView();
TextView text = (TextView) view.findViewById(android.R.id.message);
text.setTextColor(Color.RED);
text.setTextSize(32);
myToast.setGravity(Gravity.CENTER, 0, 0);
} else {
View view = myToast.getView();
TextView text = (TextView) view.findViewById(android.R.id.message);
text.setTextColor(Color.BLACK);
text.setTextSize(32);
myToast.setText("+" + bonus);
myToast.setGravity(Gravity.TOP|Gravity.RIGHT, 20, 20);
}
myToast.show();
Both actions work fine! BUT if i tap quickly on the screen and activate the second action BEFORE the toast has disappeared the gravity is not changed even though the message updates.
For example: if i choose the wrong answer, and the "wrong" message shows in the middle of the screen:
and then i immediately choose the write answer the bonus message appears in the middle of the screen (even though i tell it to set setGravity(Gravity.TOP|Gravity.RIGHT, 20, 20);)
(note: if i wait until the "Wrong" message disappears and then click the correct answer the bonus message shows in the top left corner as expected)
What i want it to do is to always put the bonus message at the top left. Even if the user clicks to fast. So i am asking: Is there a way that the previous toast can be cancelled so the new message is shown in the correct location. Or is there something i should do (such as refresh my activity) so that the message displays in the correct location.
I have written a below sample code and that's working.Please have a look at that
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnAddData;
private TextView tv;
private EditText et;
private boolean check;
Toast myToast;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myToast = Toast.makeText(getApplicationContext(), "Welcome", Toast.LENGTH_SHORT);
btnAddData = findViewById(R.id.btn_add_record);
btnAddData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (check) {
if (myToast.getView().isShown())
myToast.cancel();
check = false;
myToast = Toast.makeText(getApplicationContext(), "Welcome", Toast.LENGTH_SHORT);
myToast.setGravity(Gravity.CENTER, 0, 0);
myToast.show();
} else {
if (myToast.getView().isShown())
myToast.cancel();
check = true;
myToast = Toast.makeText(getApplicationContext(), "Welcome", Toast.LENGTH_SHORT);
myToast.setGravity(Gravity.TOP | Gravity.RIGHT, 20, 20);
myToast.show();
}
}
});
}
You need to make a button in xml and test that and let me know if you are facing any problem. Further you can adjust according to your need.
You should cancel the current toast before showing new toast message.
if (myToast != null) {
myToast.cancel();
}
// your code ...
You should Use
Toast.cancel()
Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally have to call this. Normally view will disappear on its own after the appropriate duration.
Try this
if (myToast!= null) {
myToast.cancel();
}
if (answerGiven != correctAnswer) {
myToast.setText("Wrong");
myToast.setGravity(Gravity.CENTER, 0, 0);
} else {
myToast.setText("+" + bonus);
myToast.setGravity(Gravity.TOP|Gravity.RIGHT, 20, 20);
}
Through a webservice I get an Error message containing a link (e.g.
Click <a href='blablabla'>here</a>
). I use fromHtml to turn it into a spanned and then show it in a custom Toast.
Inside the toast the text is shown and the 'here' is underlined, just like a link. However when I click it, it doesn't do anything.
How should I solve this? Is there any way to deduct the link from the xml (e.g.
<ERROR>Click <a href='blabla'>here</a></ERROR>
) so I can create a button to push
which opens the link in a Webview?
Here's the relevant code:
My main activity
CommonCode.showToast(error, mContext, mViewGroup, true);
CommonCode
public static void showToast(Spanned toastString, Context context, View view, Boolean isLink) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) view);
TextView text = (TextView) layout.findViewById(R.id.toastText);
if(isLink == true) {
text.setMovementMethod(LinkMovementMethod.getInstance());
}
text.setText(toastString);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
Toasts were designed to place a small amount of information in front of the user for a short time. If you need buttons pressed try a dialog instead of a toast.
I'm having some trouble with the logic behind storing an ImageView into an ArrayList.
The application I'm developing keeps track of player statuses in a game. The user first adds Player objects (which keeps track of a status string and a status image to go with it) to an ArrayList (to keep track of them all). Then, after submitting all of the players, a screen pops up inflating a TableRow for each player, containing a button (to view the Player's profile), an ImageView (an icon representing the status), and a TextView (containing the player's status string value).
I don't have a problem with the buttons and loading each player's profile. The problem occurs with loading the "select status" GUI from dialog_select_icon.xml, particularly the ImageView ArrayList. I get a NullPointerException, which doesn't make sense to me because I'm doing it essentially the same way as I did the buttons.
//this code runs when user clicks a player's status icon
public void playerStatusIconClicked(View v)
{
//loop through buttons to determine which player's button was clicked
for (int i = 0; i < playerList.size(); i++)
{
if (v.getId() == playerStatusIVList.get(i).getId())
{
calledPlayer = i; //instance variable
loadStatusIconGUI();
}//if
}//for
}//method playerStatusIconClicked
//showStatusIconGUI inflates the "select status icon" GUI
//and handles the user selecting an icon
private void loadStatusIconGUI()
{
//inflate the GUI for the showStatusIcon dialog (inflater is an instance variable)
View view = inflater.inflate(R.layout.dialog_select_icon, null);
//if the list has something in it, start from fresh
if (!selectStatusIVList.isEmpty())
{
selectStatusIVList.clear();
}
//list of icons in the "select status icon" dialog
selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV0));
selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV1));
selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV2));
selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV3));
selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV4));
selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV5));
selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV6));
//create a dialog so user can select an icon
AlertDialog.Builder selectIconDialog = new AlertDialog.Builder(this);
selectIconDialog.setView(view); //set the Dialog's custom view
selectIconDialog.setTitle(R.string.title_select_icon);
selectIconDialog.setNegativeButton(R.string.close, null);
selectIconDialog.show();
}//showStatusIconGUI
//Handle clicks in the "select status icon" dialog
//Assigns a new status to the player
public void statusIconClicked(View v)
{
Toast message;
for (int i = 0; i < selectStatusIVList.size(); i++)
{
if (v.getId() == selectStatusIVList.get(i).getId())
{
message = Toast.makeText(
MafiaTracker.this, "new status: " statusID[i], Toast.LENGTH_SHORT);
message.show();
playerList.get(calledPlayer).setImage(imageID[i]);
playerList.get(calledPlayer).setStatus(statusID[i]);
}
}
updateViewPlayerGUI();
}
Note that imageID[i] and statusID[i] are referring to int arrays containing the IDs for each status string and status image.
I can post the xml file but since it's 124 lines long I'd prefer not to. Just know that each ImageView in the xml file DOES have an ID, so I can't figure out why I'm getting these NullPointerExceptions, starting with the "if (!selectStatusIVList.isEmpty())" part, and continuing on with every other call after.
Please help!
statusIconGUI seems to be the main layout xml you used in setContenView().
Consider the line :
selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV0));
you are using findViewbyID on statusIconGUI. Do that instead on the view instance of R.layout.dialog_select_icon which you inflated.
so, change the above line to :
selectStatusIVList.add((ImageView) view.findViewById(R.id.statusIV0));
Initially selectStatusIVList is null. In loadStatusIconGUI check it for null
if(selectStatusIVList != null){
if (!selectStatusIVList.isEmpty())
{
selectStatusIVList.clear();
}
}else{
selectStatusIVList = new ArrrayList<Integer>();
}
I am trying to get a message to appear when a button is clicked to tell the user to fill in the blank field. Currently, if the field is blank, it crashes/force closes the app. I tried to do the following code and had zero success. Originally I didn't have the if/else in there, I just ran the calculator(); method and the following imm code.
Could someone point me into the right direction?
public void onClick(View v)
{
if ((EditText)findViewById(R.id.amount1)== null)
{
Context context = getApplicationContext();
CharSequence text = "Enter a number";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
{
calculator();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
Im pretty sure this is the bad code:
if ((EditText)findViewById(R.id.amount1)== null)
Just dont know how to word it the way I want.
Try checking the length of the text in the EditText widget
EditText e = (EditText)findViewById(R.id.amount1));
if(e.getText().length == 0){
//Show Toast
}else{
//continue your code
}
Use this code.
EditText et = (EditText)findViewById(R.id.amount1));
if(et1.getText().length() == 0){
//Display toast here
} else{
//Your code
}
EditText text = (EditText)findViewById(R.id.amount1);
if(TextUtils.isEmpty(text.toString())) {
// show toast
}
Even if the field is blank, the edittext is not null. Use:
EditText editText = (EditText)findViewById(R.id.amount1);
String text = new String(editText.getText());
if (test.equals("")) {
//...
((EditText)findViewById(R.id.amount1)== null is just getting a reference to the EditText with the id amount1, it is not checking to see if that EditText has a valid entry.
To see if the EditText has text, you can get the String it holds by via EditText#getText().toString()
To make this work, first store the reference to the EditText in a var, then perform your checks on the String:
EditText et = (EditText)findViewById(R.id.amount1);
String amount1 = et.getText().toString();
if (amount1.equals("")) {
// Do your stuff here
}
I'm using local variables and just assuming you want the string to have content. You will likely need to do other checks to handle all the cases (like malformed input). Some of this you can reduce by setting the inputType on the EditText. For example, you might set it to numberDecimal if you are trying to handle only decimal numbers.
You actually want to check if the contents of the EditText are null or an empty string.
The line in question should look something like this:
if("".equals(((EditText)findViewById(R.id.amount1)).getText().toString()))
Of course you may want to break that statement up into more lines to make it a bit more readable!