المواضيع

Dialog Customize Android

Dialog Customize Android

In this tutorial, we show you how to create a custom dialog in Android 
ex:in this image:-


Layout

File : 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 : custom.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'
    android:padding='10sp' >

    <EditText
        android:id='@+id/txt_name'
        android:layout_width='fill_parent'
        android:layout_height='wrap_content'
        android:hint='@string/dialog_uname'
        android:singleLine='true' >

        <requestFocus />
    </EditText>

    <EditText
        android:id='@+id/password'
        android:layout_width='match_parent'
        android:layout_height='wrap_content'
        android:ems='10'
        android:inputType='textPassword' >
    </EditText>

    <RelativeLayout
        android:layout_width='match_parent'
        android:layout_height='wrap_content' >

        <Button
            android:id='@+id/btn_login'
            android:layout_width='120dp'
            android:layout_height='wrap_content'
            android:text='@string/dialog_submit' />

        <Button
            android:id='@+id/btn_cancel'
            android:layout_width='120dp'
            android:layout_height='wrap_content'
            android:layout_alignParentTop='true'
            android:layout_marginLeft='10dp'
            android:layout_toRightOf='@+id/btn_login'
            android:text='@string/dialog_cancel' />
    </RelativeLayout>

</LinearLayout>


Activity
File : MainActivity.java

package com.androidarby.ameral;

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.EditText;
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("Login");

// set the custom dialog components - text, image and button

EditText text = (EditText) dialog.findViewById(R.id.txt_name);

//text.getText().toString();



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

dialog.show();
}

});

}

}

Download Source Code


ليست هناك تعليقات:

إرسال تعليق

عرب رويد arabroid Designed by Templateism | MyBloggerLab Copyright © 2014

صور المظاهر بواسطة richcano. يتم التشغيل بواسطة Blogger.