Thursday, 11 July 2019

Autosize TextView with screenSlider and Fragment

I made a screenSlider with fragment pages.

The problem is that in the single page sometimes the TextView contains a long text that will not be displayed completely but only the first shorter word will be displayed (I think it's because the other words will go in a new line out of the bottom margin of the screen).

So I implemented the autosizing TextView but now all TextViews appear small (12sp), also when the text in so short that maybe displayed properly at 36sp (e.g. only one little word).

MainActivity:

public class MainActivity extends FragmentActivity
{
    // The pager widget, which handles animation 
    // and allows swiping horizontally to access previous
    // and next wizard steps.
    private ViewPager mPager;
    // The pager adapter, which provides the pages to the view pager widget.
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }
}

ScreenSlidePagerAdapter extends FragmentStatePagerAdapter:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
    ScreenSlidePagerAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();

        ...

        return fragment;
    }
}

ScreenSlidePageFragment:

public class ScreenSlidePageFragment extends Fragment
{
    private int imageId;
    ImageView imageView;

    private String name = "";

    @Override
    public View onCreateView(LayoutInflater inflater
                            , ViewGroup container
                            , Bundle savedInstanceState)
    {
        ViewGroup rootView = null;

        TextView textView = rootView.findViewById(R.id.text_view_name);
        textView.setText(name);

        TextViewCompat.setAutoSizeTextTypeWithDefaults(textView
                  , TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);

        return rootView;
    }

    ... getters and setters (e.g. to set the name)
}

layout main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootView">

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

pager layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootViewPager">

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

single fragment page layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/rootView">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/text_view_name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_view_name"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:gravity="center_vertical|center_horizontal"
        android:maxLines="1"
        android:text="Name of image"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/image_view" />

</android.support.constraint.ConstraintLayout>

How to make the text in the text_view_name that adapts in size to be showed uniformly?

E.g. the text name must be displayed at 36sp and the text name looooooooong at < 36sp.

Thanks



from Autosize TextView with screenSlider and Fragment

No comments:

Post a Comment