I am new to Android Studio and Java and have been working on a project for some time now and would appreciate help with an issue I cannot seem to find any tutorials or information on. I am using android Studio version 3.1.4 on a computer running Windows 7 pro .
I cannot fit all my buttons and all my drawables onto the MainActivity
, so I want all my buttons on the MainActivity
and all my drawables on Activity 2.
My Problem is that I cannot work out how to make my drawables change color in the second Activity when I click any buttons in the MainActivity
.
My drawables in the form of circles are in svg format with a diameter of 10mm I have pasted all relevant code for 3 buttons to work with 3 svg drawable on the MainActivity
so that you can see how it works on a virtual device or your mobile connected via a usb cable. I have also added a button in the main activity to take you to Activity 2 where I have put 3 drawable that I need to work in the same way as the main activity but with the buttons from the first activity changing them. In my project I have many buttons. Each button when clicked changes the color of its related SVG drawable as per my program. The drawable are black to start with. Each click will make them change to the next colors in this sequential order. White, Yellow, Orange, Red, then back to black
So what I need to do is have my buttons in the main activity and the drawables in activity 2. If Button 1 (attribute is btn1) is clicked in a ctivity_main.xml
I want the button information passed to Activity2
to make the drawable with attribute iv1
change color.
So if you were to click Button1
, Button2
, Button3
, Button4
and use the go to activity2
button (attribute btn) the drawable in Activity2
would be white, yellow, white from left to right.
Thanks in advance for any help.
<package com.example.chucky.svg;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button button;
int[] colors = {0xffffffff, 0xffffff00, 0xffff6600, 0xffff0000, 0xff000000};
int counter1 = -1;
int counter2 = -1;
int counter3 = -1;
ImageView iv1;
ImageView iv2;
ImageView iv3;
Button btn;
Button btn1;
Button btn2;
Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivity2();
}
});
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter3++;
if (counter3> colors.length -1){
counter3 = 0;
}
iv3.setColorFilter(colors[counter3]);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter2++;
if (counter2> colors.length - 1){
counter2 = 0;
}
iv2.setColorFilter(colors[counter2]);
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter1++;
if (counter1> colors.length - 1){
counter1 = 0;
}
iv1.setColorFilter(colors[counter1]);
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
Activity2
class
package com.example.chucky.svg;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}
Activity_main
xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text=" Button 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.091"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button 3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.895"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Go to Activity 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.087" />
<ImageView
android:id="@+id/iv1"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="@drawable/ic_circle_e" />
<ImageView
android:id="@+id/iv2"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="@drawable/ic_circle_e" />
<ImageView
android:id="@+id/iv3"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.852"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="@drawable/ic_circle_e" />
</android.support.constraint.ConstraintLayout>
activity_2
xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity2">
<ImageView
android:id="@+id/iv3"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.852"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="@drawable/ic_circle_e" />
<ImageView
android:id="@+id/iv2"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="@drawable/ic_circle_e" />
<ImageView
android:id="@+id/iv1"
android:layout_width="56dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296"
app:srcCompat="@drawable/ic_circle_e" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Activity2"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
What you need to do is to pass an information to the second activity, extract, and make use of the information.
With an Intent Extra your goal can be achievable just like this:
First of all, declare a unique constant variable in your second activity that will hold information across the communication.
public static final String EXTRA_USER_ID = "userId";
Now, FirstActivity
sending info:
String changeColorToBlack = " black"
Intent intent = new Intent(firstActivty.this, SecondActivity.class);
intent.putExtra(SecondActivity.EXTRA_COLOR_KEY, changeColorToBlack);
startActivity(intent);
SecondActivity
can retrieve the information like this
String colorIdKey = getIntent().getStringExtra(EXTRA_COLOR_ID);
if (colorIdKey == null) {
throw new IllegalArgumentException("Must pass EXTRA_COLOR_KEY");
}
//colorIdKey is your color
That is all. All you need is an Intent Extra to pass information between 2 activities
The easiest way to active what you required is pass the color codes to second activity as arguments
in Activity1.class do this
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
// pass the color value as bundle
intent.putExtra("color1", <color_code1>); intent.putExtra("color2", <color_code2>); intent.putExtra("color3", <color_code3>); startActivity(intent); }
in Activity2.class
public class Activity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_2); Bundle extras = getIntent().getExtras(); if(extras != null){ String color1 = extras.getString("color1"); String color2 = extras.getString("color2"); String color3 = extras.getString("color3"); // set values to image views here } } }
Official documentation here : https://developer.android.com/guide/components/activities/parcelables-and-bundles#kotlin
You have colour codes in integer array
int[] colors
and selected position incounter
variable. By the way set color to imageview | pass it via intent.
Try below code:
Intent i = new Intent(MainActivity.this,Activity2.class);
i.putExtra("COLOR_1",colors[counter1]);
i.putExtra("COLOR_2",colors[counter2]);
i.putExtra("COLOR_3",colors[counter3]);
startActivity(i);
Then in Activity2
get intent value & set to corresponding ImageView
.
Add this code in onCreate()
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
iv1.setColorFilter(bundle.getInt("COLOR_1",0));
iv2.setColorFilter(bundle.getInt("COLOR_2",0));
iv3.setColorFilter(bundle.getInt("COLOR_3",0));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.