Code to add a Layout to ListView Header ,in Android

This below code also covers,how to place create Expandable Edittext view having dotted lines in between them..
means,the edittext will be expanded based on the user entered text in EditText...
This code i used in AddJobScreen of my SnailMail Project..

ListViewHeader.java:-
package org.Dharani.Dash_Lines;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewHeader extends Activity {
    ListView listViewHeader;
    FileListAdapter fileAdapter;
    String str[]={"Pavan Tilak","Rajeshh ","Umbrella corner","Foot Print"};
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);
       
        listViewHeader = (ListView) findViewById(R.id.listViewHeader);
       
       
        //Here we add a layout to listview header
        LayoutInflater inflater = getLayoutInflater();
        ViewGroup header = (ViewGroup)inflater.inflate(R.layout.dash_line, listViewHeader, false);
        listViewHeader.addHeaderView(header, null, false);
       
        fileAdapter=new FileListAdapter(ListViewHeader.this,str);
        listViewHeader.setAdapter(fileAdapter);
    }
   
   
      public class FileListAdapter extends BaseAdapter
        {       
            ViewHolder holder = null;
            String arr[];
           
            @Override
            public int getCount() {
                int count=0;
                try {
                     count=str.length;
                     System.out.println("----lengthg--"+count);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return count;
            }

            Activity context;

            public FileListAdapter(Activity context,String filesList[]) {
                super();           
                this.context = context;
                this.arr=filesList;
            }

             class ViewHolder {
               
                 TextView txtView;               
            }

            public View getView(final int position, View convertView, ViewGroup parent){
               
                View rowView = convertView;           
               
                if (rowView == null) {
                   
                    LayoutInflater inflater = context.getLayoutInflater();           
                    rowView = inflater.inflate(R.layout.simple_layout, null, true);
                    holder = new ViewHolder();               
                    holder.txtView=(TextView) rowView.findViewById(R.id.txtView);
                   
                    rowView.setTag(holder);
                }
                else
                {
                    holder = (ViewHolder) rowView.getTag();
                }           
               
                holder.txtView.setText(arr[position]);
                return rowView;
            }

            @Override
            public long getItemId(int position) {
                return position; 
            }

            @Override
            public Object getItem(int position) {
                return arr[position];
               
            }   
       

        }

}

list_view.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="@color/white"
    android:orientation="vertical" >
   
    <ListView
    android:id="@+id/listViewHeader"
    android:paddingTop="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#FFFFFF"
    android:dividerHeight="1px"/>
   
    </LinearLayout>

dash_line.xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">
  
                    <org.Dharani.Dash_Lines.LinedEditText 
                        android:id="@+id/fromBodyEdtTxt1"
                        android:gravity="left"
                        android:minHeight="150dip"
                        android:layout_width="50dip"
                        android:layout_height="wrap_content"
                        android:textColor="@color/redColor"
                        android:background="@color/transparent"
                        android:focusable="false"
                        android:clickable="false"
                        android:hint="Body"
                         />
                      <org.Dharani.Dash_Lines.LinedEditText 
                        android:id="@+id/frommiddleNameEdtTxt"
                        android:minHeight="150dip" 
                        android:gravity="left"
                        android:layout_toRightOf="@+id/fromBodyEdtTxt1"                                         
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:textColor="@color/redColor"
                        android:background="@color/transparent"                       
                        android:hint="Body"                       
                         />

    </RelativeLayout>


LinedEditText.java:-

package org.Dharani.Dash_Lines;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

public class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.GRAY);
        PathEffect effects = new DashPathEffect(new float[]{3,3,3,3},1); 
        mPaint.setPathEffect(effects);
    }

    /*@Override
    protected void onDraw(Canvas canvas) {//works good..
        int left = getLeft();
        int right = getRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int height = getHeight();
        int lineHeight =getLineHeight();
        System.out.println("--------lineHeight--------------"+lineHeight);
        int count =(height-paddingTop-paddingBottom) / lineHeight;

        for (int i = 0; i < count; i++) {
            int baseline = lineHeight * (i+1) + paddingTop-20;
            canvas.drawLine(left+paddingLeft, baseline, right-paddingRight, baseline, mPaint);
            //canvas.drawLine(35, 0, 0, 35, mPaint); //this is a second line that will cross the first line
           
        }
        super.onDraw(canvas);
    }*/
   
   
   
   
    @Override
    protected void onDraw(Canvas canvas) {//works good..
        //int count = getLineCount();

        int height = getHeight();
        Dash_LinesActivity.textHeight=height;
        int line_height = getLineHeight();
        Dash_LinesActivity.lineHight=line_height;
        int count = height / line_height;

        if (getLineCount() > count)
            count = getLineCount();

        Rect r = mRect;
        Paint paint = mPaint;

        int baseline = getLineBounds(0, r);//first line
        for (int i = 0; i < count; i++) {
           
            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            //canvas.drawText("Hello, World", 30, 30, paint);
            baseline += getLineHeight();//next line
        }

        super.onDraw(canvas);
    }
   
   
    /*private Paint mPaint = new Paint();

    public LinedEditText(Context context) {
        super(context);
        initPaint();
    }

    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initPaint();
    }

    private void initPaint() {
        mPaint.setStyle(Paint.Style.STROKE);        
        mPaint.setColor(0x80060000);
    }

    *//**
     * This is called to draw the LinedEditText object
     * @param canvas The canvas on which the background is drawn.
     *//*
    @Override protected void onDraw(Canvas canvas) {
        int left = getLeft();
        int right = getRight();
        int paddingTop = getPaddingTop()+20;
        int paddingBottom = getPaddingBottom();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int height = getHeight();
        int lineHeight = getLineHeight();
        int count =(height-paddingTop-paddingBottom) / lineHeight;

        for (int i = 0; i < count; i++) {
            int baseline = lineHeight * (i+1) + paddingTop;
            canvas.drawLine(left+paddingLeft, baseline, right-paddingRight, baseline, mPaint);
        }
        super.onDraw(canvas);
    }*/
    /*@Override
    protected void onDraw(Canvas canvas)
    {
        int height = canvas.getHeight();
        int curHeight = 20;
        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);
        for (curHeight = baseline + 1; curHeight < height;
                                                 curHeight += getLineHeight())
        {
            canvas.drawLine(r.left, curHeight, r.right, curHeight, paint);
        }
        super.onDraw(canvas);
    }*/
}




Comments

Popular posts from this blog

Custom Dialog with no title and full screen in Android

Best working code to set Date limits on Date Picker Dialog,in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard