Challenge 1: Toast Position Issue with setGravity


#1

An error comes up saying Cannot resolve symbol ‘Gravity’. Please help me figure this Gravity issue out. The QuizActivity.java looks like this:

    package com.bignerdranch.android.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton;
    private Button mFalseButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        mTrueButton = (Button) findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
                Toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });
        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
                Toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });
    }
}

The debug log says this:

Error:(25, 34) error: cannot find symbol variable Gravity
Error:(25, 46) error: cannot find symbol variable Gravity
Error:(25, 22) error: non-static method setGravity(int,int,int) cannot be referenced from a static context
Error:(33, 34) error: cannot find symbol variable Gravity
Error:(33, 46) error: cannot find symbol variable Gravity
Error:(33, 22) error: non-static method setGravity(int,int,int) cannot be referenced from a static context
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED

Thanks.


#2

I think your problem is show();
you can do easily that way to below :

Toast myToast = Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT);
myToast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
myToast.show();

#3

You need to instantiate an object from the class Toast before you can use the setGravity()method.

Try this:

mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast aToast = Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT); aToast.setGravity(Gravity.TOP,0,250); aToast.show(); } });


#4

Thanks, I understand it now. My problem is solved. :smiley:


#5

Why in this case:

public void onClick(View v) {
            Toast toast = new Toast(QuizActivity.this);
            toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.TOP, 0,0);
            toast.show();
        }

application get error: Unfortunately stopped?


#6

Please check the solution to your issue in the following link.


#7

Many Thanks to you Iscodex!! Problem solved…


#8

Iscodex is the man!! Thanks brother