Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically change the ImageButton src target when a condition is met?

I have a school project where I am trying to get a flashlight app going. For the on/off ImageButton, I want to have 4 custom images.

If flashlight is off: -turn_on.png (default) -turn_on_pressing.png (state pressed = true)

If flashlight is on: -turn_off.png (default) -turn_off_pressing.png (state pressed = true)

I am hoping to change the ImageButton src target to change between "on_selector.xml" and "off_selector.xml". Originally I was able to setImageResource() to change the default button images. And now that I am trying to add my own on press images, I am having a very difficult time.

Application.java:

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.view.View;
import android.widget.ImageButton;
import com.jrr.flashlight.R;

public class Application extends Activity {

    ImageButton flashControl;
    Camera camera;
    Parameters params;

    private boolean hasFlash;
    private boolean isFlashOn = false;


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

        flashControl = (ImageButton) findViewById(R.id.flashButtonOn);
        flashControl.setOnClickListener(flashControlListener());

        hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);  //sets value for boolean hasFlash

    }


    private View.OnClickListener flashControlListener() {
        return new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (!isFlashOn){
                    findViewById(R.id.appbackground).setBackgroundColor(Color.WHITE);

                    if(hasFlash){
                        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        camera.setParameters(params);
                        camera.startPreview();
                    }
                    isFlashOn = true;
                } else {
                    findViewById(R.id.appbackground).setBackgroundColor(Color.BLACK);

                    if(hasFlash){
                        params.setFlashMode(Parameters.FLASH_MODE_OFF);
                        camera.setParameters(params);
                        camera.stopPreview();
                    }
                    isFlashOn = false;
                }

            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();    
        camera = Camera.open();
        params = camera.getParameters();

    }

    @Override
    protected void onPause() {
        super.onPause();    

        if(camera!=null) {
            camera.release();
            camera = null;
        }
        findViewById(R.id.appbackground).setBackgroundColor(Color.BLACK);
    }

application_layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/appbackground"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="0dp"
              android:layout_margin="0dp"
              android:background="#000000">

    <ImageButton
            android:background="#80000000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            <!--I want to be able to change the following line-->
            android:src="@drawable/on_selector"
            android:id="@+id/flashButtonOn" android:layout_gravity="center"/>
</LinearLayout>

on_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/turn_on_pressing" /> <!-- pressed -->
    <item android:drawable="@drawable/turn_on" /> <!-- default -->
</selector>

off_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/turn_off_pressing" /> <!-- pressed -->
    <item android:drawable="@drawable/turn_off" /> <!-- default -->
</selector>

Is what I want to do even possible or is there another way to accomplish this? I have been stuck on this for about 3 hours and cannot find the answer online anywhere (maybe not asking the right question?) so any help is appreciated!

Thanks!

like image 888
John Riggs Avatar asked Jun 16 '13 02:06

John Riggs


People also ask

Which attribute in XML code can be used to place an image on the surface of a button control?

By default, an ImageButton looks like a regular Button , with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the <ImageButton> XML element or by the ImageView. setImageResource(int) method.

Which tag helps you draw Image button on activity?

android:src This sets a drawable as the content of this ImageView.

What is Android ImageButton?

Android ImageButton is a user interface widget which is used to display a button having image and to perform exactly like button when we click on it but here, we add an image on Image button instead of text. There are different types of buttons available in android like ImageButton, ToggleButton etc.


3 Answers

Is it a requirement that it has to be imageResource? You can try using backgroundResource instead.

ImageButton flashButtonOn = (ImageButton) findViewById(R.id.flashButtonOn);
flashButtonOn.setBackgroundResource(R.drawable.on_selector);

Or try

flashButtonOn.setImageResource(R.drawable.replacementGraphic);
like image 77
Chor Wai Chun Avatar answered Oct 17 '22 05:10

Chor Wai Chun


It should be "setImageResource", "setBackgroundResource" changes the background, not the src. btn_MyButton.setImageResource(R.drawable.button_bg_selector_collapsed);

like image 30
Sourangshu Biswas Avatar answered Oct 17 '22 06:10

Sourangshu Biswas


When setting the image using the following attribute:

android:src="@drawable/on_selector"

You will need to use the following to change the image dynamically:

ImageButton myButton = (ImageButton) findViewById(R.id.my_button);
myButton.setImageResource(R.drawable.my_button_image);
like image 34
Proxy32 Avatar answered Oct 17 '22 05:10

Proxy32