Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show toast widget underneath a view

For those who helped me out earlier regarding this project, thank you very much! My code no longer has any problems, and I made extra tweaks. Now that the app is actually robust now, I want to do one more thing.

See screenshot of the layout here.

Normally, the toast view appears at the bottom center of the screen. I want to make it appear just below (8dp) the Submit button once OnClick is called. How can I accomplish this.

See my updated complete project here.

package com.lookjohn.guessnumber;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
Random random; 
Button button;
EditText text;

int input; 
int MIN, MAX;
int comp; 
int guesses;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    random = new Random();
    button = (Button)findViewById(R.id.button1);
    text = (EditText)findViewById(R.id.editText1);

    MIN = 1;
    MAX = 100;
    comp = random.nextInt(MAX - MIN + 1) + MIN; // Generate random number between 1 and 100.
    guesses = 0;

    button.setOnClickListener(myhandler1);
}

View.OnClickListener myhandler1 = new View.OnClickListener() {

    public void onClick(View v) {

        // Implemented max number of guesses, detect 
        // number of guesses and return from the method.

        String value = text.getText().toString(); // Get value from input from editTextView

        // If user submits an empty EditText, return to prevent a crash.
        if (value.isEmpty()) {
            Toast.makeText(MainActivity.this, "You must enter a guess!", Toast.LENGTH_SHORT);
            return;
        }

        input = Integer.parseInt(value); // Turn string into integer
        guesses++;
        if (input > 100) {
            Toast.makeText(MainActivity.this, 
                "That number is greater than 100. Not Valid!", 
                Toast.LENGTH_SHORT).show();
                return;
            }

        else if (input < comp)
            Toast.makeText(MainActivity.this, 
                "Your number is too small.", 
                Toast.LENGTH_SHORT).show();
        else if (input > comp) 
            Toast.makeText(MainActivity.this, 
                "Your number is too big.", 
                Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(MainActivity.this, 
                "Good job! The answer was " + comp + ".\n" +
                "You made " + guesses + " guesses.\n" +
                "Restart the app to try again.", 
                Toast.LENGTH_LONG).show();
    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
like image 714
jjkim Avatar asked Jan 09 '14 17:01

jjkim


People also ask

How do you change the position of Toast message?

Positioning your Toast You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset. For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this: toast.

How do you put Toast in the middle of the screen?

setGravity(Gravity. CENTER, 0, 0); toast.

Where do you show Toast messages?

To initiate a Toast object, the ```makeText()``` method is used. Here you should mention the application context, content of the message, and the duration of the toast message. To display the Toast message, you can use the method ```show()```.


1 Answers

None of the answers did the job to me, i made a combination of the above answers and I got the solution down.

enter image description here

The second timer is the target view.

 public static void displayToastUnderView(Activity activity, View view, String text) {
        Toast toast = Toast.makeText(activity, text, Toast.LENGTH_SHORT);
        toast.setGravity( Gravity.TOP, view.getLeft() - view.getWidth() / 2 - toast.getView().getWidth() / 2, view.getBottom());
        toast.show();
    }
like image 170
Boldijar Paul Avatar answered Sep 27 '22 19:09

Boldijar Paul