Showing posts with label Toast. Show all posts

Thursday, 11 June 2015

Custom Alert box or custom dialog box

Hello,In this android tutorial i want to explore a simple program about android Custom Alert Dialog box to give a professional look for your app.This custom alert dialog box displays alerts to the users for get confirmation from the users.Let's go for coding with easy steps:
In this android example of Custom alert dialog box we show:

Title: The title like "Are you sure to exit". (Optional)
Content: Displays the message to the user. It can be a string message or a list or custom layout.
Action Button(s): Define action on button like "Exit" , "Yes / No" , "Later" , "Cancel" etc.

We need for custom alert box:

1) activity_main.xml
2) custom_alert.xml
3) MainActivity.java

CREATE NEW ANDROID PROJECT:

Step 1: Write code into activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonAlert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Custom Alert me" />


</LinearLayout>

Step 2: Write code into custom_alert.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/displayimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/txview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/buttonok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="All Right" />

        <Button
            android:id="@+id/buttonexit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Exit" />
    </LinearLayout>

</LinearLayout>

Step 3: Write code into MainActivity.java

package dev.androidapplink.alertboxapp;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
final Context context = this;
// create variable
private Button button;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
button = (Button) findViewById(R.id.buttonAlert);
// create button listener

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

// custom dialog and set view
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_alert);
dialog.setTitle("androidapplink");

// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.txview);
text.setText("All is right in example?");
ImageView image = (ImageView) dialog
.findViewById(R.id.displayimage);
image.setImageResource(R.drawable.ic_launcher);

// Load Control
Button dialogButtonok = (Button) dialog
.findViewById(R.id.buttonok);
Button dialogButtonexit = (Button) dialog
.findViewById(R.id.buttonexit);

// if button is clicked, close the custom dialog
dialogButtonok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

dialog.show();
// if button is clicked, close the current activity
dialogButtonexit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
// Toast...
Toast.makeText(getApplicationContext(),
"Activity is closed", Toast.LENGTH_SHORT)
.show();
// Toast...
Toast.makeText(getApplicationContext(),
"It's Too Right :)", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
});
}

}
NOTE:No need any Permission in AndroidManifest.xml
Step 4: Run Your Project:







Wednesday, 10 June 2015

Alert dialog box

Hello,today i want to explore a simple program about android Alert Dialog box to give a professional look for your app.This alert dialog box displays alerts to the users for get confirmation from the users.Let's go for coding with easy steps:

In alert dialog box we show:

Title: The title like "Are you sure to exit". (Optional)

Content: Displays the message to the user. It can be a string message or a list or custom layout.


Action Button(s): Define action on button like "Exit" , "Yes / No" , "Later" , "Cancel" etc.

CREATE NEW ANDROID PROJECT:

Step 1: Write code into activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonAlert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Alert me" />
            

</LinearLayout>

Step 2: Write code into MainActivity.java

package dev.androidapplink.alertboxapp;

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

public class MainActivity extends Activity {

final Context context = this;
// create variable
private Button button;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
button = (Button) findViewById(R.id.buttonAlert);

// create button listener
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);

// set title
alertDialogBuilder.setTitle("Are You Sure?");

// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

}

});

}


}

NOTE: No need any permission in AndroidManifest.xml

Step 3:Run Your Project:







simple Toast android example

Today i want to explore how to create Toast in android. its nothing but like a notification to user some events is done.we can say it is a one type of pop up that display certain amount of time and automatically fade in and out.package android.widget.Toast; is allow to make toast.
In this android example,you learn how to create a Simple Toast in android:

CREATE NEW PROJECT

Step 1: Write code into activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Toast" />
</LinearLayout>

Step 2: Write a code into MainActivity.java

package dev.androidapplink.tosterdemoapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

// create object
private Button button;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 // load control
button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View arg0) {

    Toast.makeText(getApplicationContext(), 
                               "Button is clicked", Toast.LENGTH_LONG).show();

 }
});
}

}
For Custom Toast:
Step 3: Now Run Your Project:



Custom Toast android example

Today i want to explore how to create Toast in android. its nothing but like t notification to user some events is done.we can say it is a one type of pop up that display certain amount of time and automatically fade in and out.

In this android example,you learn how to create a Custom Toast in android:

CREATE NEW PROJECT

We will create:

1) activity_main.xml
2) custom_toast.xml

3) MainActivity.java

Step 1: Write code into activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        
    <Button
        android:id="@+id/buttonToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Show Toast" />
    

</LinearLayout>

Step 2 Write code into custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF"
    android:orientation="horizontal"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#000" />


</LinearLayout>


Step 3: Write code into MainActivity.java


package dev.androidapplink.tosterdemoapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private Button button;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.buttonToast);

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

// inflar your custom_toast.xml ayout
LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));

// set a image
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);

// set a message to display
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Button is clicked!");

// Display Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

}
});

}
}

Step 4: Run Your Project:




Follow me Share