Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

android - 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 + ".
" +
                "You made " + guesses + " guesses.
" +
                "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;
}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

android.widget.Toast defines a bunch of useful methods that allow customizing look and feel of a toast notification. Method you should look into is setGravity(int, int, int). With 0 offsets code below will anchor toast top to the bottom of the provided view and toast center to the center of the view.

public static void positionToast(Toast toast, View view, Window window, int offsetX, int offsetY) {
    // toasts are positioned relatively to decor view, views relatively to their parents, we have to gather additional data to have a common coordinate system
    Rect rect = new Rect();
    window.getDecorView().getWindowVisibleDisplayFrame(rect);
    // covert anchor view absolute position to a position which is relative to decor view
    int[] viewLocation = new int[2];
    view.getLocationInWindow(viewLocation);
    int viewLeft = viewLocation[0] - rect.left;
    int viewTop = viewLocation[1] - rect.top;

    // measure toast to center it relatively to the anchor view
    DisplayMetrics metrics = new DisplayMetrics();
    window.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int widthMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.widthPixels, MeasureSpec.UNSPECIFIED);
    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.heightPixels, MeasureSpec.UNSPECIFIED);
    toast.getView().measure(widthMeasureSpec, heightMeasureSpec);
    int toastWidth = toast.getView().getMeasuredWidth();

    // compute toast offsets
    int toastX = viewLeft + (view.getWidth() - toastWidth) / 2 + offsetX;
    int toastY = viewTop + view.getHeight() + offsetY;

    toast.setGravity(Gravity.LEFT | Gravity.TOP, toastX, toastY);
}

Using it will require modifying toast related lines in your onClick method:

int offsetY = getResources().getDimensionPixelSize(R.dimen.toast_offset_y);

Toast toast = Toast.makeText(MainActivity.this, "That number is greater than 100. Not Valid!", Toast.LENGTH_SHORT);
positionToast(toast, v, getWindow(), 0, offsetY);
toast.show();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[2] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...