Tuesday, April 23, 2013

List View - How to avoid black selected items

  Twice I faced the same problem. I had used listview. If you drag on the view it would show all the list elements.

Solution:

   In the code, use
listview.setCacheColorHint(Color.TRANSPARENT).

Your problem is solved

Simple. Isn't it?

Monday, April 22, 2013

Activity in android

      Activity is important ui unit of android application.

To start an activity from another, you have to use an intent by sending the new activity class as parameter.

e.g.
         Intent i = new Intent(this, NewActivity.class);


Next you call startActivity method

     startActivity(i);

You can pass extra data to the new actiivity by using putExtra method.


   i.putExtra( "Title",mTitle);


In the new activity, you can extract this information using getExtra methods

   Intent i = getIntent();
   String t = i.getStringExtra("Title");

      

Saturday, April 20, 2013

Threads in Android

Many of the network activities delay the program and the screen looks unresponsive. And ultimately you will get ANR.
  You can overcome this with a thread


class MyThread extends Thread{

      public MyThread(){
          //initialise your variables
      }
      public void run(){
         //write your expensive code here
     }
};

  In your ui therad you instantiate this thread


    MyThread myt = new MyThread(a,b,c);
    myt.start();

User will not notice the delay as myt is not in ui thread

Friday, April 19, 2013

Show a progress dialog when a thread is running

  We can easily show a progress dialog with Async task. But asyn task has its own drawbacks. There can be no ui's and the task can be executed only once.
   Better way is to use a thread for lengthy processing and show a progess dialog in the ui thread. (I struggled quite a bit. My progress dialog would keep on spinning infinitely :-( )

e.g.

//this is your main activity thread
handler = new FeedReadHandler();
....
.....

pd = ProgessDialog.show(context,"Loading","Loading the file. please wait...");
Thread background = new Thread(new Runnable(){     

                        @Override
                        public void run() {
                            readFeed(nameUrl);
                            Message msg = handler.obtainMessage();
                            msg.arg1=FEED_LOAD_FINISHED;
                            handler.sendMessage(msg);
                           
                        }
                       
                    });
                    background.start();
                    -.....
......

  Now you are starting a progress dialog and then starting the thread called background to run the function readFeed. When the function is completed, the thread sends a message to the handler.

Here is the handler code
 public class FeedReadHandler extends Handler{
       @Override
       public void handleMessage(Message msg){
           Log.d("SHOWRSS","i think i received a message");
           if(msg.arg1==FEED_LOAD_FINISHED){
               pd.dismiss();
               .....

               .....
              
           }
          
       }
   }

So the handler when receives the message from our background thread, on receiving this message it closes the progress- dialog.

How to use a DatePickerDialog

You do not have a direct widget for date entry. You should use an edittext and enter the date in yyyy-mm-dd format. Instead you can use a datepicker dialog. This dialog can be displayed when user types on the editText which should store the date

If your edit text is etDialogDate,

etDialogDate.setOnClickListener(new OnClickListener() {
          
            @Override
            public void onClick(View v) {
                String dateText = ((TextView)v).getText().toString();
                int year,month,day;
                String temp[]=dateText.split("-");
                year = Integer.valueOf(temp[0]);
                month = Integer.valueOf(temp[1]);
                day= Integer.valueOf(temp[2]);
                DatePickerDialog dtpkrdlg = new 
                     DatePickerDialog(ExpenseList.this, 0, 
                      new  DateSetListener(), year, month-1, day);
                dtpkrdlg.show();
              
            }});

Now this will show the datepicker dialog with initial value as date shown in edittext.

Once the date is set in the dialog it will call the callback function using DateSetListener. Let us define this DateSetListener class as follows

class DateSetListener implements DatePickerDialog.OnDateSetListener{

        @Override
        public void onDateSet(DatePicker view, 
                              int year, int monthOfYear,
                               int dayOfMonth) {
           
             etDialogDate.setText(""+year+"-"+
               (monthOfYear+1)+"-"+dayOfMonth);
           
        }
       
    }

Now any date set in the dialog is displayed back in our edittext widget.

Observe that we have used monthOfYear+1 because in Android Jan is 0 not 1.

Thursday, April 18, 2013

How to access sqlite table from emulator

You can see the contents of text files by exporting it from emulator, but what about sqlite tables? You can use sqlite3 as explained below.
You need to access the adb shell first.
If you are using Linux system, it is simple enough. Just give the command

    adb shell

But for windows xp system, the path might not be properly set. If that is the case, go to the folder where android sdk is installed

If android sdk is installed in f:\android-sdk-windows, then go there and then goto subdirectory tools in that.
Now give the command

     adb shell

Next you need to go to directory which has your sqlite database. For that you use command
     cd /data/data
     cd com.pkg.pkg1

Here com.pkg.pkg1 must be replaced by your packagename of android project.

     cd databases
     dir

At this point, dir must show you the name of your database file. If your database name is mydb, then to open it using sqlite, give the command

    sqlite3 mydb

Now you can use any sql command like select, insert , update etc.

     .tables
     select * from expenses;

Do not forget to end your sql commands with a semicolon.


Be careful if you give a wrong database name, instead of prompting sqlite just shows empty db.
Here is a list of some other useful commands
.schema table
Gives the create statement for that table so you will come to know the columns of the table

.headers on
Shows your select statement with column name headers.

.quit
Terminate sqlite

.help

To obtain a list of sqlite3 commands you can use




Wednesday, April 17, 2013

Tabs with different background colors

For past 3 days I was trying to have different colors for android tabs instead of normal grey and whitish grey. I got an articles from net android tabs like iphone. Now I am unable to find the article now. Anyways, here is the code

View view = new MyView(this, R.drawable.custombutton, R.drawable.custombutton, "Channels");
       //         channelTabSpec = tabhost.newTabSpec("channels").setIndicator(view)
        .setContent(intent);
 tabhost.addTab(channelTabSpec);
---
----
---

Similarly I created two more tabs Newspapers and Techsites. The class MyView used above is 

private class MyView extends LinearLayout {
          ImageView iv;
          TextView tv;
          public MyView(Context c, int drawable, int drawableselec, String label) {
           super(c);
         
           tv = new TextView(c);
           tv.setText(label);
          
           tv.setGravity(Gravity.CENTER);
           tv.setBackgroundColor(Color.TRANSPARENT);
           tv.setTextColor(Color.WHITE);
           tv.setTextSize(14);
          
           tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
             LayoutParams.WRAP_CONTENT, (float) 1.0));
           setOrientation(LinearLayout.VERTICAL);
           Drawable d = getResources().getDrawable(drawableselec);
           tv.setBackgroundDrawable(d);
          
            addView(tv);
        
          }
         }

I have created a file custombutton.xml in the folder /res/drawable for having different appearances for the normal tab and selected tab. Here is the file



   
       
                            android:endColor="@color/darkblue" android:angle="270" />
           
            
                            android:right="1dp" android:bottom="20dp" />
       

   

   
       
                            android:startColor="@color/blue" android:angle="270" android:type="radial" />
               
           
           
                            android:right="1dp" android:bottom="20dp" />
       

   

   


First item is for non-selected tab and second for selected. In selected tab we are using radial gradient with start color and end color. In non-selected tab we are using linear gradient with start color, center color and end color and angle as 270. Angle can be 90, 180, and 270. Other values for angle will crash the program. Center color is optional.


You can get more info about custom shapes in this android tutorial http://developer.android.com/guide/topics/resources/drawable-resource.html
At the end, my tabs look like this. Well don't bother about color combination, I know I am not so good in that.



The gradient type can be linear, radial or sweep - linear is default. For selected tab I am using radial gradient. 

Notice one more thing here, I am defining two shapes - one for selected and another for not selected - using selector. If you want to use this for button you can use android:state_pressed and android:state_focused and define different shapes for these.
Look at another example where I am defining different drawable pngs for different states

 
             android:drawable="@drawable/lightbtn" >
        
   

            android:drawable="@drawable/darkbtn">
        
   
 
    


This xml file is stored as mybutton.xml in /res/drawable-hdpi and I use the following line in my layout file
----
----

I have lightbtn.png and darkbtn.png in my /res/drawable-hdpi . Now when I touch the button darkbtn is displayed else lightbtn is displayed