Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inflate a textview xml in my layout?

I have created a custom textview class and i am trying to inflate it in my main xml. Here is my code :-

public class CustomTextView extends TextView{

public CustomTextView(Context context) {
    super(context);
    LayoutInflater li = LayoutInflater.from(context);
    TextView view = (TextView) li.inflate(R.layout.customtextview,null);
    view.setBackgroundColor(Color.RED);
}

}

customtextview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:text="sgfiughbjkh"
    android:id="@+id/customtext"
    android:layout_height="match_parent" >

</TextView>

and in my main activity xml, i have only one linear layout :-

    LinearLayout main = (LinearLayout)findViewById(R.id.mainView);
    CustomTextView cus = new CustomTextView(this);
    main.addView(cus);

I know if i extend it to linear layout instead of textview and add a lienarlayout as parent and in it that textview, it works.

But the problem is that i want to inflate an xml with only a textview and inflate it and the above code is not working. Please suggest

How do inflate a xml containing only one textview using layout inflator ?

like image 882
Rahul Gupta Avatar asked Feb 01 '14 16:02

Rahul Gupta


3 Answers

// inflate text view
    TextView textView =  (TextView) getLayoutInflater().inflate(R.layout.customtextview, null);

// add it to your view
// in your condition, we will add it to Linear Layout
  LinearLayout main = (LinearLayout)findViewById(R.id.mainView);
  main.addView(textView);
like image 103
Mina Fawzy Avatar answered Oct 14 '22 06:10

Mina Fawzy


It's old and 2 years later, I ran into the same issue.

You cannot extend from a View and inflate from a layout. The view needs to be wrapped by a ViewGroup . Read Understanding Android's LayoutInflater.inflate()

I came up with 2 solutions , either :

1. in your class CustomTextView , you set programmatically all the properties . Don't inflate anything. Good luck with conversion from resources dimen.xml

2. In your Activity , inflate the Textview

LinearLayout main = (LinearLayout)findViewById(R.id.mainView);
TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.customtextview, null);
// you might need as well to set the layout params again
// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
// textView.setLayoutParams(params);

main.addView(textView);
like image 4
Raymond Chenon Avatar answered Oct 14 '22 06:10

Raymond Chenon


A bit late but maybe it will help people.

first and like Pratik goyal said you need to declare your CustomTextView and not a TextView

<com.example.CustomTextView
    android:id="@+id/customTextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Then in your CustomTextView class you need to had those lines (Constructor):

public FontFitTextView(final Context context) {
    super(context);

}

public FontFitTextView(final Context context, final AttributeSet attrs) {
    super(context, attrs);

}

And last (correct me if i'm wrong people), but you can't inflate a customView into a TextView.

TextView textView = (TextView) getLayoutInflater().inflate(R.layout.customtextview, null);

rather you need to put your R.layout.customTextView into a customTextView

CustomTextView customTxtView = (CustomTextView) getLayoutInflater().inflate(R.layout.customlayout, null);

And now that i'm writing this down i don't think it's possible to inflate only a TextView. If you want to inflate R.layout.customlayout you need to have at least a layout (Linear,Relative etc...) that wrap your customTextView

Something like this

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.yourpakagename.CustomTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:layout_centerInParent="true"
    android:includeFontPadding="false"
    android:text="13" />

</RelativeLayout>
like image 2
1020rpz Avatar answered Oct 14 '22 07:10

1020rpz