pbm
pbm

Reputation: 378

TextView from XML and from code difference

When I'm creating TextView from XML all text inside is black, but when I'm doing it from code it is gray. Why?

Location of elements from code and XML is the same: main layout is RelativeLayout (always from XML).

All elements that are below are created in one case from XML and in second from code: ScrollView -> TableLayout -> TableRow -> TextView.

In both cases in all hierarchy I'm not setting color/style/appaerance parameters.

Code:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.o_layout);
ScrollView sv = new ScrollView(getApplicationContext());
TableLayout tl = new TableLayout(getApplicationContext());
float density = getResources().getDisplayMetrics().density;
tl.setPadding((int)(10 * density), (int)(4 * density), (int)(4 * density), 0);
for(String s: lista){
     add_row(tl, s);
}
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.osobowe_search_field);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
sv.addView(tl);
layout.addView(sv, params);

private void add_row(TableLayout tl, String left_text){
    float density = getResources().getDisplayMetrics().density;

    TableRow tr = new TableRow(getApplicationContext());
    tr.setMinimumHeight((int)(30 * density));
    TableLayout.LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.bottomMargin = (int)(4 * density);

    TextView left = new TextView(getApplicationContext());
    left.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, 25);
    TableRow.LayoutParams left_params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
    left_params.gravity = Gravity.CENTER_VERTICAL;
    left.setText(left_text);

    TextView right = new TextView(getApplicationContext());
    right.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, 18);
    right.setGravity(Gravity.CENTER_VERTICAL);
    TableRow.LayoutParams right_params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
    right_params.leftMargin = (int)(10 * density);
    right_params.gravity = Gravity.CENTER_VERTICAL;
    //right_params.setMargins((int)(10 * density), 0, 0, 0);
    right_params.weight = 1;
    right.setText(get_desc("l", left_text));

    View line = new View(getApplicationContext());
    line.setBackgroundResource(R.drawable.line_on_bottom);
    TableLayout.LayoutParams line_params = new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, (int)density);
    line_params.setMargins((int)(20 * density), 0, (int)(20 * density), 0);

    tr.addView(left, left_params);
    tr.addView(right, right_params);
    tl.addView(tr, params);
    tl.addView(line, line_params);


}

XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/o_details"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:paddingRight="4dip"
        android:paddingTop="4dip" >

        <TableRow
            android:id="@+id/o_row01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="30dp"
            android:layout_marginBottom="4dip" >

            <TextView
                android:id="@+id/o_num01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="@string/_"
                android:textSize="25dip"
                />

Upvotes: 0

Views: 339

Answers (1)

user1658057
user1658057

Reputation: 21

One simple workaround is as follows -- Try retrieving the TextView using its known ID(according to the xml above), either in the onResume() method or in the add-row function(code above) and manually set the color to black/gray. Function setBackgroundColor will help.

Usage --

mytextview.setBackgroundColor(getResources().getColor(R.color.darkgrey)); 

(if you have defined the color 'darkgrey' in a resources/color.xml using its hexadecimal value)

Upvotes: 1

Related Questions