Search code examples
androidandroid-layoutincludefindviewbyid

not able to set typeface on include layout childviews


i have textview and edittext that repeat in every layout so i use include tag to avoid repeatation.

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayout2"
    android:overScrollMode="never">

    <LinearLayout
           android:id="@+id/llEmployee"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="vertical"
           android:visibility="gone">

              <include
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/llCompany"
                  layout="@layout/layout_company" />              
        </LinearLayout>

         <LinearLayout
             android:id="@+id/llContactor"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="vertical"
             android:visibility="gone">

              <include
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:id="@+id/llCompany"
                 layout="@layout/layout_company" />              
        </LinearLayout>

        <LinearLayout
            android:id="@+id/llManager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone">

              <include
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/llCompany"
                  layout="@layout/layout_company" />              
        </LinearLayout>
</ScrollView>

layout_company.xml

<?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="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/llCompany"
    android:orientation="vertical"
    tools:showIn="@layout/activity_verify">

    <TextView
        android:id="@+id/txtCompanyName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:text="@string/companyname"
        android:textColor="@color/greyLight" />

    <EditText
        android:id="@+id/edtCompanyName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:imeOptions="actionNext"
        android:inputType="text"
        android:maxLines="1"
        android:textColor="@color/black"
        android:textSize="16sp"
        android:theme="@style/MyEditTextTheme" />

</LinearLayout>

now my problem is i am not able to set typeface to those textview that inside include layout.

i tried this link: findViewById not working for an include?

but its not work for me.. is there anything i missing.

in java file:

LinearLayout view = (LinearLayout) findViewById(R.id.llCompany);
txtCompanyName = (TextView)view.findViewById(R.id.txtCompanyName);
edtCompanyName = (EditText) view.findViewById(R.id.edtCompanyName);
txtCompanyName.setTypeface(Glob.avenir(context));
edtCompanyName.setTypeface(Glob.avenir(context));

i also tried with without id(direct approach), and its not work either. Thanks.

enter image description here


Solution

  • you are using the same id for all includes. try giving a unique id for all includes.