Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add ImageView to the Layout

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 278
    Comment on it

    How to add imageview to the layout and change the source of the image with the click of the button.

    This is a very simple android code in which we set the mage view in the layout and then use the the imageview to set the new source.This change in the source of the image is set on the click of the button.

    Code for Layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout&#95;width="match&#95;parent"
        android:layout&#95;height="match&#95;parent"
        android:paddingBottom="@dimen/activity&#95;vertical&#95;margin"
        android:paddingLeft="@dimen/activity&#95;horizontal&#95;margin"
        android:paddingRight="@dimen/activity&#95;horizontal&#95;margin"
        android:paddingTop="@dimen/activity&#95;vertical&#95;margin"
        tools:context=".MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout&#95;width="wrap&#95;content"
            android:layout&#95;height="wrap&#95;content"
            android:layout&#95;alignParentTop="true"
            android:layout&#95;centerHorizontal="true"
            android:layout&#95;marginTop="158dp"
            android:text="Button" />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout&#95;width="wrap&#95;content"
            android:layout&#95;height="wrap&#95;content"
            android:layout&#95;alignLeft="@+id/button1"
            android:layout&#95;alignParentTop="true"
            android:layout&#95;alignRight="@+id/button1"
            android:layout&#95;marginTop="54dp"
            android:src="@drawable/ic&#95;launcher" />
    
    </RelativeLayout>
    

    Now put the new image that you want to change in the drawable folder in the res.

    Code for MainActivity:

    package com.example.imageset;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity&#95;main);
            final ImageView image=(ImageView)findViewById(R.id.imageView1);
            Button change=(Button)findViewById(R.id.button1);
            change.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    image.setImageResource(R.drawable.index);
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: