Button Colors

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Button Colors

Postby andy_spoo » Wed Aug 12, 2009 6:44 pm

Thanks for creating this site guys, it's been very useful for a beginner like myself. But, I need help with the following:-

I know how to change button colors using the main.xml file and color.xml file etc., and how to change them individually using buttonname.setTextColor(red);. But I'd like to do a loop like the example below: -

// **** Read saved data from xml file ************************
SharedPreferences settings = getSharedPreferences("/sdcard/directory1/files/myxmlfile", 0);
// Get the number of files
int filecount=settings.getInt("filenumber", 0); // get the number of files listed in the xml file.
String filename="";
int red = getResources().getColor(R.color.red); // define the colour red

do
{
filename=settings.getString(("filename_"+filecount), ""); // get string like "filename_3" for example
filecount--;
filename.setTextColor(red);
}
while (filecount>0);
// ********************************************************

I'm only a beginner so please be kind!

Because 'filename' is a string 'filename' it won't let me enter or run the code. If 'filename' is changed for an ACTUAL button name it will change the button color fine, but thats not what I want.

Hope that makes sense?

Any ideas guys??

Cheers, Andy
If you pay peanuts then expect moneys.
andy_spoo
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 12, 2009 6:25 pm

Top

Postby andy_spoo » Thu Aug 13, 2009 3:43 pm

No ideas?
If you pay peanuts then expect moneys.
andy_spoo
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 12, 2009 6:25 pm

Alternative way.

Postby andy_spoo » Thu Aug 13, 2009 5:45 pm

or is there a way to change the line :- android:textColor="@color/black"
in the main.xml file from one value to another (e.g. android:textColor="@color/red") from within the program and store it for the next time the program is ran again?
If you pay peanuts then expect moneys.
andy_spoo
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 12, 2009 6:25 pm

Postby konic » Wed Sep 30, 2009 1:21 pm

Hi,
here is the issue I came across with Button.
---
What's the difference between using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. b.setTextColor(R.color.tabs_bg_clr);
  3.  
  4.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

and
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. b.setTextColor(0xFFFF8000);
  3.  
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

where b is a Button :?:
Of course, in colors.xml I have this line
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <color name="tabs_bg_clr">#FFFF8000</color>
  3.  
  4.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

---
The reason I am asking it is because
in first case text color on the screen is dark gray,
in second - orange as it should be.
---
P.S. Could it be related to ColorStateList (?)
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

Postby azraeal » Wed Sep 30, 2009 6:58 pm

andy_spoo:
I'm assuming you have a loop that would create a button for each filename. Why not do it there when you have a synchronous match of button to filename?

Also, Red is a predefined constant that can be readily used. Color.RED. Ignore this comment if you're using some custom color in your xml.

konic:
The first method: Using R.color, uses more resources simply because you have to generate another R variable and the extra definition in XML, and to parse the XML. The difference is a few bytes. I'd stick to having the colors as some form of global variables.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Postby konic » Wed Sep 30, 2009 8:58 pm

azraeal wrote:andy_spoo:
konic:
The first method: Using R.color, uses more resources simply because you have to generate another R variable and the extra definition in XML, and to parse the XML. The difference is a few bytes. I'd stick to having the colors as some form of global variables.

Hi, the question mostly was: Why when I use resource it sets wrong (!) color.
In parallel there is one more (maybe related) question.
When I use setBackgroundColor(R.color.bg_col) it sets black color(!?),
but setBackgroundResource(R.color.bg_col) does the right job ?
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

Top

Postby shadow2kx » Thu Oct 01, 2009 8:46 pm

Hello,
I've got a similar problem but while using textview. Using hardcoded colors works perfectly but when referring to color stored in colors.xml the text's color stay grey. I'm rather new to android and can't figure out what's going on.

Here is an example that show what's going on. You just need to comment one of the two colorarray and see what's happening:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package test.testfactory.color;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.text.Spannable;
  10.  
  11. import android.text.style.ForegroundColorSpan;
  12.  
  13. import android.view.View;
  14.  
  15. import android.view.View.OnClickListener;
  16.  
  17. import android.widget.Button;
  18.  
  19. import android.widget.TextView;
  20.  
  21.  
  22.  
  23.  
  24.  
  25. public class ColorTest extends Activity
  26.  
  27. {
  28.  
  29.     private int roll;
  30.  
  31.        
  32.  
  33.     /** Called when the activity is first created. */
  34.  
  35.     @Override
  36.  
  37.     public void onCreate(Bundle savedInstanceState)
  38.  
  39.     {
  40.  
  41.         super.onCreate(savedInstanceState);
  42.  
  43.         setContentView(R.layout.main);
  44.  
  45.    
  46.  
  47.         roll = 0;
  48.  
  49.         Button bt = (Button) findViewById(R.id.Button01);
  50.  
  51.        
  52.  
  53.         bt.setOnClickListener(new OnClickListener() {
  54.  
  55.                         @Override
  56.  
  57.                         public void onClick(View v)
  58.  
  59.                         {
  60.  
  61.                                 TextView tv = (TextView) findViewById(R.id.tvText);
  62.  
  63.                                 tv.setText(R.string.hello, TextView.BufferType.SPANNABLE);
  64.  
  65.                                
  66.  
  67.                                 // Choose between these two array.
  68.  
  69.                                 // They should do the same job but don't. First one don't work, second one does
  70.  
  71.  
  72.  
  73.                                 // Color array, from colors.xml
  74.  
  75.                                 /*int[] colorarray = {R.color.cl_lfs1, R.color.cl_lfs2, R.color.cl_lfs3,
  76.  
  77.                                                                         R.color.cl_lfs4, R.color.cl_lfs5, R.color.cl_lfs6,
  78.  
  79.                                                                         R.color.cl_lfs7, R.color.cl_lfs8, R.color.cl_lfs9 };*/
  80.  
  81.                                 // Hardcoded colors
  82.  
  83.                                 int[] colorarray = { 0xFF000000, 0xFFFF0000, 0xFF05FD05,
  84.  
  85.                                                                          0xFFFFFF00, 0xFF0000FF, 0xFFFF00FF,
  86.  
  87.                                                                          0xFF00FFFF, 0xFFFFFFFF, 0xFFCCCCCC };
  88.  
  89.                
  90.  
  91.                                
  92.  
  93.                                 // We applies the color to the text
  94.  
  95.                                 Spannable str = (Spannable) tv.getText();
  96.  
  97.                                 str.setSpan(new ForegroundColorSpan(colorarray[roll]),0,tv.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  98.  
  99.  
  100.  
  101.                                 roll ++;
  102.  
  103.                                 if (roll >= colorarray.length) roll = 0;        
  104.  
  105.                         }
  106.  
  107.                 });
  108.  
  109.     }
  110.  
  111. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


With colors.xml being:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.  
  6.  
  7. <color name="cl_lfs1">#FF000000</color>
  8.  
  9. <color name="cl_lfs2">#FFFF0000</color>
  10.  
  11. <color name="cl_lfs3">#FF05FD05</color>
  12.  
  13. <color name="cl_lfs4">#FFFFFF00</color>
  14.  
  15. <color name="cl_lfs5">#FF0000FF</color>
  16.  
  17. <color name="cl_lfs6">#FFFF00FF</color>
  18.  
  19. <color name="cl_lfs7">#FF00FFFF</color>
  20.  
  21. <color name="cl_lfs8">#FFFFFFFF</color>
  22.  
  23. <color name="cl_lfs9">#FFCCCCCC</color>
  24.  
  25.  
  26.  
  27. </resources>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


I tryed with and without the alpha channel but it doesn't seem to affect the result.
shadow2kx
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 01, 2009 8:23 pm

Postby azraeal » Thu Oct 01, 2009 9:07 pm

When I use setBackgroundColor(R.color.bg_col) it sets black color(!?),
but setBackgroundResource(R.color.bg_col) does the right job ?


setBackgroundColor(R.color.value) parses the POINTER R.color.value as a color. It does not parse the object of the pointer but the pointer itself. So you've been parsing a 0x7fXXXXXX pointer value as a color instead of your bg_col value.

I believe it is the same for shadow2kx
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Postby konic » Thu Oct 01, 2009 10:50 pm

Oops, you're absolutely right.
I made this mistake because both APIs take the same type of argument - int,
but for the Color it must be color,
whereas for Resource it must be resource!
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests