Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Kotlin का उपयोग करके Android TextView में लाइन ब्रेक कैसे जोड़ें?

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android TextView में लाइन ब्रेक (नई लाइन) कैसे जोड़े जाते हैं। \n एस्केप सीक्वेंस, HTML <br>, या string.xml में फॉर्मेटिंग का उपयोग किया जा सकता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।

चरण 2: activity_main.xml में TextView जोड़ें

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/Questions"
        android:textSize="24sp" />

</RelativeLayout>

चरण 3: MainActivity.kt में टेक्स्ट कलर सेट करें

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        val textView: TextView = findViewById(R.id.textView)
        textView.setTextColor(Color.RED)
    }
}

चरण 4: strings.xml में लाइन ब्रेक के साथ स्ट्रिंग परिभाषित करें

<resources>
    <string name="app_name">Q5</string>
    <string name="Questions">
1. What is your Favourite food?
2. Which is your favourite sport?
3. Whats your bucket list?
    </string>
</resources>

नोट: XML में वास्तविक नई लाइनें (एंटर की) काम करती हैं। \n या <br> की आवश्यकता नहीं है।

चरण 5: कोड में डायनामिक लाइन ब्रेक

// तरीका 1: \n एस्केप सीक्वेंस
textView.text = "Line 1\nLine 2\nLine 3"

// तरीका 2: HTML fromHtml (डिप्रिकेटेड नहीं)
textView.text = Html.fromHtml("Line 1<br>Line 2<br>Line 3", Html.FROM_HTML_MODE_LEGACY)

// तरीका 3: बिल्डर पैटर्न
textView.text = buildString {
    append("Line 1")
    append('\n')
    append("Line 2")
    append('\n')
    append("Line 3")
}

// तरीका 4: Kotlin मल्टी-लाइन स्ट्रिंग
textView.text = """
    Line 1
    Line 2
    Line 3
""".trimIndent()

चरण 6: AndroidManifest.xml कॉन्फ़िगर करें

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

    <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>

ऐप चलाएं

अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।

  1. Android में TextView को LinearLayout में डायनामिकली कैसे जोड़ें? Android में TextView को LinearLayout में डायनामिकली कैसे जोड़ें?

    यह उदाहरण दर्शाता है कि Android में Java कोड से TextView बनाकर LinearLayout में डायनामिकली कैसे जोड़ा जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML res/layout/activity_main.xml - खाली LinearLayout: <?xml version=1.0 encoding=utf-8?> <LinearLayout

  1. एंड्रॉइड में टेक्स्ट पर शैडो कैसे जोड़ें? एंड्रॉइड में टेक्स्ट पर शैडो कैसे जोड़ें?

    एंड्रॉइड में TextView पर टेक्स्ट शैडो लागू करना यह ट्यूटोरियल XML और प्रोग्रामेटिकली दोनों तरीकों से टेक्स्ट पर शैडो इफेक्ट जोड़ने को कवर करता है। विधि 1: XML एट्रिब्यूट्स (सबसे सरल) <TextView android:id=@+id/textView android:layout_width=wrap_content android:layout_height=wrap_conte

  1. Android TextView में लाइन ब्रेक (नई लाइन) कैसे जोड़ें? Android TextView में लाइन ब्रेक (नई लाइन) कैसे जोड़ें?

    Android TextView में लाइन ब्रेक जोड़ने के तरीके यह ट्यूटोरियल दिखाता है कि कैसे strings.xml में \n एस्केप सीक्वेंस या HTML <br> टैग का उपयोग करके TextView में बहु-लाइन टेक्स्ट प्रदर्शित किया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं। चरण