Thursday, March 1, 2012

Image control in android

This program is for Image control in Android.It will display one image from our project folder.

Step 1) code of your main java file is here


package toggle.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class Toggle_buttonActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final TextView tv=(TextView)findViewById(R.id.textView1);
        ImageButton img=(ImageButton)findViewById(R.id.imageButton1);
        //img.setOnKeyListener(null);
        img.setOnClickListener(new OnClickListener() {
                                               
                                                public void onClick(View arg0) {
                                                                // TODO Auto-generated method stub
                                                tv.setText("You have clicked over image");
                                                }
                                });
   }
}


Step 2) Code for your main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
/>
<ImageButton
 android:id="@+id/imageButton1"
 android:layout_width="wrap_content"
 android:src="@drawable/icon"
 android:layout_height="wrap_content">
 </ImageButton>


<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="wrap_content"
 android:layout_height="wrap_content">
</TextView>
</LinearLayout>

No comments:

Post a Comment