To display an Android Toast message at the Top of an Activity screen (over the ActionBar) we have to make use of setGravity() function. To do so we have to create a Toast Object, lets see an example
ToastActivity.javapackage com.code2care.toast;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
public class ToastActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast);
}
public void displayToast(View view) {
Toast toast = Toast.makeText(getApplicationContext(),
"This toast message will be displayed at the top of the activity screen!",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
}
}
activity_toast.xml
<LinerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.code2care.toast.MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="displayToast"
android:text="Display Toast at Top" />
</LinerLayout>
More Posts related to Android,
- Change Android Toast background color
- Maven : java.lang.ClassNotFoundException: Xmx512m
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Android Alert Dialog with Checkboxes example
- Android Error Generating Final Archive - Debug Certificate Expired
- How to add Newline to text in Android TextView
- Read Text file from SD Card : Android Programming
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- ActivityManager Warning: Activity not started, its current task has been brought to the front
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Android Developers Bluetooth Tutorial
- java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix]
- Android: Save Data in local Db using Android Room
- Channel 50 SMSes received every few minutes Android Phones
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Changing Android Intent Tittle using java code
- Android : No Launcher activity found! Error
- How to change TextView or EditText Text Color on Focus and on Press
- How to display Toast on Button Click : Android
- Android : Execute some code after back button is pressed
- Stop android adb service from command prompt or terminal
- [Soluiton] You already have the latest version of Android Studio installed
- Create Custom Android AlertDialog
- Android R Cannot Be Resolved To A Variable
- How to make Android EditText not editable
More Posts:
- Android Activity Main xml stuck loading - Android-Studio
- How to create StackOverflow error in java - Java
- How to fix: You will need Google Chrome to install most apps, extensions and themes. - Chrome
- Sublime Text 3 Convert Case to Upper, Lower, Title or Swap - Sublime-Text
- Java -Day of the week using Java 8 DayOfWeek Enum - Java
- SharePoint list excel import error - Title is a required filed and can't be empty - SharePoint
- How to send SMS on Android Emulator - Android
- Users experience call quality issue, voice distortion, disconnection with Microsoft Teams call and meeting - Teams