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

How to control the height and width of the default alert dialog in android?


অ্যান্ড্রয়েডে ডিফল্ট AlertDialog-এর উচ্চতা এবং প্রস্থ নিয়ন্ত্রণ করা

এই উদাহরণটি দেখায় কিভাবে WindowManager.LayoutParams ব্যবহার করে AlertDialog-এর আকার ফুলস্ক্রিন বা কাস্টম সাইজে পরিবর্তন করতে হয়।

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

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">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Dialog"
        android:layout_centerInParent="true" />
</RelativeLayout>

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

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {
    Button button;
    RelativeLayout relativeLayout;
    Context context;
    Activity myActivity;

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

        relativeLayout = findViewById(R.id.relativeLayout);
        context = getApplicationContext();
        button = findViewById(R.id.button);
        myActivity = MainActivity.this;

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(myActivity);
                builder.setTitle("My Action Bar");
                builder.setMessage("This is my custom Action bar, do you like it?");
                builder.setPositiveButton("Yes", null);
                builder.setNegativeButton("No", null);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();

                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
                layoutParams.copyFrom(alertDialog.getWindow().getAttributes());
                layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
                alertDialog.getWindow().setAttributes(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 to control the height and width of the default alert dialog in android?

How to control the height and width of the default alert dialog in android?

How to control the height and width of the default alert dialog in android?

  1. অ্যান্ড্রয়েড এলার্ট ডায়ালগে ListView প্রদর্শনের পদ্ধতি

  2. অ্যান্ড্রয়েডে ঠিকানা থেকে ল্যাটিটিউড এবং লংগিটিউড বের করা

  3. How to show alert dialog in Android?

  4. অ্যান্ড্রয়েডে ImageView-এর উচ্চতা ও প্রস্থ কীভাবে পেতে হয়?