The actual font size changes, but the rest of the view does not move.

enter image description here enter image description here

public class StackAdapter extends BaseAdapter... ... public View getView... final ViewHolder viewHolder; ... viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.telephone_1); ... viewHolder.layout.post(new Runnable() { @Override public void run() { TextView textView = (TextView)viewHolder.layout.findViewById(R.id.textView11); textView.setText("din1"); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22); //or TextView textView2 = (TextView)viewHolder.layout.findViewById(R.id.textView22); textView2.setText("din2"); textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22); int height_in_pixels2 = textView2.getLineCount() * textView2.getLineHeight(); //approx height text textView2.setHeight(height_in_pixels2); } }); 

xml

 <LinearLayout android:id="@+id/telephone_1" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.8" android:layout_marginRight="8dp" android:layout_marginTop="5dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" android:id="@+id/textView11"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" android:id="@+id/textView22"/> </LinearLayout> 

    2 answers 2

    How do I understand this list? most likely you need to change the size of convertView, since the size of textView is changing normally

    In general, I tried to throw a simple example - the size of the list item changes normally:

    xml activation

     <?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"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> 

    xml list item

     <?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"> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> 

    adapter code

     class StackAdapter extends BaseAdapter { private final Context context; public StackAdapter(Context context) { this.context = context; } ....... @Override public int getCount() { return 3; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.list_item, parent, false); TextView textView = (TextView) v.findViewById(R.id.text1); textView.setText("din1"); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22 * (position + 1)); return v; } } 

    Result: enter image description here

    • You're right. It also works for me if I add it to the straight line in getView . But I add layout.post(new Runnable() in a separate thread, in which I first recognize layout.getHeight and then add a certain number of elements to the layout . I didn’t indicate this in the example because layout.getHeight is not important. The problem is that via layout.post(new Runnable() elements are not added, but are changed quickly. - Bogdan Shulga
    • Try in your Runnable , after setting the font size, call layout.invalidate() or layout.requestLayout() - miha_dev
    • @BogdanShulga write the full code of the adapter, I still do not understand why changing the font size in such a complicated way - k.gornichnov
    • Tried layout.invalidate() did not help - Bogdan Shulga

    Calculating the textView height

     int height_in_pixels = textView.getLineCount() * textVew.getLineHeight(); //approx height text textView.setHeight(height_in_pixels); 

    You must transfer the units in which you want to measure the font size. Recommended for androids in SP.

     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22); 

    Or through resources

    Create res/values/dimensions.xml file

     <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="bigTextSize">22sp</dimen> </resources> 

    In the activity we write the following:

     textView.setTextSize(getResources().getDimension(R.dimen.bigTextSize)); 

    After searching I found this option:

     textView.setText("I am a Text", TextView.BufferType.SPANNABLE); 

    Or after resize:

     textView.setText(textView.getText(), TextView.BufferType.SPANNABLE); 
    • rewrote according to your comments. But the result is the same - Bogdan Shulga
    • one
      Completed. Try it - Pleshevskiy
    • No, it does not work. I can't even add an element to this layout . I can change only existing objects. And then you see how they change. - Bogdan Shulga
    • one
      And the xml of this layout is? Can you bring? - Pleshevskiy