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