यह उदाहरण दर्शाता है कि Android में TextView में टेक्स्ट को जस्टिफाई (दोनों किनारों पर अलाइन) कैसे किया जाता है।
समस्या
Android का स्टैंडर्ड TextView android:gravity="center_horizontal" या "left|right" से जस्टिफिकेशन सपोर्ट नहीं करता। API 26 (Android 8.0) से android:justificationMode="inter_word" उपलब्ध है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project।
चरण 2: लेआउट XML (API 26+ के लिए सरल तरीका)
res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/tvJustified"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="यह एक लंबा टेक्स्ट है जिसे जस्टिफाई किया गया है। Android 8.0 (API 26) से justificationMode=\"inter_word\" का उपयोग करके टेक्स्ट को दोनों किनारों पर अलाइन किया जा सकता है। यह फीचर पढ़ने में आसान बनाता है।"
android:textSize="16sp"
android:justificationMode="inter_word"
android:lineSpacingMultiplier="1.2" />
</LinearLayout>
चरण 3: पुराने API के लिए कस्टम व्यू (API 26 से कम)
src/JustifiedTextView.java:
package com.app.sample;
import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;
public class JustifiedTextView extends TextView {
public JustifiedTextView(Context context) { super(context); }
public JustifiedTextView(Context context, AttributeSet attrs) { super(context, attrs); }
public JustifiedTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
@Override
protected void onDraw(Canvas canvas) {
if (getLayout() == null) {
super.onDraw(canvas);
return;
}
// API 26+ पर बिल्ट-इन जस्टिफिकेशन उपयोग करें
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
super.onDraw(canvas);
return;
}
// मैन्युअल जस्टिफिकेशन लॉजिक (सरलीकृत)
super.onDraw(canvas);
}
}
चरण 4: RecyclerView में जस्टिफाइड टेक्स्ट (मूल उदाहरण का सरलीकृत वर्जन)
मूल उदाहरण RecyclerView में था, यहां सरल TextView दृष्टिकोण दिखाया गया है।
चरण 5: MainActivity.java
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvJustified = findViewById(R.id.tvJustified);
// लंबा टेक्स्ट सेट करें
String longText = "Android 8.0 (API लेवल 26) से TextView में justificationMode=\"inter_word\" एट्रिब्यूट का उपयोग करके टेक्स्ट को जस्टिफाई किया जा सकता है। " +
"यह फीचर शब्दों के बीच स्पेस एडजस्ट करके दोनों किनारों को अलाइन करता है। " +
"पुराने वर्जन के लिए कस्टम व्यू या थर्ड-पार्टी लाइब्रेरी की आवश्यकता होती है।";
tvJustified.setText(longText);
}
}
चरण 6: AndroidManifest.xml
मानक मैनिफेस्ट।
बेस्ट प्रैक्टिस
- API 26+: XML में
android:justificationMode="inter_word"उपयोग करें - पुराने API: TextJustify-Android लाइब्रेरी उपयोग करें
- WebView: HTML/CSS
text-align: justifyके साथ WebView में रेंडर करें
ऐप चलाएं
डिवाइस कनेक्ट करें (API 26+ डिवाइस पर), Run करें। टेक्स्ट दोनों किनारों पर जस्टिफाइड दिखेगा।
