Android TextView में पैराग्राफ की गणना करने का तरीका
यह उदाहरण दर्शाता है कि Android TextView में प्रदर्शित टेक्स्ट में पैराग्राफ की संख्या कैसे गिनी जाती है। डबल लाइन ब्रेक के आधार पर पैराग्राफ को विभाजित किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML में कोड जोड़ें
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginTop="100dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
चरण 3: MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्न कोड जोड़ें:
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
text.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
"Lorem Ipsum has been the industry's standard dummy text ever since the " +
"1500s, when an unknown printer took a galley of type and scrambled it to " +
"make a type specimen book. It has survived not only five centuries, but " +
"also the leap into electronic typesetting, remaining essentially unchanged. " +
"It was popularised in the 1960s with the release of Letraset sheets " +
"containing Lorem Ipsum passages, and more recently with desktop publishing " +
"software like Aldus PageMaker including versions of Lorem Ipsum.");
// डबल लाइन ब्रेक के आधार पर पैराग्राफ विभाजित करें
String[] para = text.getText().toString().split("\r\n\r\n");
Toast.makeText(MainActivity.this, "" + para.length, Toast.LENGTH_LONG).show();
}
}
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिवाइस पर डिफ़ॉल्ट स्क्रीन देखें।
