I have a trouble creating my app in Android Studio, I explain myself
I created a layout for my toast, the XML looks like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/elemento_correcto_s" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:background="#afff45"
android:weightSum="1">
<ImageView android:layout_height="100dp" android:layout_width="100dp" android:src="#drawable/base" android:padding="5dip" android:id="#+id/ok"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false">
</ImageView>
<TextView android:layout_height="50dp" android:id="#+id/tv" android:layout_width="match_parent" android:text="¡CORRECTO!" android:textColor="#FFF" android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical"
android:layout_toRightOf="#+id/ok"
android:layout_marginTop="26dp"
>
</TextView>
It works fine when I inflate the layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(
R.layout.elemento_correcto_s
, (ViewGroup) findViewById(R.id.elemento_correcto_s));
this.elementoCorrecto = new Toast(this);
this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0);
this.elementoCorrecto.setDuration(Toast.LENGTH_LONG);
this.elementoCorrecto.setView(layout);
this.elementoCorrecto.show();
But the problem is that I want to change dynamically the text for the TextView, I already tried just calling the TextView and changing the text, but it doesn't work, so I hope you can help me
This is my code
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(
R.layout.elemento_correcto_xl
, (ViewGroup) findViewById(R.id.elemento_correcto_xl));
TextView tvCombo = (TextView) findViewById(R.id.tv);
if(combo > 1) {
tvCombo.setText("¡" + combo + " VECES SEGUIDAS!");
}
else
tvCombo.setText("¡CORRECTO!");
this.elementoCorrecto = new Toast(this);
this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0);
this.elementoCorrecto.setDuration(Toast.LENGTH_LONG);
this.elementoCorrecto.setView(layout);
this.elementoCorrecto.show();
You can use this method to as i passing the string to show..
public static void makeToast(Context c, String msgToShow){
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v= inflater.inflate(R.layout.view_toast,null); //your layout to show
TextView text = (TextView) v.findViewById(R.id.textViewToast);
text.setText(msgToShow);
Toast toast = new Toast(c);
toast.setGravity(Gravity.BOTTOM, 10, 25);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(v);
toast.show();
}
Call it wherever you want in the Activities.
Is there any special reason to show a toast using TextView ? Otherwise you can follow below method to show toast dynamically -
String toastText = "";
if(combo > 1) {
toastText = "¡" + combo + " VECES SEGUIDAS!";
}
else
toastText = "¡CORRECTO!";
Toast.makeText(activity, toastText, Toast.LENGTH_SHORT).show();
Related
Is there a way to check if there is a toast message being displayed in android?
I am writing an idlingResource and I want to make sure no toast is currently being displayed before I return the resource as being idle.
Create Custom toast.
private Toast myToast = nuul; // Create class Level Object to
Add this code in the method
String message = "Your Message";
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
the (ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = (TextView) layout.findViewById(R.id.custom_toast_text);
text.setText(Message);
myToast = new Toast(getApplicationContext());
myToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
myToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
myToast.setDuration(Toast.LENGTH_LONG);
myToast.setView(layout);
myToast.show();
custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#80000000">
<TextView
android:id="#+id/custom_toast_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:enabled="true"
android:textSize="16dp"
/>
</LinearLayout>
Dismiss the Toast, if it is not null
if (myToast != null)
myToast.cancel(); // Dismiss the toast
I am using an AlertDialog created using android.suport.v7.app.AlertDialog and I am having a custom layout for the Dialog with a few EditTexts and a few TextViews.
The TextViews that are used are initially having their visibility set to GONE.
I am using the TextViews mainly to prompt the user for incorrect inputs in the EditText fields.
So when I try to toggle the visibility of the TextView to VISIBLE for showing an error message, the visibility does not change.
My XML File for layoutS is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/contactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="#string/sampark_naam"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp">
<requestFocus />
</EditText>
<TextView
android:id="#+id/noName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/no_name_error"
android:textColor="#android:color/holo_red_dark"
android:layout_marginEnd="5dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="5dp"
android:visibility="gone"/>
</LinearLayout>
The AlertDialog used is:
final Context context = view.getContext();
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View aDrishya = layoutInflater.inflate(R.layout.layoutS, null, false);
vNaam = (EditText) aDrishya.findViewById(R.id.contactName);
nameError = (TextView) aDrishya.findViewById(R.id.noName);
sSanddok = new AlertDialog.Builder(context)
.setView(aDrishya)
.setTitle("Add A New Contact")
.setPositiveButton("Add +", new PositiveButton(context))
.setNegativeButton("Pick", new null);
sSanddok.show();
In the positive button I check for the contactName entered as:
if(vNaam.getText().toString().equals("")) {
Toast.makeText(this, "No Name", Toast.LENGTH_SHORT).show();
nameError.setVisibility(View.VISIBLE);
} else {
nameError.setVisibility(View.GONE);
}
It is showing the Toast but it's not showing the TextView.
Any idea what's wrong here and how to fix it?
Hi can you try to change and run this code, See if it works..
Alert dialog changes :
sSanddok = new AlertDialog.Builder(context)
.setView(R.layout.layoutS)
.setTitle("Add A New Contact")
.setPositiveButton("Add +", null)
.setNegativeButton("Pick",null);
sSanddok.show();
sSanddok.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//if edit text value is empty do this
TextView tv = (TextView)sSanddok.findViewById(R.id.noName);
tv.setVisibility(View.VISIBLE);
}
});
TextView visibility in xml is set to gone..
Hi this is working for me...Try this code pls
I have seen sometimes a Blue toast showing up when i connect my Samsung phone with wifi network. Can anyone help for customising the colour of the toast.
For example:
Try This:-
SpannableString text = new SpannableString("Please Wait !!!!! ");
text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 41, 0);
Toast.makeText(c.getApplicationContext(),text , Toast.LENGTH_LONG).show();
Another Way:-
Make an xml / customToast.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#DAAA"
>
<ImageView android:src="#drawable/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
</LinearLayout>
In Activity:-
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.customToast,
(ViewGroup) findViewById(R.id.toast_layout));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Like this way:
LayoutInflater inflater = youractivity.this.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
null);
TextView text = (TextView) layout.findViewById(R.id.tvtoast);
text.setText("No Internet Connection");
text.setTextColor(Color.BLACK);
Toast toast = new Toast(getActivity());
toast.setView(layout);
toast.setDuration(100);
toast.show();
Make your own layout custom_toast.xml and set Color to TextView Text as per your need
Output:
You can create a custom Toast view to suit your requirements. See the section titled "Creating a Custom Toast View" at http://developer.android.com/guide/topics/ui/notifiers/toasts.html
I have an app that retrieves data from a webservice in the form of an array. In the getView() of the adapter i set the TextViews to the elements in the array. The array is called recordItem and has various elements one of which is "status". Status is a String that can be either completed, ncr, or waiting. if i set the TextView in the listview directly with the value completed, ncr or waiting there's no problem.
I don't want to display the full string in the textview but rather display a C, NCR or W instead. I have a series of "if statements" that check the array element and then sets the textview to the respective character.
The problem is that no character is being displayed. why?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rotarowlayout, parent,
false);
TextView startTime = (TextView) rowView
.findViewById(R.id.rowstarttime);
TextView duration = (TextView) rowView
.findViewById(R.id.rowduration);
TextView status = (TextView) rowView.findViewById(R.id.rowstatus);
TextView name = (TextView) rowView.findViewById(R.id.rowclientname);
String record = list.get(position).toString();
String[] itemsInRecord = record.split(",");
Log.e(TAG, "itemin record = " + itemsInRecord.length);
String[] recordItem = new String[itemsInRecord.length];
for (int x = 0; x < itemsInRecord.length; x++) {
recordItem[x] = itemsInRecord[x];
Log.e(TAG, "x = " + x);
}
String withoutBraket = recordItem[0].substring(11);
String withoutSecs = withoutBraket.substring(0, 6);
Log.e(TAG, "recordItem = " + recordItem[2]);
if(recordItem[2].toString().equalsIgnoreCase("Completed")){
statusField = "c";
}else if(recordItem[2].toString().equalsIgnoreCase("NCR")){
statusField = "NCR";
}else if(recordItem[2].toString().equalsIgnoreCase("Waiting")){
statusField = "W";
}
Log.e(TAG, "statusField = " + statusField);
startTime.setText(withoutSecs );
duration.setText( recordItem[1]);
status.setText( statusField);
name.setText( recordItem[3] + recordItem[4]);
callID = recordItem[5];
needName = recordItem[6];
return rowView;
}
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp" >
<TextView
android:id="#+id/rowstarttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/rowduration"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp" >
<TextView
android:id="#+id/rowstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/rowclientname"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
recordItem[2] needed trim() calling on it. .equals not picking the strings up because of this.
I'm trying to center two textViews that are in a LinearLayout. This LinearLayout is nested in another one, with a ListView-element.
I think my XML is pretty correct. I fill my textViews dynamically in my Adapterclass.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView android:layout_height="wrap_content"
android:id="#+id/atlVacaturesnummer"
android:layout_width="wrap_content"
android:textColor="#color/Accent"
android:text="x"
/>
<TextView android:layout_height="wrap_content"
android:id="#+id/atlVacatures"
android:layout_width="wrap_content"
android:text="y"
/>
</LinearLayout>
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView android:layout_height="fill_parent"
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:text="Er zijn geen jobs die voldoen aan uw criteria..."
android:gravity="center"/>
</LinearLayout>
Adapterclass:
/*
* Klasse VacatureAdapter
*/
private class VacatureAdapter extends ArrayAdapter<Vacature>{
private ArrayList<Vacature> vacatures;
public VacatureAdapter(Context context, int textViewResourceId, ArrayList<Vacature> vacatures){
super(context, textViewResourceId, vacatures);
this.vacatures = getArray();
//System.out.println("Array vacatureadapter: " + v);
}
#Override
public View getView(int position, View convertview, ViewGroup parent){
View view = convertview;
if(view==null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.vacature_list_item, null);
//view.setBackgroundColor((position % 2) == 1? Color.LTGRAY: Color.WHITE);
}
TextView atlVacatures = (TextView)findViewById(R.id.atlVacatures);
TextView atlVacaturesnr = (TextView)findViewById(R.id.atlVacaturesnummer);
atlVacaturesnr.setText("" + arrVacatures.size());
atlVacatures.setText(" jobs op maat gevonden!");
Vacature vaca = vacatures.get(position);
if(vaca != null){
TextView tvNaam = (TextView) view.findViewById(R.id.vacatureNaam);
TextView tvWerkveld = (TextView) view.findViewById(R.id.vacatureWerkveld);
TextView tvRegio = (TextView) view.findViewById(R.id.vacatureRegio);
if(tvNaam != null){
tvNaam.setText(vaca.getTitel());
if(tvWerkveld != null){
tvWerkveld.setText("Werkveld: " + vaca.getWerkveld());
if(tvRegio!=null){
tvRegio.setText("Regio: "+vaca.getRegio());
}
}
}
}
return view;
}
}
The weird thing is that if my spinner runs, he shows the texts set in my XML correctly, if the spinner stops and fills my ListView it only shows one digit of my number and when I scroll once he shows my number completly plus the second TextView. I don't quite understand what's wrong, maybe some code needs te be put somewhere else?
I've solved my problem by putting my setText code in my Runnable method, after I dismiss my Dialog. Now It looks like this:
private Runnable returnRes = new Runnable(){
public void run(){
if (arrVacatures.size() == 0){
dialog.dismiss();
}
if(arrVacatures!=null && arrVacatures.size() > 0){
adapter.notifyDataSetChanged();
for(int i= 0; i< arrVacatures.size();i++){
adapter.add(arrVacatures.get(i));
}
dialog.dismiss();
TextView atlVacatures = (TextView)findViewById(R.id.atlVacatures);
TextView atlVacaturesnr = (TextView)findViewById(R.id.atlVacaturesnummer);
atlVacaturesnr.setText("" + arrVacatures.size());
atlVacatures.setText(" jobs op maat gevonden!");
adapter.notifyDataSetChanged();
}
}
};