কম্পিউটার টিউটোরিয়াল

Android-এ TextView টেক্সট বাউন্ডের মধ্যে ফিট করার জন্য অটো-স্কেল করার সেরা পদ্ধতি

Android-এ TextView টেক্সট বাউন্ডের মধ্যে ফিট করার জন্য অটো-স্কেল করার পদ্ধতি

এই উদাহরণটি দেখায় কিভাবে AutofitTextView লাইব্রেরি ব্যবহার করে TextView-এর টেক্সট আকার স্বয়ংক্রিয়ভাবে সামঞ্জস্য করা যায় যাতে এটি নির্ধারিত বাউন্ডের মধ্যে সুন্দরভাবে ফিট হয়।

প্রয়োজনীয় ধাপসমূহ

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন এবং ডিপেন্ডেন্সি যোগ করুন

Android Studio-তে File → New Project এ যান এবং প্রজেক্ট তৈরি করুন। তারপর build.gradle (Module: app) ফাইলে নিম্নলিখিত ডিপেন্ডেন্সিগুলি যোগ করুন:

dependencies {
    implementation 'com.android.support:design:28.0.0'
    implementation 'me.grantland:autofittextview:0.2.1'
}

ধাপ ২: লেআউট ফাইল সংশোধন করুন (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    xmlns:autofit="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:singleLine="true"
        android:text="Android AutoFit TextView উদাহরণ"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:maxLines="2"
        android:singleLine="true"
        android:text="একটি TextView যা স্বয়ংক্রিয়ভাবে টেক্সট আকার পরিবর্তন করে বাউন্ডের মধ্যে পুরোপুরি ফিট করার জন্য"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:singleLine="true"
        android:text="Android AutoFit TextView"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:maxLines="2"
        android:singleLine="true"
        android:text="স্বয়ংক্রিয়ভাবে আকার পরিবর্তিত টেক্সট"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />

    <me.grantland.widget.AutofitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:singleLine="true"
        android:text="স্বয়ংক্রিয়ভাবে টেক্সট আকার পরিবর্তন করে বাউন্ডের মধ্যে পুরোপুরি ফিট করার জন্য"
        android:textSize="42sp"
        autofit:minTextSize="5sp" />

</LinearLayout>

ধাপ ৩: MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // কোনো অতিরিক্ত জাভা কোড প্রয়োজন নেই - সবকিছু XML-এ হ্যান্ডেল করা হয়েছে
    }
}

ধাপ ৪: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

গুরুত্বপূর্ণ অ্যাট্রিবিউট ব্যাখ্যা

অ্যাট্রিবিউট বর্ণনা
autofit:minTextSize ন্যূনতম টেক্সট আকার (sp-এ) যা অটো-ফিটের সময় ব্যবহৃত হবে
android:maxLines সর্বোচ্চ লাইন সংখ্যা যা TextView প্রদর্শন করবে
android:singleLine true সেট করলে টেক্সট এক লাইনে সীমাবদ্ধ থাকবে
android:textSize প্রাথমিক টেক্সট আকার (AutofitTextView এটি প্রয়োজনে কমিয়ে দেবে)

আধুনিক বিকল্প (API 26+)

Android 8.0 (API लेভেল 26) এবং তার পরবর্তী ভার্সনগুলিতে, আপনি বিল্ট-ইন android:autoSizeTextType অ্যাট্রিবিউট ব্যবহার করতে পারেন কোনো এক্সটার্নাল লাইব্রেরি ছাড়াই:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoSizeTextType="uniform"
    android:autoSizeMinTextSize="12sp"
    android:autoSizeMaxTextSize="100sp"
    android:autoSizeStepGranularity="2sp"
    android:maxLines="2" />

অ্যাপ্লিকেশন চালানোর পদ্ধতি

আপনার Android ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Android Studio-তে Run আইকনে ক্লিক করুন এবং ডিভাইসে আউটপুট দেখুন।

Android-এ TextView টেক্সট বাউন্ডের মধ্যে ফিট করার জন্য অটো-স্কেল করার সেরা পদ্ধতি

  1. অ্যান্ড্রয়েড অটোর ৭টি সেরা অল্টার্নেটিভ

  2. সবচোয়ে ভালো ফ্রি অ্যান্ড্রয়েড লঞ্চার কোনটি?

  3. অ্যান্ড্রয়েডের জন্য ১০টি সেরা টেলিপ্রম্প্টার অ্যাপ

  4. ডুপ্লিকেট ফাইল দক্ষতার সাথে সরিয়ে ডিস্ক স্পেস ফ്രീ করুন