Android : setBackgroundColor with array stays gray, doesn't change color

on Saturday, August 16, 2014


I have a view with a button, and when the button is clicked the background color should change to one of the hex codes in strings.xml :



<string-array name="colorcode_array">
<item>#3498db</item>
<item>#2ecc71</item>
<item>#9b59b6</item>
<item>#f1c40f</item>
<item>#1abc9c</item>
<item>#2980b9</item>
<item>#8e44ad</item>
<item>#e41c1c</item>
<item>#2ecca9</item>
<item>#752ecc</item>
<item>#4f2ecc</item>
<item>#2eccc3</item>
<item>#2ecc53</item>
<item>#2ecc2e</item>
<item>#5bcc2e</item>
<item>#9ecc2e</item>
<item>#cca12e</item>
<item>#cc712e</item>
<item>#f1c209</item>
<item>#86f109</item>
<item>#f11616</item>
<item>#9c1818</item>
</string-array>


But when it is clicked the color changes to lightgray and stays lightgray at every click. Here's the rest of my code:


activity_my2.xml:



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity2"
android:background="#ffdb4b5e">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#ffffffff"
android:textSize="72sp"
android:background="#00000000"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/thing1"
android:id="@+id/textView"
android:textColor="#ffffffff"
android:textSize="36sp"
android:gravity="center"
android:layout_above="@+id/button"
/>


</RelativeLayout>


MyActivity2.java:



package com.MR.brd;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.MR.brd.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class MyActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity2);
final TextView tv = (TextView) findViewById(R.id.textView);
Button n = (Button) findViewById(R.id.button);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
n.setTypeface(typeface);
Typeface face = Typeface.createFromAsset(getAssets(),"OSP-DIN.ttf");
tv.setTypeface(face);


final String[] values = getResources().getStringArray(R.array.things_array);
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Random RAND=new Random();
int position = RAND.nextInt(values.length);
String nextValue = values[position];
int colors[] = getResources().getIntArray(R.array.colorcode_array);
int color = colors[position];
tv.setText(nextValue);
View layout = findViewById(R.id.layout_view);
layout.setBackgroundColor(color);
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.animation4, R.anim.animation3);
}
}

0 comments:

Post a Comment