First Android Application Challenge


#1

My code
package com.example.ray.geoquiz;

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

public class QuizActivity extends Activity {
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 mToast=Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_LONG);
             mToast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL,0,0);
             mToast.show();
             //Toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);

             //Does nothing yet,but soon!
         }
     });

     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_LONG).show();

            //Does nothing yet,but soon!
        }
    });
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}


#2

I know this is a bit old, but I just wanted to point out. The toast is a local variable to the onClick method, so by naming conventions we wouldn’t want to use the “m” when naming the variable as it denotes that it’s a class member.


#3

What i did is that i create a method that take int value which i pass the true and false string id .

//this method display toast with TextView.
private void toastCustomDisplay(int resId) {
Toast toast = Toast.makeText(MainActivity.this, resId, Toast.LENGTH_SHORT);
TextView textView = new TextView(getApplicationContext());
textView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.colorPrimary));
textView.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.white_color));
textView.setTextSize(30);
Typeface typeface = Typeface.create(“serif”, Typeface.BOLD);
textView.setTypeface(typeface);
textView.setPadding(10, 10, 10, 10);
textView.setText(resId);
toast.setView(textView);
toast.setGravity(Gravity.TOP|Gravity.CENTER,0,400);
toast.show();
}

then on calling the method i did something like this…

trueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int trueStringResId = R.string.true_toast;
toastCustomDisplay(trueStringResId);
}
});

    falseButtom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int falseStringResId = R.string.false_toast;
            toastCustomDisplay(falseStringResId);
        }
    });