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

How can I dynamically set the position of view in android?


অ্যান্ড্রয়েডে ডাইনামিকভাবে ভিউয়ের অবস্থান সেট করা

এই উদাহরণটি দেখায় কিভাবে RelativeLayout.LayoutParams এবং addRule() ব্যবহার করে রানটাইমে একটি TextView-এর অবস্থান বাটনের নিচে পরিবর্তন করতে হয়।

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

Android Studio-তে File → New Project-এ যান এবং প্রজেক্ট তৈরির জন্য প্রয়োজনীয় বিবরণ পূরণ করুন।

ধাপ ২: লেআউট ফাইল সংশোধন করুন (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:id="@+id/relativeLayout"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textStyle="bold|italic"
        android:text="Sample TextView"
        android:padding="10dp" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Put text below this button"
        android:layout_centerInParent="true" />
</RelativeLayout>

ধাপ ৩: MainActivity.java-তে কোড যোগ করুন

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button button;
    TextView textView;
    RelativeLayout relativeLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        relativeLayout = findViewById(R.id.relativeLayout);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) textView.getLayoutParams();
                layoutParams.addRule(RelativeLayout.BELOW, button.getId());
                layoutParams.addRule(RelativeLayout.ALIGN_LEFT, button.getId());
                textView.setLayoutParams(layoutParams);
            }
        });
    }
}

ধাপ ৪: 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>
                
                
            </intent-filter>
        </activity>
    </application>
</manifest>

অ্যাপ্লিকেশন চালান

আপনার অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে ধরে নেওয়া হয়েছে। Android Studio-তে প্রজেক্টের activity ফাইল খুলুন এবং টুলবারের Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।

How can I dynamically set the position of view in android?

How can I dynamically set the position of view in android?

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

  2. অ্যান্ড্রয়েডে প্রোগ্রামেটিক্যালি ব্যাকগ্রাউন্ড ড্রয়েবল সেট করা

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

  4. অ্যান্ড্রয়েডে বাটন অদৃশ্য বা মুছে ফেলার পদ্ধতি