Tuesday, October 19, 2010

Using a GridView to layout your app screen.

 

For many of us coming from other platforms to android development the idea of using an xml definition sheet to plan your display may feel very foreign, or maybe it just me,  whether you are coming from web development or using a development tool such as Visual Studio the ability to place your buttons or controls in a nice formatted layout can be a tricky one. 

For my first android application I have decided to use androids GridView as a way to place mock buttons in a nice formatted fashion on to my screen rather then using androids button control I will use the GridView control to layout a pattern of images which will act like buttons for my application.

First let me recommend reading Android Developers GridView Tutorial 'Hello Views'.  This is what I used for the base layout of my application.  Using the public ImageAdapter provided in the tutorial I add a listener to the click event to launch 4 separate activities tied to the 4 images I used as mock buttons.

GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { switch (position) { case 0: Intent myIntent = new Intent(v.getContext(), MyClass0.class);startActivityAct0(myIntent, 0);break; case 1: Intent myIntent1 = new Intent(v.getContext(), MyClass1.class);startActivityAct1(myIntent1, 0);break; case 2: Intent myIntent2 = new Intent(v.getContext(), MyClass2.class);startActivityAct2(myIntent2, 0);break; case 3: Intent myIntent3 = new Intent(v.getContext(), MyClass3.class);startActivityAct3(myIntent3, 0);break; } } });

Now when any of the 4 images are clicked by the user it will launch into a new activity.

Hope this helps.

1 comment:

  1. I needed to visit and allow you to know how lucky I liked identifying your web blog today. I’d recognize it the honor to operate at my place of work and be able to operate on the information discussed on your website and also be involved in visitors responses like this.

    The Great Healthcare Website Design Company America, Optimized360 designed unique and eye-catching websites for all kind of generic practitioners.

    ReplyDelete