परिचय
यह ट्यूटोरियल दिखाता है कि Android में TextView के टेक्स्ट को उसके बाउंड्स (सीमाओं) के अंदर स्वचालित रूप से कैसे फिट किया जाए। हम इसके लिए एक लोकप्रिय थर्ड-पार्टी लाइब्रेरी AutofitTextView का उपयोग करेंगे।
आवश्यक चरण
चरण 1: नया प्रोजेक्ट बनाएं और डिपेंडेंसी जोड़ें
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'
}
चरण 2: लेआउट फ़ाइल बनाएं (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:maxLines="2"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
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:maxLines="2"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
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>
चरण 3: मुख्य गतिविधि (src/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);
// AutofitTextView XML में ही कॉन्फ़िगर हो जाता है, यहां अतिरिक्त कोड की जरूरत नहीं
}
}
चरण 4: मैनिफेस्ट फ़ाइल (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>
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें और देखें कि लंबे टेक्स्ट कैसे अपने आप छोटे होकर व्यू में फिट हो जाते हैं।

महत्वपूर्ण बिंदु
autofit:minTextSize- न्यूनतम टेक्स्ट साइज सेट करता है (यहां 5sp)।android:maxLinesऔरandroid:singleLineटेक्स्ट को कितनी लाइनों में दिखाना है, यह नियंत्रित करते हैं।- यह लाइब्रेरी API 14+ सपोर्ट करती है।
- नए प्रोजेक्ट्स के लिए AndroidX वर्जन
me.grantland:autofittextview:0.2.1का उपयोग करें।