1

i have a problem with my application. Basically what it does, is when i click a button, it creates 3 fields (2 editText and 1 spinner) on a scrollView. The thing works well, the only problem that im having, is related with the style, the activity bgColor is white(as the rest of the app) but, when i create elements programmatically, these elements doesnt have the look of the rest of my app. The editTexts are white with white letters (impossible to read since my bgColor is white as well) and its the same thing with the spinner. What can i do? Here is a snipet of code so you can see what im doing here.

public class AddIngredients extends Activity {
public int Count = 0;
public String[] spinnerArray = {"Gr", "kg", "Cups", "ml", "L", "oz"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addingredients);
    final TableLayout lm = (TableLayout) findViewById(R.id.TableMain);

    TableLayout.LayoutParams params = new TableLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    Button addMore = (Button)findViewById(R.id.addmore);
    addMore.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            TableRow ll = new TableRow(getApplicationContext());
            //ll.setOrientation(LinearLayout.HORIZONTAL);


            EditText product = new EditText(getApplicationContext());
            product.setHint(" Ingredient "+Count +"    ");

            // Create Button
            EditText amount = new EditText(getApplicationContext());
                // Give button an ID
                amount.setId(Count);
                amount.setHint("Quantity");

            final Button btn2 = new Button(getApplicationContext());

                btn2.setId(Count);
                btn2.setText("Remove " + Count);

            Spinner spinner = new Spinner(getApplicationContext());
            ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
            spinner.setAdapter(spinnerArrayAdapter);

            ll.addView(product);
            ll.addView(amount);
            ll.addView(spinner);
            lm.addView(ll);
            Count = Count + 1;

I know my XML is working well because if i create the 3 views on my xml, they look great. PD: Thx in advance for any help! Greetings.

3 Answers3

1

you can use

amount.setTextColor(Color.BLACK);

to set colour of text to black or any other colour
same can be used for spinner

karan
  • 93
  • 1
  • 12
  • I did try that, the thing is that, yes, the color of the text changes, but not the line under the text (the editText line) and sadly, there is no way to do it with the spinner, at least i didnt find it. :c – user3405999 May 13 '14 at 16:12
  • you can add LayoutParams with the EditText as well and you can change the ui according to your wish – karan May 13 '14 at 16:19
  • How can i do that Karan? :$ – user3405999 May 13 '14 at 16:35
  • LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); and add using ll.addView(amount, params); – karan May 13 '14 at 16:47
  • you av=n refer to this link http://stackoverflow.com/questions/14199240/how-to-remove-the-border-of-edittextfield-in-android if you want to remove borders of your edittext... if this do not work then tell what eactly what you want nd what is your outcome.. – karan May 13 '14 at 16:56
  • Well, tried that too, and, no work, keeps showing that ugly theme... :/ – user3405999 May 13 '14 at 16:57
  • No no, i dont want to remove the editText border, i want to change the color of it, well not change, i want it to be as the rest of the app. If i create those fields manually, they work perfect, if i create them programmatically, they look different... – user3405999 May 13 '14 at 16:59
  • try account.setBackgroundColor(Color.WHITE) or post the screens so that we can identify what difference are you talking about – karan May 13 '14 at 17:02
  • Here is a picture. As you can see, the first 3 fields were added manually(they look great, with the apptheme), the next ones were added by pressing the addMore Button, (they look different, they have another theme, look carefully, because you may think the text is normal, but is not, that text is a hint.) http://www.sodaberry.com/?attachment_id=131 – user3405999 May 13 '14 at 17:16
  • if you want to change the colour of border of edit text refer to this link http://stackoverflow.com/questions/9711954/how-to-change-edittext-color-border-in-android – karan May 13 '14 at 17:18
  • Ok, gonna try that. Thx a LOT! – user3405999 May 13 '14 at 17:21
  • change the background of the edittext to black,it must work and the thing you was talking about hint what happens when u click on the edittext to write something?? – karan May 13 '14 at 17:23
  • If i click the EditText, the hint hides(as expected) and the text goes white. – user3405999 May 14 '14 at 09:51
  • No, i think there must be some kind of "bug" or something.. Because the problem goes only with the fields i create programmatically, if i create those fields manually in xml, they work great. – user3405999 May 15 '14 at 10:36
  • Ok, i did it, i was looking and reading for some documentation, and i had an error when creating the fields. The error was (getApplicationContext()). That doesnt work, the way to do it is MyActivityName.this. That fix my problem! Thank you very much for helping me! Greetings – user3405999 May 15 '14 at 11:42
1

Here's how I set the colors of my edit text lines and other theme-related android views.

http://android-holo-colors.com/

I just picked the color and views I wanted, then unzipped them in my res folder, then set the theme according the android tutorials.

I recommend backing up your res folder first, in case you don't like the results.

Garret

Garret
  • 1,137
  • 9
  • 17
  • Ok, but my whole app theme works perfect, and even the scrollview works great (if i add the elements in the xml manually), the problem comes with i create the elements programmatically :/. – user3405999 May 13 '14 at 16:45
  • As far as I know, there isn't an attribute for the edit text line color that you can change programmatically and I remember searching for it a while back. You can keep your existing theme with the method above, just add the elements created from that site to override the edit text line. – Garret May 13 '14 at 16:54
0

I had a error in my code. When creating the fields, i was using (getApplicationContext());. I fixed it using MyApplicationName.this.