0

(My first post, btw)

My problem is so newbie-esque that I can't find anyone who has asked it and oh have I tried. So here goes..:

My main screen has an imageview. Click it and it sends you to a different layout from where a bunch of images (imagebuttons) can be selected (works fine).

Pressing any of these imagebuttons should send that image to my main imageview (crashing)

I made an imageview inside this layout where the ImButtons are and sent the images there instead. This is working fine so the switch statements are ok.

I have tried to make my main imageview a "public" in my main.java but still crashing.

So the question: How do I send an image from one layout/class to another?

Grateful in advance.

Jakob

Some code:

My secondary layout:

package egen.helt.min;

import android.app.Activity;``
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;

public class FartSelect extends Activity implements OnClickListener {

    public ImageView VælgMax;
    MediaPlayer mpButtonClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fartselect);

        VælgMax = (ImageView) findViewById(R.id.ivValgtMax);
        ImageButton skilt30 = (ImageButton) findViewById(R.id.ib30s);
        ImageButton skilt40 = (ImageButton) findViewById(R.id.ib40s);
        ImageButton skilt50 = (ImageButton) findViewById(R.id.ib50s);
        ImageButton skilt60 = (ImageButton) findViewById(R.id.ib60s);
        ImageButton skilt70 = (ImageButton) findViewById(R.id.ib70s);
        ImageButton skilt80 = (ImageButton) findViewById(R.id.ib80s);
        ImageButton skilt90 = (ImageButton) findViewById(R.id.ib90s);
        ImageButton skilt100 = (ImageButton) findViewById(R.id.ib100s);
        ImageButton skilt110 = (ImageButton) findViewById(R.id.ib110s);
        ImageButton skilt120 = (ImageButton) findViewById(R.id.ib120s);
        ImageButton skilt130 = (ImageButton) findViewById(R.id.ib130s);

        skilt30.setOnClickListener(this);
        skilt40.setOnClickListener(this);
        skilt50.setOnClickListener(this);``
        skilt60.setOnClickListener(this);
        skilt70.setOnClickListener(this);
        skilt80.setOnClickListener(this);
        skilt90.setOnClickListener(this);
        skilt100.setOnClickListener(this);
        skilt110.setOnClickListener(this);
        skilt120.setOnClickListener(this);
        skilt130.setOnClickListener(this);

    }

    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.ib30s:
            VælgMax.setImageResource(R.drawable.skilt30s);
            break;
        case R.id.ib40s:
            VælgMax.setImageResource(R.drawable.skilt40s);
            break;
        case R.id.ib50s:
            VælgMax.setImageResource(R.drawable.skilt50s);
            break;
        case R.id.ib60s:
            VælgMax.setImageResource(R.drawable.skilt60s);
            break;
        case R.id.ib70s:
            VælgMax.setImageResource(R.drawable.skilt70s);
            break;
        case R.id.ib80s:
            VælgMax.setImageResource(R.drawable.skilt80s);
            break;
        case R.id.ib90s:
            VælgMax.setImageResource(R.drawable.skilt90s);
            break;
        case R.id.ib100s:
            VælgMax.setImageResource(R.drawable.skilt100s);
            break;
        case R.id.ib110s:
            VælgMax.setImageResource(R.drawable.skilt110s);
            break;
        case R.id.ib120s:
            VælgMax.setImageResource(R.drawable.skilt120s);
            break;
        case R.id.ib130s:
            VælgMax.setImageResource(R.drawable.skilt130s);
            break;

        }

    }
}

And here is FartSelect.xml:

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



    <ImageView
        android:id="@+id/ivValgteMax"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/blanktskiltsk" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="400dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib30s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt30s" />

                <ImageButton
                    android:id="@+id/ib40s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt40s" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib50s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt50s" />

                <ImageButton
                    android:id="@+id/ib60s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt60s" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib70s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt70s" />

                <ImageButton
                    android:id="@+id/ib80s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt80s" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib90s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt90s" />

                <ImageButton
                    android:id="@+id/ib100s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt100s" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib110s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt110s" />

                <ImageButton
                    android:id="@+id/ib120s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt120s" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib130s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt130s" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib30sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt30sk" />

                <ImageButton
                    android:id="@+id/ib40sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:background="@android:color/black"
                    android:gravity="center"
                    android:src="@drawable/skilt40sk" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib50sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt50sk" />

                <ImageButton
                    android:id="@+id/ib60sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt60sk" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib70sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt70sk" />

                <ImageButton
                    android:id="@+id/ib80sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt80sk" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib90sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt90sk" />

                <ImageButton
                    android:id="@+id/ib100sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt100sk" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib110sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt110sk" />

                <ImageButton
                    android:id="@+id/ib120sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt120sk" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib130sk"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt130sk" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib30"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt30" />

                <ImageButton
                    android:id="@+id/ib40"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt40" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib50"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt50" />

                <ImageButton
                    android:id="@+id/ib60"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt60" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib70"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt70" />

                <ImageButton
                    android:id="@+id/ib80"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt80" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib90"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt90" />

                <ImageButton
                    android:id="@+id/ib100"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt100" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib110"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt110" />

                <ImageButton
                    android:id="@+id/ib120"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt120" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/ib130"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:src="@drawable/skilt130" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

Within the same layout the imageview can change. Just not when sent to an imageview on main layout.

ThorDenSky
  • 51
  • 4

4 Answers4

1

You're not supposed to sent images between activities, if that's what you mean. You could probably do it, but it would definitely be slow through something like an intent. From what I understand, you want to go to Activity B and then click an ImageButton, then return back to Activity A and have that displayed in the ImageView. Couldn't you just pass in the path of the image through an intent (i.e. as a String)? Alternatively, you could just pass in the name as a String and then display that image in the next Activity by setting the source of the ImageView in Activity A.

If the image was created temporarily or something, and you needed to pass it, you could just save it to a private file (MODE_PRIVATE) and then access it in the next activity.

Sorry if I'm thinking something off topic.

EDIT

Look at this for more information.

EDIT 2

You can use intents to pass Strings between activities: (i.e. the image path/name)

String myImageName = "image1";
Intent picIntent = new Intent(this, NextActivity.class);
picIntent.putExtra("name", myImageName); //this has to correspond to the below name
startActivity(intent);

You can retrieve it in the next activity:

String thePictureName = getIntent().getStringExtra("name"); //correspond to above name
Community
  • 1
  • 1
Kgrover
  • 2,113
  • 2
  • 35
  • 54
  • You got my problem right but I am very new and passing paths or names as strings is not yet on my repetoire. – ThorDenSky Jun 27 '12 at 23:27
  • Ah, okay. Just use an intent...it has very easy syntax. (see the link for intent above). I will also edit my answer on how to use intents to pass Strings from one activity to another. – Kgrover Jun 27 '12 at 23:28
  • Thanks a bunch. I'll try. Probably taking some time. Thanks for answers. – ThorDenSky Jun 27 '12 at 23:39
  • No problem. Tell me if you need anymore help. – Kgrover Jun 28 '12 at 03:09
0

You don't send an ImageView from one layout to another. You may either,

  1. Remove the ImageView on layout and add to the other layout (by inflating).
  2. Hide on first layout and show on other layout.
dcanh121
  • 4,509
  • 10
  • 34
  • 80
0
  1. Set up an intent i for the FartSelector
  2. startActivity(i);
  3. Create an OnActivityResult(int result_code) and check for the FartSelector's code
  4. In fart selector, pack a new intent with the desired image data on click
  5. Call setResult(newIntent); and finish(); after creating and packing the intent
  6. Picking up where we left off at 3, unpack the intent and use it in your ImageView.
Jack Satriano
  • 1,989
  • 1
  • 12
  • 16
  • Ehh. I am really, really new at this. A couple of weeks to learn xml, java and android. Still learning by watching code. Can't figure out your solution. The intent – ThorDenSky Jun 27 '12 at 23:46
  • Intents are a fundamental part of the Android Framework; I recommend you do some reading on the subject – Jack Satriano Jun 27 '12 at 23:53
0

Your problem is this

VælgMax = (ImageView) findViewById(R.id.ivValgtMax);

ivValgtMax is not an id that is defined in fartlayout therefore that code will give you null. I assume ivValgtMax refers to an ImageView from another layout.

If I understand you correctly, you want to launch the Activity B (which has the images) from another Activity A (which has the main ImageView), select an image while in Activity B, and return the selected image to Activity A after the user has clicked.

If all of this is correct, we can proceed. The way you want to launch Activity B from A is to use something like

Intent intent = new Intent(this, ActivityB.class);    
startActivityForResult(intent, IMAGE_SELECTION_VALUE);

where IMAGE_SELECTION_VALUE is a static value that you assign an integer to, just make it something unique.

In Activity B, once the user finishes clicking, your goal is to return the selection to Activity A. To do this, you need to create an intent and set the result.

public void onClick(View v) {
    ... your code ...
    Intent resultIntent = new Intent(null);
    resultIntent.putExtra(IMAGE_FIELD_NAME, userSelection);
    setResult(Activity.RESULT_OK, resultIntent);
    finish();
}

In the above code, IMAGE_FIELD_NAME will be a static String that you can use to find the correct image (we will find how to use it in the next step). userSelection will be the data that you send back to Activity A. You can use v.getId() for this and move your switch statement to Activity A.

Lastly, you will want to override onActivityResult in Activity A. This will allow you to get the result from Activity B and is where we will use IMAGE_FIELD_NAME.

public void onActivityResult(int requestCode, int resultCode, Intent data) {     
    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode) { 
        case IMAGE_SELECTION_VALUE:
            if (resultCode == Activity.RESULT_OK) { 
                int imageId = data.getIntExtra(IMAGE_FIELD_NAME);
                // now you can update your ImageView here
            }
            break; 

        default: 
            break;
    }
} 
cklab
  • 3,601
  • 18
  • 29
  • Wow. Thanks a bunch! Java is quite daunting. A lot of possibilities and conventions (and a lot of parenthesies!). Mixed with android >= 10GB of stuff to learn. I didn't know how to fetch. I learned a lot from this code. I think I understand the concepts of the other replies I just don't know how to actually write it out yet. How long does it usually take to learn JAVA (at least the basics)? – ThorDenSky Jun 28 '12 at 03:46
  • Sure thing, glad I could help. I can't really say there is a time frame to learn Java, really; even for the basics. I've been doing Java for about 7-8 years now and Android for a little over a year, I'm learning new things almost every day just by browsing SO (even things I might consider to be "basic".) Not to mention things I learn on the job and in my independent projects! – cklab Jun 28 '12 at 04:30