Jump to content
MVH1362

Create A Pop Up Windows On Android Applications - Tutorial - Basics

Recommended Posts

MVH1362

BASIC TUTORIAL #2

 

Hello There, It's Me MVH1362, Today I Will Teach You How To Setup A Pop Up Windows On Android Applications.

 

Android Layout Files Two XML files, one for main screen, one for custom dialog.

 

File : res/layout/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/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Dialog" />

</LinearLayout>

 

File : res/layout/custom.xml

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

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

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

     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />

</RelativeLayout>

 

2. Activity

 

File : MainActivity.java

package com.appname.app;

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;

public class MainActivity extends Activity {

 final Context context = this;
 private Button button;

 public void onCreate(Bundle savedInstanceState) {

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

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

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

    @Override
    public void onClick(View arg0) {

   // custom dialog
   final Dialog dialog = new Dialog(context);
   dialog.setContentView(R.layout.custom);
   dialog.setTitle("Title...");

   // set the custom dialog components - text, image and button
   TextView text = (TextView) dialog.findViewById(R.id.text);
   text.setText("Android custom dialog example!");
   ImageView image = (ImageView) dialog.findViewById(R.id.image);
   image.setImageResource(R.drawable.ic_launcher);

   Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
   // if button is clicked, close the custom dialog
   dialogButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     dialog.dismiss();
    }
   });

   dialog.show();
    }
  });
 }
}

 

Now You Are Done! Build Your APK And Click On The Show Custom Dialog To View The Pop Up Window - Click On OK To Close It.

 

You Can Also Use It In Any Applications Or Games Too...

 

Basic Tutorial #2

 

Advanced Tutorials Are Coming Soon... Stay Tuned !:cool: 

  • Upvote 5

Share this post


Link to post
Share on other sites
abhi93696

Nice & keep them coming!! :)

  • Like 1

Share this post


Link to post
Share on other sites
MVH1362

Ya ! Will Be Out Here As A Series Of Tutorials ! :)

 

Thanks !

  • Like 1

Share this post


Link to post
Share on other sites
KBIT013

Thanks Slotbro

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

Guidelines