-2

I have tried the following code to implement 'width' (in percentage) to the TextView. But I am always getting null for the PercentLayoutInfo. Please help.

PercentRelativeLayout.LayoutParams lp = (PercentRelativeLayout.LayoutParams) textView.getLayoutParams();

        // This will currently return null, if it was not constructed from XML.
PercentLayoutHelper.PercentLayoutInfo info = lp.getPercentLayoutInfo();

info.widthPercent = .5f;
textView.requestLayout();

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content_tile_main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    android:duplicateParentState="true"
    android:focusable="false">

    <ImageView
        android:id="@+id/main_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <android.support.percent.PercentRelativeLayout
        android:id="@+id/percent_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >

        <TextView
            android:id="@+id/title_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Title: " />
    </android.support.percent.PercentRelativeLayout>


</RelativeLayout>
Saamzzz
  • 256
  • 1
  • 14
  • 2
    Hi Saamzzz, it seems like you are having a problem but you’re providing very little information about it. I’d recommend you add more code/information to your question (specifically, the layout/XML or Code you use), and things you have tried and what results you’ve achieved. People can help you if they see the problem, we don’t have many mind-readers that I know of around here ;) – Martin Marconcini May 04 '18 at 16:19
  • @MartinMarconcini. I have updated the question. Please take a look. – Saamzzz May 05 '18 at 17:02

1 Answers1

0

I have fixed the issue by adding percentage related attribute to the TextView in XML as follows. As per my understanding, we have to provide percent related attribute in XML, then only we will be able to apply PercentLayoutInfo programatically for that view.

 <TextView
            android:id="@+id/title_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:layout_widthPercent="50%"
            android:text="Title: " />

Now I am getting not null value for PercentLayoutInfo .

Saamzzz
  • 256
  • 1
  • 14