Tuesday 7 June 2011

Android : Displaying list with image and text

See following class

package com.example;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class ListWithImage extends ListActivity {

String[] scrips;


//for running task in background
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
setListAdapter(new Scripdapter(getApplicationContext(), R.layout.main, scrips));
}

};

/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scrips = getResources().getStringArray(R.array.scrip);
upadateList();

}

public void upadateList() {
Thread t = new Thread(new Runnable() {
public void run() {
handler.handleMessage(handler.obtainMessage());
}
});
t.start();
}


class Scripdapter extends ArrayAdapter {

private String[] items;

Scripdapter(Context context, int textViewResourceId, String[] items) {
super(context, textViewResourceId, items);
this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = vi.inflate(R.layout.main, null);

}

String item = items[position];
if (item != null) {
TextView txtV = (TextView) row.findViewById(R.id.scripList);
ImageView imgV = (ImageView) row.findViewById(R.id.scripimage);
if(txtV != null){
txtV.setText(item);
}
if (item.equals("sbi") || item.equals("unitech")) {
imgV.setImageResource(R.drawable.red);
} else if (item.equals("BNP paribas") || item.equals("wipro")) {
imgV.setImageResource(R.drawable.green);
} else if (item.equals("dlf") || item.equals("nokia")) {
imgV.setImageResource(R.drawable.green);
} else {
imgV.setImageResource(R.drawable.icon);
}
}

return row;
}

}

}

main.xml                                                                                                                      



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
    <ImageView android:layout_height="wrap_content"
               android:src="@drawable/icon"
               android:layout_width="22px"
               android:layout_marginTop="4px"
               android:layout_marginRight="4px"
               android:layout_marginLeft="4px"
               android:id="@+id/scripimage">
    </ImageView>
    <TextView android:text="@+id/TextView01"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/scripList"
              android:textSize="30px"></TextView>
</LinearLayout>









string.xml                                                                                      

<resources>
    <string name="app_name">ContactsView</string>
    <string-array name="scrip">
        <item>sbi</item>
        <item>Geojit</item>
        <item>BNP paribas</item>
        <item>dlf</item>
        <item>unitech</item>
        <item>suzlon</item>
        <item>axis bank</item>
        <item>wipro</item>
        <item>infosis</item>
        <item>nokia</item>
    </string-array>
</resources>



output                                                                                                                                        



                   

Saturday 4 June 2011

Android programming using IntelliJIdea

Prerequisite


  Install following:-

  1. JDK                :  http://www.oracle.com/
  2. IntelliJIdea       :http://www.jetbrains.com/idea      (Community Edition)
  3 .Android SDK   : http://developer.android.com

 
 Open IntelliJIdea .Select file > New project,the click select project from scratch as shown below :

  

                            

    Click ok.

Then type Project name as HellowWorld in Name textbox,then choose Android module from select type.Click Ok.




 check create source and click next.

                                           

                                                                            

 Browse android sdk.

                                    

 Click + button as shown above to browse android sdk .select jdk too.click ok.Then home sceen will show as follows:
                                         
 Click edit configuration above to select/create andoird virtual device.

                                    


 Check select device manually or select listed device you previously created.If suggest to use choose manually,so that we can select preferred config device at run time.click ok.Now click run.Then you are supposed to create a device if not created previously.Click launch emulator.Noe create one as follows.


                                        


Click ok.Click launch emulator.Noe emulator will launch as follows:

                                     
   Unlock Virtual phone.Click on green color lock and drag to right will unlock virtual phone.

                                      


Now you can see HellowWold Application on screen.Click that icon.

                             

 That's it.Great you did it well.Now you can have a simle, your first android application launched ryt.


Note : Don't close emulator just do changes in code and click run it will automatically reflect  in emulator.