C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Android Custom Toast ExampleYou are able to create custom toast in android. So, you can display some images like congratulations or loss on the toast. It means you are able to customize the toast now. activity_main.xmlDrag the component that you want to display on the main activity. File: activity_main.xml
customtoast.xmlCreate another xml file inside the layout directory. Here we are having ImageView and TextView in this xml file. File: customtoast.xml
Activity classNow write the code to display the custom toast. File: MainActivity.java
package example.TheDeveloperBlog.com.customtoast; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //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(); } } Output:
Next TopicAndroid ToggleButton Example
|