About
Home > Tutorials Home > Ios > AlertBox In Android

AlertBox In Android

Published on October 17, 2019 8:14:56 PM

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the Eclipse and create a new Android project using Blank Activity application template. Give the application name "Button Example in Android".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
	
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Aler Box"
        android:id="@+id/bt1" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Toast"
        android:id="@+id/bt2" />

</LinearLayout>

Step 2 : Copy the Following Code into your .Java File.

package com.example.alertboxexample;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button bt1;
	private Button bt2;
	
	@Override
    public void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt1=(Button)findViewById(R.id.bt1);
        bt2=(Button)findViewById(R.id.bt2);

        bt2.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v)
			{
				Toast.makeText(MainActivity.this,"Toast Example",1000).show();
			}
		});

        bt1.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v)
			{
				new AlertDialog.Builder(MainActivity.this) .setIcon(R.drawable.ic_action_search).setTitle("Alert Box").
				setMessage("Wel Come AlertBox") .setPositiveButton("Ok",new DialogInterface.OnClickListener()
				{
					[email protected]
					public void onClick(DialogInterface dialog, int which)
					{
						Toast.makeText(MainActivity.this,"U press Yes Button",1000).show();
					}
				}).setNegativeButton("Cancle",new DialogInterface.OnClickListener()
				{	
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						finish();
					}
				}).show();
			}
		});
    }
}
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile