Showing posts with label Multicolumns ListView in Android. Show all posts
Showing posts with label Multicolumns ListView in Android. Show all posts

Jun 3, 2011

Android : Creating notification using Toast


What is toast notification ?
Toast notification is a message popup in android application which popup on application window and automatically fades in. It fills only required area on screen and user's activity and interaction are remains enable, it mean during popup of toast message user can do interaction's like entering text, selection of controls ect.
For this android lib has one class android.widget.Toast, this class has methods to create toast, set their location , duration and message.
For example, simple text message base toast.
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;
 
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//call to show toast
ShowToast("Test Toast");
}
private void ShowToast(String msg )
{
Toast toast = Toast.makeText(Main.this, msg,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
}
}
n above example we have created a toast object using makeText static method of class which returns toast object.
makeText has two overloads as given below
//Make a standard toast that just contains a text 
view with the text from a resource.
static Toast makeText(Context context, 
int resId, int duration)
 
//Make a standard toast that just contains a text from string.
static Toast makeText(Context context, 
CharSequence text, int duration)
 
For setting location of toast notification there is method setGravity. it Set the location at which the notification should appear on the screen.
public void setGravity (int gravity, int xOffset, int yOffset) 
First parameter is gravity and different gravity constants are define in class Gravity. You can see different  Gravity constants and their description using Eclipse IDE.
Other useful methods are
setDuration(int duration) and setMargin(float horizontalMargin, float verticalMargin)
setDuration is used to set duration of toast display and it can be short or long while setMargin is used to specify vertical and horizontal margin.
show() method is used to show the toast.

Custom View Toast:

There is an important method setView(View view) and this is used to show custom view as toast.
This is used when you want to show custom view as toast notification like some colorful text and image and other.
Here is one example
package bimbim.in;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
Button mBtn, mBtn2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtn = (Button) findViewById(R.id.btn1);
mBtn2 = (Button) findViewById(R.id.btn2);
mBtn.setOnClickListener(this);
mBtn2.setOnClickListener(this);
toast = new Toast(getApplicationContext());
}
@Override
public void onClick(View v) {
if (v == mBtn)
ShowCustomeViewToast();
else if (v == mBtn2)
toast.cancel();
}
/*
* private void ShowToast(String msg) { Toast toast =
* Toast.makeText(Main.this, msg, Toast.LENGTH_LONG);
* toast.setGravity(Gravity.TOP, 0, 0); toast.show(); }
*/
Toast toast;
private void ShowCustomeViewToast() {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.customeview,
(ViewGroup) findViewById(R.id.root_layout));
ImageView image = (ImageView) layout.findViewById(R.id.image1);
image.setImageResource(R.drawable.icon);
TextView text = (TextView) layout.findViewById(R.id.text1);
text.setText("Image and Text toast");
toast.setGravity(Gravity.TOP, 50, 50);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
}
Show Custom Toast
Close Custom Toast


Mar 4, 2011

Multicolumn ListView in Android

Ever since I started programming on the Android platform, I have been wondering when the SDK would include a ready-made multicolumn ListView (or listbox as it is often called in other frameworks). One could of course construct such a thing by slapping regular ListViews side by side and then painfully adding code to keep them all in sync when scrolled and so on. It felt such a hack that I used GridView instead. GridView is not really a great list, though, because the cells will all be formatted the same. There are other issues besides that, but for me that was the main one.
I finally saw a blog post which customized ListView to put Three TextViews vertically into one line. It was pretty simple going from there to one that would display three TextViews side-by-side. The main layout XML file could define ListView something like this:
<!-- main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
 />
</LinearLayout>
Here is the XML for each row, main point being that we put three TextViews in LinearLayout with horizontal orientation:
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:paddingTop="4dip"
    android:paddingBottom="6dip"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView 
    android:id="@+id/t1"
    android:layout_width="90dip"
    android:layout_height="wrap_content"
    android:text="text1"/>
     <TextView 
     android:id="@+id/t2"
    android:layout_width="90dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="text2"/>
      <TextView 
     android:id="@+id/t3"
    android:layout_width="50dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="text3" />
      </LinearLayout>
Notice how you can format each TextView separately. There is no column separator, but something decent should not be too hard to whip up if desired. My understanding is that the screen width is supposed to be 160 dip, but for some reason I had to use width values that actually add up to more than that, as well as using layout weight to grow some fields proportionally so that when you switch to landscape mode things are still nicely aligned. I would have expected specifying widths in dip would have automatically done that.
Here is how you could populate that ListView with data:
ListView list =getListView();
 
Main class :
-------------
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MultiList extends ListActivity {
   
    ListView lv;
    SimpleAdapter sd;
      @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
        super.onCreate(savedInstanceState);
       Calendar c=Calendar.getInstance();
       lv=getListView();
        ArrayList<HashMap<String,String>> alist=new ArrayList<HashMap<String,String>>();
        HashMap<String, String>map=new HashMap<String, String>();
        map.put("Date","Date :"+c.get(Calendar.DATE));  // printing date
        map.put("Month", "Month :"+(c.get(Calendar.MONTH)+1)); // printing month
        map.put("Time", "Time :"+new Date().toString());  // printing Date
        alist.add(map);
        sd=new SimpleAdapter(this,alist,R.layout.rows,new String[]{"Date","Month","Time"},new int[]{R.id.t1,R.id.t2,R.id.t3});
        lv.setAdapter(sd);
        }
        catch (Exception e) {
            Log.w("Sravan", e.toString());
        }
    }
}
The main point here is that the SimpleAdapter requires the data to be in a List, where each entry is a Map. In my case, I simulate the columns by putting in three entries into each of the maps, and the maps each have the same keys. The adapter then maps the key values (like "Date") to the corresponding TextView (R.id.t1).
Putting the above pieces of code into Caltroid produces results that look like this:

Final Screen  For Complete Project Source code Click Here