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

Kotlin-এ Android-এ TextView-এর লেআউট ওয়েট প্রোগ্রামেটিক্যালি সেট করার পদ্ধতি


এই উদাহরণটি দেখায় কিভাবে Kotlin ব্যবহার করে Android-এ একটি TextView-এর লেআউট ওয়েট প্রোগ্রামেটিক্যালি সেট করা যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project নির্বাচন করে একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: activity_main.xml ডিজাইন করুন

res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন:

<?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"
    android:gravity="center"
    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" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="expand"
        android:text="Hello World"
        android:textSize="24sp"
        android:textStyle="bold" />
</RelativeLayout>

ধাপ ৩: MainActivity.kt-এ বাটন আকার বড় করার কোড লিখুন

src/MainActivity.kt ফাইলে নিচের কোডটি যোগ করুন:

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
    }

    fun expand(view: View) {
        val height = RelativeLayout.LayoutParams.WRAP_CONTENT
        val width = 200
        val layoutParams = RelativeLayout.LayoutParams(width, height)
        val button: Button = findViewById(R.id.button)
        button.layoutParams = layoutParams
    }
}

ধাপ ৪: AndroidManifest.xml কনফিগার করুন

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 Studio থেকে Run করুন। বাটনে ক্লিক করলে এর প্রস্থ ২০০ পিক্সেল হবে।

  1. অ্যান্ড্রয়েডে প্রোগ্রাম্যাটিক্যালি বাটনে drawableLeft সেট করার পদ্ধতি

  2. অ্যান্ড্রয়েড TextView-এর নির্দিষ্ট অংশকে ক্লিকযোগ্য করার পদ্ধতি

  3. অ্যান্ড্রয়েডে টেক্সটভিউয়ের লেআউট ওয়েট প্রোগ্রামেটিক্যালি সেট করা

  4. অ্যান্ড্রয়েডে TextView-এ স্প্যান ব্যবহার করে টেক্সটের রঙ পরিবর্তন করার পদ্ধতি