Subscribe to H3R3T1C's Blog        RSS Feed
-----

Dream.In.Code for Android: The Code - Custom Dialogs!

Icon 1 Comments
Hello everyone today I added a new feature to DIC Mobile and I though I should share some of the code with you guys. Today im going to be sharing how to create custom dialogs!

NOTE: This is not meant to be a tutorial! Its just to show whats going on under the hood of the app! If anyone wants me to write a tutorial on custom dialogs or anything don't hesitate to ask :D.

This what the result looks like:
Posted Image

how to initialize the dialog
if(user.comments.size()>0)
	new CommentDialog(this).showComments(user.comments,user);


comment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" android:layout_width="fill_parent">
<TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textStyle="bold" android:background="@color/background" android:textColor="#000"></TextView>
<TextView android:text="@+id/TextView02" android:id="@+id/TextView02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="@color/background" android:textColor="#000"></TextView>
<TextView android:text="@+id/TextView03" android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:textColor="#000"></TextView>
</LinearLayout>


forum_page.xml
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout android:id="@+id/FrameLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_gravity="bottom" android:orientation="vertical">


<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="-20dip" android:text="Page: 1"></TextView><Button android:id="@+id/Button04" android:layout_height="40dip" android:text="Next Page" android:layout_gravity="right" android:layout_width="120dip" android:onclick="next"></Button>
<Button android:id="@+id/Button05" android:layout_height="40dip" android:layout_marginTop="-41dip" android:layout_width="120dip" android:onclick="prev" android:text="Previous Page"></Button>

</LinearLayout><ListView android:layout_width="fill_parent" android:id="@id/android:list" android:layout_height="fill_parent" android:layout_marginBottom="40dip">

</ListView></FrameLayout>



CommentDialog.java
import java.util.ArrayList;
import java.util.List;

import com.dic.api.MainPage;
import com.dic.api.data.Category;
import com.dic.api.data.Comment;
import com.dic.api.data.User;
import com.h3r3t1c.dic.adp.CommentArrayAdapter;
import com.h3r3t1c.dic.adp.StatsArrayAdapter;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class CommentDialog {

	private Context context;
	private List<Comment>comments;
	private Handler handler;
	private ProgressDialog prj;
	private Dialog d;
	private User user;
	
	private final int DISMISS = 0;
	private final int SHOW = 1;
	
	public CommentDialog(Context c)
	{
		context  = c;
		makeHandler();
	}
	private void makeHandler()
    {
    	handler = new Handler(){
    		@Override
			public void handleMessage(Message msg) {
    			switch(msg.what)
    			{
    			case DISMISS:
    			{
    				prj.dismiss();
    				((ListView)d.findViewById(android.R.id.list)).setAdapter(new CommentArrayAdapter(context,R.layout.comment,(ArrayList<Comment>) comments));
    				((TextView)d.findViewById(R.id.TextView01)).setText("Page: "+user.currentPage);
    				break;
    			}
    			case SHOW:
    			{
    				prj = new ProgressDialog(context);
    		    	prj.setIndeterminate(true);
    		        prj.setTitle("Please Wait...");		
    		        prj.setMessage(msg.getData().getString("msg"));
    		        //prj.setIcon(context.getResources().getDrawable(R.drawable.info));		
    		        prj.show();
    				break;
    			}
    			}
    		}
    	};
    }
	public void showComments(List<Comment>cmts,User u)
	{
		user = u;
		comments =cmts;
		d = new Dialog(context);
		d.setTitle("Comments");
		d.setContentView(R.layout.forum_page);
		((ListView)d.findViewById(android.R.id.list)).setAdapter(new CommentArrayAdapter(context,R.layout.comment,(ArrayList<Comment>) cmts));
		((ListView)d.findViewById(android.R.id.list)).setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				final User u = comments.get(position).user;
				 Message m = new Message();
			        m.what=SHOW;
			        Bundle b = new Bundle();
			        b.putString("msg", "Loading Profile...");
			        m.setData(B)/>;
			        handler.sendMessage(m);
				Runnable r = new Runnable(){
					@Override
					public void run() {
						u.viewUser();
						handler.sendEmptyMessage(DISMISS);
						Intent i = new Intent(context,UserPage.class);
						Bundle b = new Bundle();
						b.putSerializable("user", u);
						i.putExtras(B)/>;
						context.startActivity(i);
					}
		    	};
		    	Thread thread =  new Thread(null, r, "MagentoBackground");
		        thread.start();
				
				
			}});
		((Button)d.findViewById(R.id.Button04)).setonclickListener(new View.onclickListener() {
			
			@Override
			public void onclick(View arg0) {
				next();
			}
		});
		((Button)d.findViewById(R.id.Button05)).setonclickListener(new View.onclickListener() {
			
			@Override
			public void onclick(View arg0) {
				prev();
			}
		});
		d.show();
	}
	
	public void prev()
	{
		Runnable r = new Runnable(){
			@Override
			public void run() {
				user.viewPrevious();
				comments = user.comments;
				handler.sendEmptyMessage(DISMISS);
			}
        };
        Thread thread =  new Thread(null, r, "MagentoBackground");
        thread.start();
        Message m = new Message();
        m.what=SHOW;
        Bundle b = new Bundle();
        b.putString("msg", "Loading Next Previous...");
        m.setData(B)/>;
        handler.sendMessage(m);
	}
	public void next()
	{
		Runnable r = new Runnable(){
			@Override
			public void run() {
				user.viewNext();
				comments = user.comments;
				handler.sendEmptyMessage(DISMISS);
			}
        };
        Thread thread =  new Thread(null, r, "MagentoBackground");
        thread.start();
        Message m = new Message();
        m.what=SHOW;
        Bundle b = new Bundle();
        b.putString("msg", "Loading Next Page...");
        m.setData(B)/>;
        handler.sendMessage(m);
	}
	
}




CommentArrayAdapter.java
package com.h3r3t1c.dic.adp;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dic.api.data.Comment;
import com.dic.api.data.Forum;
import com.dic.api.data.User.Contact;
import com.h3r3t1c.dic.R;

public class CommentArrayAdapter extends ArrayAdapter<Comment>{

	private ArrayList<Comment> items;
	private Context context;
	private int id;
    public CommentArrayAdapter(Context context, int textViewResourceId, ArrayList<Comment> items) {
            super(context, textViewResourceId, items);
            this.items = items;
            this.context = context;
            id = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(id, null);
            }
            Comment o = items.get(position);
            if (o != null) {
                    TextView t1 = (TextView) v.findViewById(R.id.TextView01);
                    TextView t2 = (TextView) v.findViewById(R.id.TextView02);
                    TextView t3 = (TextView) v.findViewById(R.id.TextView03);
                    if(t1!=null)
                    	t1.setText("By: "+o.user.name);
                    if(t2!=null)
                    	t2.setText("On: "+o.date);
                    if(t3!=null)
                    	t3.setText(Html.fromHtml(o.text));
            }
            return v;
    }
}

1 Comments On This Entry

Page 1 of 1

DaneAU Icon

27 July 2010 - 07:10 PM
Nice work, was thinking of doing something similar on maemo :)
0
Page 1 of 1

October 2014

S M T W T F S
   1234
567891011
12131415161718
192021 22 232425
262728293031 

Tags

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories

    Android

    Android related stuff!