Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Implement an application that creates an alert upon receiving a message in android

1)Open eclipse or android studio and select new android project
2)Give project name and select next
3) Choose the android version.Choose the lowest android version(Android 2.2) and select next
4) Enter the package name.package name must be two word seprated by comma and click finish
5)Go to package explorer in the left hand side.select our project.
6)Go to res folder and select layout.Double click the main.xml file.Add the code below
  <ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" >
<TableLayout         
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:shrinkColumns="*"  android:stretchColumns="*" android:background="#000000">
                 
                        <TableRow
                            android:layout_height="wrap_content"
                            android:layout_width="match_parent"
                            android:gravity="center_horizontal">
                           <TextView
      android:id="@+id/Title"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_margin="5px"
      android:focusable="false"
      android:focusableInTouchMode="false"
      android:gravity="center_vertical|center_horizontal"
      android:text="QUIZ"
      android:textSize="25sp"
      android:textStyle="bold" />
                           <View
                android:layout_height="2px"
                android:layout_marginTop="5dip"
                android:layout_marginBottom="5dip"
                android:background="#DDFFDD"/>
                       </TableRow>
                        <TableRow
                            android:layout_height="wrap_content"
                            android:layout_width="match_parent"
                            android:gravity="center_horizontal">
                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:textSize="18sp" android:text="1.CAPTIAL OF INDIA"
android:layout_span="4"
                                android:padding="18dip"
                                android:textColor="#ffffff"/>
                       </TableRow>
                  
                        <TableRow
                            android:id="@+id/tableRow1"
                            android:layout_height="wrap_content"
                            android:layout_width="match_parent">
         <RadioGroup
            android:id="@+id/answer1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.4" >
            <RadioButton
                android:id="@+id/answer1A"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:text="CHENNAI" />
            <RadioButton
                android:id="@+id/answer1B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:text="NEW DELHI" />
            <RadioButton
                android:id="@+id/answer1C"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:text="MUMBAI" />
             <RadioButton
                android:id="@+id/answer1D"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:text="HYDERBAD" />
        </RadioGroup>
                        </TableRow>
                      <TableRow
                            android:layout_height="wrap_content"
                            android:layout_width="match_parent"
                            android:gravity="center_horizontal">
                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:textSize="18sp"
                                android:text="2. CAPTIAL OF RUSSIA?"
                                android:layout_span="4"
                                  android:padding="18dip"
                                android:textColor="#ffffff"/>
                       </TableRow>
                     
                        <TableRow
                            android:id="@+id/tableRow2"
                            android:layout_height="wrap_content"
                            android:layout_width="match_parent">
                            <RadioGroup
            android:id="@+id/answer2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.4" >
            <RadioButton
                android:id="@+id/answer2A"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:text="WARSAW    " />
            <RadioButton
                android:id="@+id/answer2B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:text="BERLIN" />
            <RadioButton
                android:id="@+id/answer2C"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:text="MASCOW    " />
             <RadioButton
                android:id="@+id/answer2D"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:text="CANEBRA    " />
        </RadioGroup>
                        </TableRow>
                         <TableRow
                            android:layout_height="wrap_content"
                            android:layout_width="match_parent"
                            android:gravity="center_horizontal">
                            <Button
                                android:id="@+id/submit"
                                android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                  android:gravity="center"
                                    android:text="Submit"
                                    />
                       </TableRow>                                                                       
      </TableLayout>      
</ScrollView>

7) Now select mainactivity.java file and type the following code.In my coding maniactivity name is Alert1Activity .
package alert1.quiz;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Alert1Activity extends Activity {
    private Button btnSubmitQuiz;
    int score,ans1,ans2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RadioGroup b1=(RadioGroup)findViewById(R.id.answer1);
        b1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                switch(checkedId) {
                    case R.id.answer1A:
                                                      ans1 =1;
                    break;
                    case R.id.answer1B:
                                                      ans1 =2;                  
                    break;
                    case R.id.answer1C:
                                                      ans1 =3;
                    break;
                    case R.id.answer1D:
                                                      ans1=4;
                    break;
                }
            }
        });
         RadioGroup b2=(RadioGroup)findViewById(R.id.answer2);
         b2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
             public void onCheckedChanged(RadioGroup group, int checkedId) {
                 // TODO Auto-generated method stub
                 switch(checkedId) {
                     case R.id.answer2A:
                                                       ans2 =1;
                     break;
                     case R.id.answer2B:
                                                       ans2 =2;
                   
                     break;
                     case R.id.answer2C:
                                                       ans2 =3;
                     break;
                     case R.id.answer2D:
                                                       ans2=4;
                     break;
                 }
             }
         });
         btnSubmitQuiz = (Button) findViewById(R.id.submit);
         btnSubmitQuiz.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 AlertDialog.Builder alertDialog = new AlertDialog.Builder(Alert1Activity.this);
                  // Setting Dialog Title
                  alertDialog.setTitle("SHOW RESULT");
                  // Setting Dialog Message
                  alertDialog.setMessage("Are you sure you want SUBMIT this?");
                  // Setting Icon to Dialog
                 // alertDialog.setIcon(R.drawable.tick);
                  // Setting Positive "Yes" Button
                  alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog,int which) {
                          score =0;
                           // TODO Auto-generated method stub
                           if(ans1 == 2)
                                                             score++;
                           if(ans2 == 3)
                                                             score++;                                                                               
                           Toast.makeText(Alert1Activity.this, "Your score is:"+score+" out of 2.", Toast.LENGTH_LONG).show();
                         
                      }
                  });
                  // Setting Negative "NO" Button
                  alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                      // Write your code here to invoke NO event
                      Toast.makeText(getApplicationContext(), "You clicked  NO.CHECK YOUR ANSWER", Toast.LENGTH_SHORT).show();
                      dialog.cancel();
                      }
                  });
                  // Showing Alert Message
                  alertDialog.show();
             }
         });
       
    }
}

8)Now go to main.xml and right click .select run as option and select run configuration
9) Android output is present in the android emulator as shown in below.







  
  
  
      

No comments:

Post a Comment

Flag Counter