0

i want to change a text of my text view to fill the parent. i'm following this question: How to adjust text font size to fit textview

But i have a nullPointerException when i try to manage my FontFitTextView istance.

this is my java code:

     @SuppressLint("WrongCall") @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_avatar);

    LinearLayout llyCoins = (LinearLayout) findViewById(R.id.llyCoins);

    FontFitTextView textCoins = new FontFitTextView(this);
    textCoins.onMeasure( llyCoins.getMeasuredWidth(),llyCoins.getMeasuredHeight());

    Log.d("FLAG", "error in this line");
    // rimuovo il parent da textcoins
    ((ViewGroup)textCoins.getParent()).removeView(textCoins);


   // aggiungo textcoins alla view llycoins
    llyCoins.addView(textCoins);

i have a null pointer exception in this line :

 ((ViewGroup)textCoins.getParent()).removeView(textCoins);

my xml:

 <LinearLayout 
            android:id="@+id/llyCoins"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:orientation="horizontal">


        </LinearLayout>

Can someone help me?

Community
  • 1
  • 1
Matt
  • 49
  • 9

1 Answers1

0

you have to add your text view to linear layout then you can use getParent

LinearLayout llyCoins = (LinearLayout) findViewById(R.id.llyCoins);

FontFitTextView textCoins = new FontFitTextView(this);

llyCoins.addView(textCoins);    

textCoins.onMeasure( llyCoins.getMeasuredWidth(),llyCoins.getMeasuredHeight());

Log.d("FLAG", "error in this line");
// rimuovo il parent da textcoins
((ViewGroup)textCoins.getParent()).removeView(textCoins);


// aggiungo textcoins alla view llycoins
llyCoins.addView(textCoins);
Mohammad Rahchamani
  • 4,406
  • 1
  • 22
  • 35