-2

I've been searching the forum but can't seem to find what I'm looking for... I've got a xml with some 40 buttons. Whenever a specific button is clicked I want to open a new activity and set the contentview depending on the button clicked. Opening the new activity is no problem but I can't figure out how to set the contentview to the right image...

My xml file:

<?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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_regular"
        android:layout_marginRight="@dimen/margin_regular"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/chromatic_scale_bflat" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_bes1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/bes"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_b1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/b"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_c1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/c"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_cis1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/cis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_d1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/d"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_dis1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/dis"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/e"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/f"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/fis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/g"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/gis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/a"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/chromatic_scale_bflat" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/bes"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/b"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/c"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/cis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/d"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/dis"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/e"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/f"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/fis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/g"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/gis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/a"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/chromatic_scale_bflat" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/bes"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/b"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/c"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/cis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/d"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/dis"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/e"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/f"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/fis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/g"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/gis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/a"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

</LinearLayout>

My Java file:

package com.example.saxofoonnieuw;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FingeringChart extends Activity implements OnClickListener {

    Button bes1, b1, c1;

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

        bes1 = (Button) findViewById(R.id.btn_bes1);
        bes1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_bes1:
            Intent a = new Intent(this, PictureFingeringChart.class);
            startActivity(a);

            break;

        default:
            break;
        }
    }

}
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Ruud
  • 9
  • 3

5 Answers5

1

You should add Extra info in your Intent as is:

In your code pass an image_name String variable into your intent:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_bes1:
        Intent a = new Intent(this, PictureFingeringChart.class);
        String strName = "image_2"; // this variable changes for each button
        a.putExtra("image_name", strName);
        startActivity(a);

        break;

    default:
        break;
    }
}

Now get the variable name in your destination activity:

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

    Intent intent = getIntent();
    String image_name = intent.getStringExtra("image_name");

    // now you have the image_name and you can go on... 

    // 1st get the reference to your ImageView :
    ImageView img = (ImageView) findViewById(R.id.image);
    // 2nd assume you pass as string param the name of the drawable, in this example, regarding previous code, we would like to load image_2.png, so we retrieve the corresponding resource ID like this :
    int resourceID = getResources().getIdentifier(image_name , "drawable", getPackageName());
    // 3rd then we assign it to our ImageView :
    img.setImageResource(resourceID);
    // 4th done!
}
JBA
  • 2,889
  • 1
  • 21
  • 38
  • This solution looks the most familiar to me, but I still can't figure out how to set the imageview to the image in the 'drawable' folder. In other words: what do I code where you write "// now you have the image_name and you can go on...". – Ruud Feb 18 '15 at 14:24
1

modify your calling activity

Activity1

 //initial part
     @Override
    public void onClick(View v) {
     Intent a = new Intent(this, PictureFingeringChart.class);
    switch (v.getId()) {
    case R.id.btn_bes1:

        a.putExtra("outputtodisplay",R.layout.viewToDisplay);


        break;

    default:
        break;
    }
    startActivity(a);
}

and in your called activity

//activity 2

protected void onCreate(){
        super.onCreate()
setContentView(a.getIntExtra("outputtodisplay",R.layout.default_layout),

}
Droidekas
  • 3,464
  • 2
  • 26
  • 40
0

you can pass a bundle to your activity

bundle.putByte("info", 1);
startActivity(new Intent(this, PictureFingeringChart.class).putExtras(bundle));

in your onCreate method of PictureFingeringChart.class

Bundle bundle = this.getIntent().getExtras();
        byte info = bundle.getByte("info");
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
0

modify this:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_bes1:
        Intent a = new Intent(this, PictureFingeringChart.class);
        int img_ID = R.id.your_image;
        a.putExtra("EXTRA_ID", img_ID);
        startActivity(a);
        break;

    default:
        break;
    }
}

in PictureFingeringChart.class :

 @Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Bundle extras = getIntent().getExtras();
  if (extras != null) {
   int id = extras.get("EXTRA_ID");
   if (id!= null) {
        // setContentView or set your image here
   }        
}

and since you are calling different activities every time, you can have different layout files for the respective activity.

dvs
  • 644
  • 5
  • 14
  • if it solves your problem..do check this answer as correct..thank you, have a good day :) – dvs Feb 18 '15 at 07:10
0

Try this out...

FingeringChart.java

public static final IMAGE="package-name image id";
public class FingeringChart extends Activity implements OnClickListener {

Button bes1, b1, c1;

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

    bes1 = (Button) findViewById(R.id.btn_bes1);
    bes1.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_bes1:
        Intent a = new Intent(this, PictureFingeringChart.class);
        intent.putExtra(IMAGE,R.drawable.image1);
        startActivity(a);

        break;

    default:
        break;
    }
}

}

PictureFingeringChart.java

public class Test extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int image = getIntent().getIntExtra(FingeringChart.IMAGE,0);
    ImageView imageView = new ImageView(this);
    imageView.setImageDrawable(getResources().getDrawable(image));
    setContentView(imageView);
}
}

Hope it helps...

Fabin Paul
  • 1,701
  • 1
  • 16
  • 18