الخميس، 26 فبراير 2015

How to change position of Toast in android ?

What is Toast?

A toast provides simple feedback about an operation as a simple popup message. It is usually be displayed at the bottom of the Application.

What are we going to develop?

The application which we are going to develop will help you to create toast messages in different positions by altering Gravity and also by taking custom X-offset and Y-offset values.

hirpara

Positioning the Toast message

A standard toast notification appears at the bottom of the screen, centered horizontally. You can alter this position with the help of setGravity(int, int, int) method. This method accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
Below piece of code will display the Toast at top-left corner with X-Offset and Y-Offset as 100 and 200 respectively.


  1. Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
  2. // Set the Gravity to Top and Left
  3. toast.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);
  4. toast.show();

To show the Toast in Top Right corner with X-Offset and Y-Offset as 100 and 200 respectively:

1
2
3
4
Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
// Set the Gravity to Top and Right
toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);
toast.show();



Step 1: Create Android Application Project

  • Create new android project [File >> New >> Android Application Project] with project name AndroidToastPosition
  • Enter package name as ‘com.prgguru.example’ and activity name as ‘MainActivity’
  • Choose Minimum Required SDK, Target SDK and Compile with (Choose latest SDKs to make sure Facebook SDK will work without a ny issues). Confused on choosing these options? Take a look at Minimum Required SDK – Target SDK – Compile With post.
  • Click Next button and finally click ‘Finish’ to create project
Step 2: Design screen
Open activity_main.xml and replace it with below code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.prgguru.example.MainActivity" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="X-Offset" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:gravity="center"
        android:inputType="number" >
        <requestFocus />
    </EditText>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:text="Y-Offset" />
    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:gravity="center"
        android:inputType="number" >
    </EditText>
    <Button
        android:id="@+id/button1"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:onClick="showToastTopLeft"
        android:text="Show Toast (With Top/Left Gravity)" >
    </Button>
    <Button
        android:id="@+id/button2"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:onClick="showToastTopRight"
        android:text="Show Toast (With Top/Right Gravity)" >
    </Button>
    <Button
        android:id="@+id/button3"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:onClick="showToastBottomLeft"
        android:text="Show Toast (With Bottom/Left Gravity)" >
    </Button>
    <Button
        android:id="@+id/button4"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true"
        android:onClick="showToastBottomRight"
        android:text="Show Toast (With Bottom/Right Gravity)" >
    </Button>
</RelativeLayout>
Step 3: MainActivity.java – Main Class
Open MainActivity.java and replace it with below code.
Each line of code is commented well, if you still have any doubt or question 0 Comments it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.prgguru.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
     
    /**
     * When Button "@+id/button1" is clicked
     * @param v
     */
    public void showToastTopLeft(View v) {
        EditText e1 = (EditText) findViewById(R.id.editText1);
        EditText e2 = (EditText) findViewById(R.id.editText2);
        if (e1.getText().toString().trim().length() > 0
                && e2.getText().toString().trim().length() > 0) {
            String editText1 = e1.getText().toString();
            String editText2 = e2.getText().toString();
            int xOffset = Integer.parseInt(editText1);
            int yOffset = Integer.parseInt(editText2);
            Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
            // Set the Gravity to Top and Left
            toast.setGravity(Gravity.TOP | Gravity.LEFT, xOffset, yOffset);
            toast.show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT)
                    .show();
        }
    }
     
    /**
     * When Button "@+id/button2" is clicked
     * @param v
     */
    public void showToastTopRight(View v) {
        EditText e1 = (EditText) findViewById(R.id.editText1);
        EditText e2 = (EditText) findViewById(R.id.editText2);
        if (e1.getText().toString().trim().length() > 0
                && e2.getText().toString().trim().length() > 0) {
            String editText1 = e1.getText().toString();
            String editText2 = e2.getText().toString();
            int xOffset = Integer.parseInt(editText1);
            int yOffset = Integer.parseInt(editText2);
            Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
            // Set the Gravity to Top and Right
            toast.setGravity(Gravity.TOP | Gravity.RIGHT, xOffset, yOffset);
            toast.show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT)
                    .show();
        }
    }
     
    /**
     * When Button "@+id/button3" is clicked
     * @param v
     */
    public void showToastBottomLeft(View v) {
        EditText e1 = (EditText) findViewById(R.id.editText1);
        EditText e2 = (EditText) findViewById(R.id.editText2);
        if (e1.getText().toString().trim().length() > 0
                && e2.getText().toString().trim().length() > 0) {
            String editText1 = e1.getText().toString();
            String editText2 = e2.getText().toString();
            int xOffset = Integer.parseInt(editText1);
            int yOffset = Integer.parseInt(editText2);
            Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
            // Set the Gravity to Bottom and Left
            toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, xOffset, yOffset);
            toast.show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT)
                    .show();
        }
    }
     
    /**
     * When Button "@+id/button4" is clicked
     * @param v
     */
    public void showToastBottomRight(View v) {
        EditText e1 = (EditText) findViewById(R.id.editText1);
        EditText e2 = (EditText) findViewById(R.id.editText2);
        if (e1.getText().toString().trim().length() > 0
                && e2.getText().toString().trim().length() > 0) {
            String editText1 = e1.getText().toString();
            String editText2 = e2.getText().toString();
            int xOffset = Integer.parseInt(editText1);
            int yOffset = Integer.parseInt(editText2);
            Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
            // Set the Gravity to Bottom and Right
            toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, xOffset, yOffset);
            toast.show();
        } else {
            Toast.makeText(getApplicationContext(),    "Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT).show();
        }
    }
}

Demo

That’s all. It’s time to test our code.
Run the application using emulator or device by right clicking on the project >> Run as >> Android applicaiton >> Choose emulator or device.




Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project (How to import android project in eclipse).
Download Source Code

0 التعليقات:

إرسال تعليق