Thursday, 28 March 2013

Custamized Dialog

Demo:




1)Custom Activity:



package com.venkool.customdialogdemo;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CustomDialogDemoActivity extends Activity {
      /** Called when the activity is first created. */

      Button btnCustomDialog;

      EditText edtUserName, edtPassword;

      Dialog customDialog;

      String Username, Password;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnCustomDialog = (Button) findViewById(R.id.showDialog);

            btnCustomDialog.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        customDialog = new Dialog(CustomDialogDemoActivity.this);
                        customDialog.setTitle("Custom Dialog");
                        customDialog.setContentView(R.layout.custom_dialog);
                        customDialog.show();

                        Button btnSave = (Button) customDialog
                                    .findViewById(R.id.button1);
                        Button btnCancel = (Button) customDialog
                                    .findViewById(R.id.button2);

                        edtUserName = (EditText) customDialog
                                    .findViewById(R.id.editText1);
                        edtPassword = (EditText) customDialog
                                    .findViewById(R.id.editText2);
                        btnSave.setOnClickListener(new OnClickListener() {

                              @Override
                              public void onClick(View v) {
                                    // TODO Auto-generated method stub

                                    Username = edtUserName.getText().toString();
                                    Password = edtPassword.getText().toString();

                                    Toast.makeText(getApplicationContext(),
                                                Username + "-" + Password, 3000).show();

                              }
                        });

                        btnCancel.setOnClickListener(new OnClickListener() {

                              @Override
                              public void onClick(View v) {
                                    // TODO Auto-generated method stub

                                    Toast.makeText(getApplicationContext(), "Cancel", 3000)
                                                .show();
                              }
                        });
                  }
            });
      }
}


2)main.xml:
 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <Button
        android:id="@+id/showDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="84dp"
        android:text="Custom Dialog" />

</RelativeLayout>


3)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:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter your Name"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Password"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel" />
    </TableRow>

</LinearLayout>


4)Download this Project Click Here

Combination of Grid and Gallery Views ( While select one image in GridView that image open in Gallery )

 Demo:



1)Main Activity:
 
package com.venky.gridandgallery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridandGalleryActivity extends Activity {
      /** Called when the activity is first created. */

      GridView gridView;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            gridView = (GridView) findViewById(R.id.gridView1);
            ImageAdapter ia = new ImageAdapter();

            gridView.setAdapter(ia);

      }

      class ImageAdapter extends BaseAdapter {

            public int getCount() {
                  // TODO Auto-generated method stub
                  return images.length;
            }

            public Object getItem(int position) {
                  // TODO Auto-generated method stub
                  return null;
            }

            public long getItemId(int position) {
                  // TODO Auto-generated method stub
                  return 0;
            }

            public View getView(final int position, View convertView,
                        ViewGroup parent) {
                  // TODO Auto-generated method stub

                  ImageView image = new ImageView(getApplicationContext());

                  image.setLayoutParams(new GridView.LayoutParams(100, 100));

                  image.setImageResource(images[position]);

                  image.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                              // TODO Auto-generated method stub

                              Intent intent = new Intent(getApplicationContext(),
                                          GalleryView.class);

                              intent.putExtra("position", position);

                              startActivity(intent);

                        }
                  });

                  return image;
            }

      }

      int[] images = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
                  R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7,
                  R.drawable.a8, R.drawable.a9, R.drawable.a10, R.drawable.a11,
                  R.drawable.a12 };
}


2)Gallery Activity:
 
package com.venky.gridandgallery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;

public class GalleryView extends Activity {

      Gallery gallery;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gallery);

            Intent intent = getIntent();

            int pos = intent.getIntExtra("position", 0);

            gallery = (Gallery) findViewById(R.id.gallery1);

            ImageAdapter ia = new ImageAdapter();
            gallery.setAdapter(ia);
            gallery.setSelection(pos);

      }

      class ImageAdapter extends BaseAdapter {

            public int getCount() {
                  // TODO Auto-generated method stub
                  return images.length;
            }

            public Object getItem(int position) {
                  // TODO Auto-generated method stub
                  return null;
            }

            public long getItemId(int position) {
                  // TODO Auto-generated method stub
                  return 0;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                  // TODO Auto-generated method stub

                  ImageView imageView = new ImageView(getApplicationContext());

                  imageView.setLayoutParams(new Gallery.LayoutParams(
                              LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

                  imageView.setImageResource(images[position]);

                  return imageView;
            }

      }

      int[] images = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
                  R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7,
                  R.drawable.a8, R.drawable.a9, R.drawable.a10, R.drawable.a11,
                  R.drawable.a12 };
}


3)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:background="#FFFFFF"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:numColumns="3" >
    </GridView>

</LinearLayout>


4)gallery.xml:

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


    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


5)Download this Project Click Here