Ashoka
Ashoka

Reputation: 482

Toast message when the checkbox is not selcted and the button is clicked in android

Hey Guys I have a checkbox and a apply button . So I when some doesn't check the checkbox and click on the apply button it should give a toast message and if he does check the checkbox and onclick on the apply it will take him to the next activity. This is my code. MainActvity.java

package com.meti.workforhome;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button mApply = (Button) findViewById(R.id.Apply);
    final CheckBox checkBox = (CheckBox) findViewById(R.id.check);
    mApply.setOnClickListener(new OnClickListener(){
         public void onClick(View v) {
             if (checkBox.isChecked()) { 
                // TODO Auto-generated method stub
                String clicks = "click";
                String message = "Apply button clicked";
                Log.i(clicks, message);
                Intent i = new Intent(MainActivity.this, FormActivity.class);
                startActivity(i);
            }
             else 
             {
                 Toast.makeText(this.getApplicationContext(), R.string.notice, Toast.LENGTH_LONG).show();
             }
         }

        private Context getApplicationContext() {
            // TODO Auto-generated method stub
            return null;
        }
        });
    }




@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;
}

}

activity_main.xml

<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=".MainActivity" 
android:orientation="vertical"
android:background="#A9F5F2">

<TextView
    android:id="@+id/topic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="22sp"
    android:text="@string/topic" />


<ScrollView 
android:id="@+id/Scrolly"
android:layout_width="275dp"
android:layout_height="200dp"
android:layout_below="@id/topic"
android:isScrollContainer="true"
android:fadingEdgeLength="14.5sp"
android:focusable="true">

<LinearLayout
    android:layout_width="275dp"
    android:layout_height="200dp" >

 <TextView
    android:id="@+id/rules"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:textSize="22sp"
    android:text="@string/rules" />
 </LinearLayout>
</ScrollView>
  <CheckBox
     android:id="@+id/check"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/Scrolly"
     android:layout_below="@+id/Scrolly"
     android:layout_marginTop="8dp"
     android:text="@string/check" />

 <Button
     android:id="@+id/Apply"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/check"
     android:layout_below="@+id/check"
     android:layout_marginTop="32dp"
     android:background="#3b5998"
     android:paddingLeft="2dp"
     android:paddingRight="2dp"
     android:text="@string/after"
     android:textColor="#ffffff"
     android:textStyle="bold" />

</RelativeLayout>

This question may silly .But i am new to android please correct were I am goin wrong .Thanks in advance.

Upvotes: 0

Views: 776

Answers (2)

Raghunandan
Raghunandan

Reputation: 133570

Remove

private Context getApplicationContext() {
        // TODO Auto-generated method stub
return null;
}

Just use

Toast.makeText(MainActivity.this, R.string.notice, Toast.LENGTH_LONG).show();

or

Toast.makeText(getApplicationContext(), R.string.notice, Toast.LENGTH_LONG).show(); 

You can also use v.getContext()

Upvotes: 2

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

Remove this:

    private Context getApplicationContext() {
        // TODO Auto-generated method stub
        return null;
    }

The rest can be left as is, since getApplicationContext() is a system function and will return the Context of your app.
You don't need to redefine it.

And as you did, it doesn't return any context, since you make it return null.

Upvotes: 1

Related Questions