Jun 19, 2011

Android Grid Border


For who are just searching for a GridView type actually to be more specific dynamic GridView type example in android I will show you how to do it by drawing a Canvas and put it with awesome look and feel. Here is the code:
public class CustomeTblLayout extends TextView{
 public CustomeTblLayout(Context context, AttributeSet attrs) {
  super(context, attrs);
 }
 @Override
 public void onDraw(Canvas canvas){
  canvas.drawColor(Color.WHITE);
  Paint mLinePaint = new Paint();
  Paint mMarginPaint = new Paint();
  canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), mLinePaint);
  canvas.drawLine(15, 0, 15, getMeasuredHeight(), mMarginPaint);
  canvas.save();
  canvas.translate(20, 10);
  super.onDraw(canvas);
  canvas.restore(); 
 }
}
Now For the XML Layout you need to add the following:

    
   
   


And then create an XML to show the Custom TextView you have just created:

  

  
  

  


And write the Main Activity:
Add The Activity Class for the Main XML:
public class TestingActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView appointmentHistory = (ListView) findViewById(R.id.chu);
        ArrayList< Map< String,String > > appntmntHistoryList = new ArrayList< Map  >();
        Map  mpStr = new HashMap < String, String >();
        mpStr.put("date", "20/10/08");
        mpStr.put("amount", "200");
        appntmntHistoryList.add(mpStr);
        mpStr = new HashMap < String, String >();
        
        mpStr.put("date", "12/05/11");
        mpStr.put("amount", "45");
        appntmntHistoryList.add(mpStr);
        
        SimpleAdapter adapter = new SimpleAdapter(this,appntmntHistoryList,R.layout.list_items,new String[] {"date","amount"},new int[] {R.id.customId,R.id.customId2});
  appointmentHistory.setAdapter(adapter);
        
    }
}
After that, you can see a Dynamic Android Grid Border.

4 comments:

  1. Nice tutorial. Not so lengthy. By the way remove the (dot) from your codes.

    ReplyDelete
  2. Would be nice to at least include a screenshot with your post.

    ReplyDelete
  3. Hi

    I'm a newbie to Monodroid using c# and have no experience in Java. Can anyone give me a C# monodroid example which will achieve the same results?

    ReplyDelete