Tuesday, November 18, 2014

Android Toast: Custom font or other things

Two things to do: Define a simple small layout for the custom toast and a simple startup sequence:

Toast background:
 <?xml version="1.0" encoding="utf-8"?>  
 <selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item>  
     <shape>  
       <solid android:color="@color/toast_back" />  
       <corners android:radius="3dp" />  
       <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />  
     </shape>  
   </item>  
 </selector>  

Toast layout:
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical" android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:id="@+id/Toast_Root">  
   
   <TextView  
     android:padding="10dp"  
     android:id="@+id/Toast_Message"  
     android:background="@drawable/toast_back"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content" />  
 </LinearLayout>  

Sample startup sequence in a static method:
 public class Toast {  
   static private Activity mCurrentActivity;  
   static public void updateCurrentActivity(Activity act) {  
     mCurrentActivity = act;  
   }  
   
   static public void Show(int id) {  
     LayoutInflater inflater = mCurrentActivity.getLayoutInflater();  
     View layout = inflater.inflate(R.layout.toast,  
         (ViewGroup) mCurrentActivity.findViewById(R.id.Toast_Root));  
   
     TextView text = (TextView)layout.findViewById(R.id.Toast_Message);  
     text.setText(mCurrentActivity.getApplicationContext().getText(id));  
     android.widget.Toast toast = new android.widget.Toast(mCurrentActivity.getApplicationContext());  
     toast.setDuration(android.widget.Toast.LENGTH_SHORT);  
     toast.setView(layout);  
     toast.show();  
   }  
 }  

No comments:

Post a Comment