Thursday, 14 January 2016

XML : Change attributes for a TextView from an inflated layout

I have a list view whose margins and paddings I want to change dinamically, according with the screen density, so I need to set those attributes in java rather than xml, I was trying to inflate the item layout view and get its inner TextView, but the attributes don't change, here is my code:

this is the activity xml

  <?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context="com.onward.hemoapp.SearchDevicesActivity">        <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="We've found the following devices:"          android:id="@+id/devicesLabel"/>        <ListView          android:id="@layout/device_list"          android:layout_width="fill_parent"          android:layout_height="match_parent"          android:divider="@android:color/transparent"          android:dividerHeight="20.0sp"          android:layout_below="@+id/devicesLabel">        </ListView>    </RelativeLayout>    

this is the item layout

  <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"      android:background="@drawable/list_selector">          <TextView          android:id="@+id/deviceId"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:textStyle="bold"          android:textColor="@drawable/list_selector">      </TextView>    </LinearLayout>    

this is the piece of java code that I'm trying

  setContentView(R.layout.activity_search_devices);  DisplayMetrics metrics = new DisplayMetrics();  getWindowManager().getDefaultDisplay().getMetrics(metrics);  float scale=metrics.density;  devicesLabel=(TextView) findViewById(R.id.devicesLabel);  devicesLabel.setPadding((int) (20 * scale), (int) (15 * scale), 0, (int) (15 * scale));  devicesLabel.setTextSize(10 * scale);  LinearLayout deviceList=(LinearLayout)LayoutInflater.from(SearchDevicesActivity.this).inflate(R.layout.device_list, null);  TextView deviceId=(TextView) deviceList.findViewById(R.id.deviceId);  deviceId.setPadding((int) (20 * scale), (int)(15 * scale), 0, (int) (15 * scale));    

The TextView doesn't change its attributes, I tested many others and it remains the same

No comments:

Post a Comment