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;
}


}

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;
}


}

How to Draw a Image on Android

Hello friends Its very use full code for you when you are starting learner in android tutorial. Because you  never start without How to draw a image on android.

First you create a project Camera testing in Eclipse

Go to file section and click on New Android Project


And fill the section.

Then work on activity.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

<ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

<ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

<ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />



</LinearLayout>/


Now you come on the his MainActivity. just simple click on his main activity and to call image on onCreate activity and simple you are done your first project in android.

  package com.example.image
import android.app.Activity;
import android.widget.ImageView;


public class MainActivity extends Activity 
 {
    ImageView[] ivImage = new ImageView[4];



@Override
protected void onCreate(Bundle savedInstanceState) 
{
           ivImage[0]=(ImageView)findViewById(R.id.imageView1);
   ivImage[1]=(ImageView)findViewById(R.id.imageView2);
   ivImage[2]=(ImageView)findViewById(R.id.imageView3);
   ivImage[3]=(ImageView)findViewById(R.id.imageView4);
}


@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_main, menu);
return true;
}

  }