Friday, January 18, 2013

Implement A Image Listener In Android

In the ANDROID development you use this code. It's helpful for you. If you use any image like button then by java coding you use that implement. It is call a image listener. Firstly you have a project in android application who called package com.example.imagebutton (example) then you export these things like Bundle, Activity, Menu, Motion event (Because on touch you call the listener) , View (for image) , On touch listener and etc. Then u use that Java code and enjoy you first app using image listener.




package com.example.imagebutton;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Imagebutton extends Activity {

ImageView ivCard;
TextView editText;
boolean isPressed=false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagebutton);

addListenerOnImage();

}


public void addListenerOnImage()
 {
// TODO Auto-generated method stub
ivCard=(ImageView)findViewById(R.id.imageView1);

ivCard.setOnTouchListener(new OnTouchListener()
{

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(isPressed){
ivCard.setBackgroundResource(R.drawable.ic_launcher);

}
else{
ivCard.setBackgroundResource(R.drawable.cancelglow);
Toast.makeText(Imagebutton.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
isPressed=!isPressed;
return false;
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_imagebutton, menu);
return true;
}


}