Monthly Archives: February 2012

Change appearance of button/imageview/imagebutton in android for different states

“States” here, I mean the press/focus states of control. We have normal state, on-focusing state and pressed-state. ( I used these, and don’t care about others ).
I find an interesting post at

Just re-post in my blog for later use.
Step 1 : Add images to resource.
button_normal_green.png – Default image button.
button_focused_orange.png – Display when button is focused, for example, when phone’s keypad is move (focus) on this button.
button_pressed_yellow.png – Display when button is pressed.
Step 2: Now, create a new XML file in “res/drawable/” folder, in whatever name you want, in this case, we just give a name as “new_button.xml“. This file defined which button state is belong to which image.

Now, you can refer to this button via this Id : @drawable/new_button.

Step 3: Add Button
Open “res/layout/main.xml” file, add a normal button, and attach the background image to above “new_button” via “android:background=”@drawable/new_button”
File : res/layout/main.xml

Step 4: Code the main activity
A normal button with a simple click listener.

File :

import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends Activity {

Button imageButton;

public void onCreate(Bundle savedInstanceState) {



public void addListenerOnButton() {

imageButton = (Button) findViewById(;

imageButton.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

"ImageButton (selector) is clicked!",





Reference :

3/ Sample source code can be found here

MVC – Error#1

When working with MVC 2, one of the most favourite thing was the way MVC help us to route to our markup-pages ( .aspx pages).
If you follow the normal rule of MVC that will generate Views with name according to ActionName ( name of Actions in Controllers). You would not meet any problem with GET and POST.
Let me make this clear, for example you need a Delete action in PersonController, from this Action, you generate a View ( called Delete.aspx in View folder).
Now, you implement following code for [HttpGet] and [HttpPost] Delete methods like:
public ActionResult Delete(int id)
Person p = new Person();
foreach (Person pn in people)
if (pn.Id == id)
p.Name = pn.Name;
p.Age = pn.Age;
p.Id = pn.Id;
p.Phone = pn.Phone;
p.Email = pn.Email;

return View(p);

// POST: /Person/Delete/5

public ActionResult Delete(Person p)
foreach (Person pn in people)
if (pn.Id == p.Id)

return RedirectToAction("Index");

When you select Delete ActionLink in Index pages, you will be redirected to a confirming-page to ask if you really want to delete the item in list. This page look like this ( just resemble, not exactly, i take this picture from another sample ):

Let’s see what is that button ?

This means if you click this Delete button, a POST will be sent to Web server with parameter: ?productId=2

Now, how HttpRoutingModule works with this url, if not a well-formed url according to our definition :
Problem: our httppost Delete method will receive a junk Model object and our logic breaks !

So how to fix this, simply, in our http-post Delete method, we need to get the parameter that our browser sends in POST request by using request command like this:
int id = int.Parse(Request["productId"].ToString());

Then, do whatever you want with this id (like build an model object with this id…)

Ok, problem solved !
See ya.