0
  • 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.
  • And this is the source code of AutoResizeTextView i used: https://github.com/AndroidDeveloperLB/AutoFitTextView

Application Screenshots

    package com.kr.hakan.myapplication;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.util.TypedValue;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.TextView;

    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);
        }
    }

and the 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>
Hakan kr
  • 1
  • 2

1 Answers1