Tuesday, January 15, 2013

How to Call a Image as a Button in Android

In this project how i call a image as a button . Then i use the  button and in this button i call the touch listener who call the image on the screen then you already use that code it is tested. You believe that.



package com.example.imagebutton;


import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Imagebutton extends Activity
{
ImageView ivCard;
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;
}


}

No comments:

Post a Comment