Darpit
Darpit

Reputation: 33

Emulator not displaying image on button click android

Please bear with me on this I'm new to android programming. I was trying to show an image on a button click in android but unfortunately my emulator is not showing the required output. Here is my layout.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="14dp"
    android:text="Button" />

</RelativeLayout>

main_activity.java

package com.example.app1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
Button button;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
         }


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


public void addListenerOnButton() {

    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {


                  image.setImageResource(R.drawable.optimus);

        }

    });


}   

}

I have included the image file under /res/drawable-mdpi. When I try running on emulator it does not show image on button click. Is there any problem with the code?

Upvotes: 3

Views: 107

Answers (4)

JRad the Bad
JRad the Bad

Reputation: 511

Try This:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/background" <!-- you're adding this here -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="14dp"
    android:onClick="showImage" <!-- you're adding this here -->
    android:text="Button" />

</RelativeLayout>

Then in the .java file...

Package com.example.app1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
Button button;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
         }


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


public void showImage(View view) {
    RelativeLayout background = (RelativeLayout) findViewById(R.id.background);
    background.setBackgroundResource(R.drawable.optimus);
}   /* This is a whole new section of code to replace the onClick listener */

}

Since you're already using the XML Layouts, try to use the built in onClick listener whenever possible. You can do this by naming a function in your .java file called yourFunctionName with a single argument of (View view). Then, in your XML, you need to point to it by adding android:onClick="yourFunctionName" for the view that needs the onClick method.

Lastly, you never had the image you were referring to actually pointing to anything. You simply said that the object "image" now used "R.drawable.optimus" but that Image was never used! So instead, I just pointed to placing it in the background of your RelativeView. If instead you wanted it in a separate image, you'll have to add an ImageView to the XML file and set the background resource for the ImageView the same way.

Best of luck on learning Android!

Upvotes: 0

You have to write onclick method like this code and also you need to call addListenerOnButton method in the onCreate() method

package com.example.app1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
    Button button;

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

        addListenerOnButton();
     }


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


public void addListenerOnButton() {

    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {


              button.setBackgroundResource(R.drawable.optimus);

    }

    });


}   

}

Upvotes: 0

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

Assuming that you have an ImageView called imageView1,
You have to assign it, before using it.

So change this:

    @Override
    public void onClick(View arg0)
    {
        image.setImageResource(R.drawable.optimus);
    }

to this

    @Override
    public void onClick(View arg0)
    {
        image = (ImageView) findViewById(R.id.imageView1);
        image.setImageResource(R.drawable.optimus);
    }

Upvotes: 0

Iulian Buga
Iulian Buga

Reputation: 325

In your onCreate() method write this please :

Button theButton = (Button)findViewById(R.id.button1);
theButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

theButton.setBackgroundResource(R.drawable.optimus);
        }

Upvotes: 1

Related Questions