question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't auto resize in the first time

See original GitHub issue
  • I am trying to use AutoResizeTextView on a simple activity, but it fails on the first go. If the button is touched to auto resize there is no problem. But, the thing is, i need it to be resized when the activity is first created.
  • Here is the list of things i have tried: Created AutoRiseTextView in xml as a CustomView. Called “show_the_text” method twice on the start. Called the method twice with different texts. Moved container decleration to inside of onCreate. Used RelativeLayout insted of FrameLayout. Created a temporary TextView inside of FrameLayout with text in it.
  • I run the application on API23 emulator and API21 phone but result is the same.
  • I have to say that i am a begginer in programming. Any ideas are welcome.

oncreateview

JAVA FILE

     public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //auto resizing fails on the first run
        show_auto_resized_text();

        //but when we use user touch it works
        TextView button=(TextView) findViewById(R.id.text_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show_auto_resized_text();
            }
        });
    }
    public void show_auto_resized_text(){
        FrameLayout text_container=(FrameLayout) findViewById(R.id.container);
        text_container.removeAllViews();
        int width= text_container.getWidth()-40;
        int height= text_container.getHeight()-40;
        AutoResizeTextView textView=new AutoResizeTextView(MainActivity.this);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
        textView.setMaxLines(10);
        textView.setTextColor(Color.WHITE);
                textView.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, height, getResources().getDisplayMetrics()));
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setLayoutParams(new ViewGroup.LayoutParams(width, height));
        final String DOUBLE_BYTE_SPACE = "\u3000";
        String fixString = "";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR1
        && android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            fixString = DOUBLE_BYTE_SPACE;
        }
        textView.setText(fixString + "THIS IS MY TEXT TO BE AUTO RESIZED" + fixString);
        text_container.addView(textView);
    }
}

XML FILE

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kr.hakan.myapplication.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/text_button"
        android:background="#ff5b14"
        android:layout_margin="20dp"
        android:id="@+id/container"></FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Auto Resize The Text"
        android:id="@+id/text_button"
        android:layout_alignParentBottom="true"
        android:background="#000000"
        android:textSize="30sp"
        android:textColor="#ffffff"
        android:gravity="center" />
</RelativeLayout>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
hakan6000commented, Apr 6, 2016

Thanks a lot AndroidDeveloperLB, it works now.

1reaction
AndroidDeveloperLBcommented, Apr 5, 2016

Just use what I wrote there:

private static void runJustBeforeBeingDrawn(final View view, final Runnable runnable)
{
    final ViewTreeObserver vto = view.getViewTreeObserver();
    final OnPreDrawListener preDrawListener = new OnPreDrawListener()
    {
        @Override
        public boolean onPreDraw()
        {
            Log.d(App.APPLICATION_TAG, CLASS_TAG + "onpredraw");
            runnable.run();
            final ViewTreeObserver vto = view.getViewTreeObserver();
            vto.removeOnPreDrawListener(this);
            return true;
        }
    };
    vto.addOnPreDrawListener(preDrawListener);
}

alternatively , you can use addOnGlobalLayoutListener instead of addOnPreDrawListener if you wish.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't my view auto resize af… | Apple Developer Forums
I have an app using the splitscreen controller. The detail view part of it does not resize when the iPad orientation changes. All...
Read more >
JScrollpane does not autoresize when there is a JTable inside ...
The problem is, that if the JFrame is resized, the JScrollpane stays the same, doesn't matter if the JFrame is made really small,...
Read more >
Turn on and off automatic copyfitting - Microsoft Support
You can have Publisher automatically resize your text to fit in a text box when the text box is not connected to other...
Read more >
Auto resize with contrainer · Issue #28 · microsoft/monaco-editor
The container always takes 100% of the page size. When I resize the page, however, the monaco editor doesn't resize itself.
Read more >
How to change and AutoFit row height in Excel - Ablebits
For this, select any cell in the row(s) you'd like to resize, and do the following: On the Home tab, in the Cells...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found