অ্যান্ড্রয়েডে গতিশীলভাবে একটি বোতাম কীভাবে যুক্ত করবেন?


119

অ্যান্ড্রয়েডে গতিশীলভাবে একটি বোতাম কীভাবে যুক্ত করবেন?

উত্তর:


128
Button myButton = new Button(this);
myButton.setText("Push Me");

LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);

করার জন্য একটি চেহারা আছে এই উদাহরণ


1
আমি ইউআরএল আপডেট করেছি কারণ পুরানোটি 404 দিয়েছে Please দয়া করে পরীক্ষা করুন যে আমি সঠিক পৃষ্ঠাটি উল্লেখ করেছি।
বিল

1
আপনি কি পুরোপুরি যোগ্যতা অর্জন করতে পারেন LayoutParams? আমি এই নামে 12 টিরও বেশি ক্লাস দেখতে পাচ্ছি।
সা Saeedদ নেমতি

1
@ সাeedদ এই নির্দিষ্ট উদাহরণে এটি লিনিয়ারালাউট সম্পর্কিত লেআউটপ্যারাম। android.widget.LinearLayout.LayoutParams
বাসড্রপ কম্বারউবউবউবব

54

এটা চেষ্টা কর:

for (int i = 1; i <= 20; i++) {
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    Button btn = new Button(this);
    btn.setId(i);
    final int id_ = btn.getId();
    btn.setText("button " + id_);
    btn.setBackgroundColor(Color.rgb(70, 80, 90));
    linear.addView(btn, params);
    btn1 = ((Button) findViewById(id_));
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Toast.makeText(view.getContext(),
                    "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                    .show();
        }
    });
}

1
বোতামটি কেন যুক্ত করা হয়, তারপরে ক্লিক শ্রোতাদের সেট করার আগে পেয়ে যান। আপনি শ্রোতাদের যুক্ত করতে পারবেন না, তারপরে এটিকে বিন্যাসে যুক্ত করুন এবং হয়ে গেলেন?
rwilson04

সেরা উত্তর কারণ আপনি একটি আইডি সেট করে বোতামগুলিতে ক্লিক করতে পারেন :)
বক্টুলাস

10

এটা চেষ্টা কর:

LinearLayout ll = (LinearLayout)findViewById(R.id.layout);

Button btn = new Button(this);
btn.setText("Manual Add");
btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(btn);

আর.আইডি.আলআউট কী এখানে উল্লেখ করছে? অমীমাংসিত হিসাবে আমি লেআউটটি পেতে
আনা গোল্ডবার্গ

আপনার প্রাসঙ্গিক এক্সএমএল ফাইলে @ আন্নাগল্ডবার্গ আপনার একটি লিনিয়ারলআউট সংজ্ঞায়িত হওয়া উচিত। এই অংশে তারা তাদের লিনিয়ারলআউটকে একটি লেআউট নামক একটি আইডি দিয়েছে, যেমন: android:id="@+id/layout"তাদের লিনিয়ারলআউট এক্সএমএল সংজ্ঞায়।
অ্যামিগা

6

এটা চেষ্টা কর

private void createLayoutDynamically(int n) {

    for (int i = 0; i < n; i++) {
        Button myButton = new Button(this);
        myButton.setText("Button :"+i);
        myButton.setId(i);
        final int id_ = myButton.getId();

        LinearLayout layout = (LinearLayout) findViewById(R.id.myDynamicLayout);
        layout.addView(myButton);

        myButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(DynamicLayout.this,
                        "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }

6
for (int k = 1; k < 100; k++) {
    TableRow row = new TableRow(this);

    innerloop:
    for (int l = 1; l < 4; l++) {
        btn = new Button(this);
        TableRow.LayoutParams tr = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layout.setWeightSum(12.0f);
        tr.weight = 0;
        btn.setLayoutParams(tr); 
        btn.setTextColor(a);
        btn.setHeight(150);

        btn.setWidth(150);
        btn.setId(idb);
        btn.setText("Button " + idb);
        row.addView(btn);
    }
}

5

এই কোড ব্যবহার করে দেখুন

 Button btn=new Button(this);
btn.setId(btn);
btn.setBackgroundResource(R.drawable.image);
btn.setMinimumHeight(150);
btn.setMinimumWidth(150);
Relativelayout.addView(btn); 

4

এই কোড ব্যবহার করে দেখুন। এটা ঠিক কাজ করবে ..

public class DynamicViewsActivity extends Activity {

Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_dynamic_views);
    ScrollView scrl=new ScrollView(this);
    final LinearLayout ll=new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(100, 500, 100, 200);
    scrl.addView(ll);
    Button add_btn=new Button(this);
    add_btn.setText("Click Here");

    ll.addView(add_btn, layoutParams);


    final Context context = this;

    add_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(context, App2Activity.class);
            startActivity(intent);
        }
    });
    this.setContentView(scrl);
}
}

4

এটি পরীক্ষা করে দেখুন।

LinearLayout ll_Main  = new LinearLayout(getActivity());
LinearLayout ll_Row01 = new LinearLayout(getActivity());
LinearLayout ll_Row02 = new LinearLayout(getActivity());

ll_Main.setOrientation(LinearLayout.VERTICAL);
ll_Row01.setOrientation(LinearLayout.HORIZONTAL);
ll_Row02.setOrientation(LinearLayout.HORIZONTAL);

final Button button01    = new Button(getActivity());
final Button button02    = new Button(getActivity());   
final Button button03    = new Button(getActivity());
final Button button04    = new Button(getActivity());

ll_Row01.addView(button01);
ll_Row01.addView(button02);

ll_Row02.addView(button03);
ll_Row02.addView(button04);

ll_Main.addView(ll_Row01);
ll_Main.addView(ll_Row02);

button04.setVisibility(View.INVISIBLE);
button04.setVisibility(View.VISIBLE);

3

নিম্নলিখিত কোড চেষ্টা করুন।

LinearLayout layout = (LinearLayout) findViewById(R.id.llayout); 
layout.setOrientation(LinearLayout.VERTICAL);

Button btn = new Button(this);
btn.setText("Button1");

layout.add(btn);

btn = new Button(this);
btn.setText(Button2);
layout.add(btn);

এটি আপনার প্রয়োজনীয়তা অনুসারে বাটনগুলি যুক্ত করুন।


3
public void add_btn() {

    lin_btn.setWeightSum(3f);
    for (int j = 0; j < 3; j++) {
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params1.setMargins(10, 0, 0, 10);
        params1.weight = 1.0f;

        LinearLayout ll;
        ll = new LinearLayout(this);
        ll.setGravity(Gravity.CENTER_VERTICAL);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setLayoutParams(params1);

        final Button btn;
        btn = new Button(DynamicActivity.this);

        btn.setText("A"+(j+1));
        btn.setTextSize(15);
        btn.setId(j);
        btn.setPadding(10, 8, 10, 10);

        ll.addView(btn);

        lin_btn.addView(ll);


        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if(v.getId()==0)
                {
                    txt_text.setText("Hii");
                }else if(v.getId()==1)
                {
                    txt_text.setText("hello");
                }else if(v.getId()==2)
                {
                    txt_text.setText("how r u");
                }



            }
        });
    }

}

2

আসলে আমি এক্সএমএল লেআউট ফাইলটিতে যা কিছু ব্যবহার করা যায় তা যুক্ত করি! তারপরে নির্দিষ্ট ক্রিয়াকলাপের উত্স কোড থেকে আমি তার আইডি দ্বারা অবজেক্টটি পাই এবং আমি দৃশ্যমানতা পদ্ধতিতে "খেলি"।

এখানে একটি উদাহরণ:

((Spinner)findViewById(R.id.email_spinner)).setVisibility(View.GONE);


1
এটি জিজ্ঞাসা করা হয় না। লোকেরা যখন গতিশীল যোগ করার বিষয়ে জিজ্ঞাসা করে তখন তাদের বিন্যাস ছাড়াই বোঝানো হয়। আপনি যা করছেন তা লুকিয়ে রাখছে ... যোগ করা হচ্ছে না।
মোগ

2

আমি এই (বা খুব অনুরূপ) কোডটি লিনিয়ারলয়েটে বেশ কয়েকটি পাঠ্য ভিউ যুক্ত করতে ব্যবহার করেছি:

// Quick & dirty pre-made list of text labels...
String names[] = {"alpha", "beta", "gamma", "delta", "epsilon"};
int namesLength = 5;

// Create a LayoutParams...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.FILL_PARENT);

// Get existing UI containers...
LinearLayout nameButtons = (LinearLayout) view.findViewById(R.id.name_buttons);
TextView label = (TextView) view.findViewById(R.id.master_label);

TextView tv;

for (int i = 0; i < namesLength; i++) {
    // Grab the name for this "button"
    final String name = names[i];

    tv = new TextView(context);
    tv.setText(name);

    // TextViews CAN have OnClickListeners
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            label.setText("Clicked button for " + name); 
        }
    });

    nameButtons.addView(tv, params);
}

এটি এবং ডিক্ল্যাভ 95৯৯ এর কোডের মধ্যে প্রধান পার্থক্য হ'ল এটি প্রতিটি পাঠ্যদর্শনটির জন্য আইডি সেট () এবং পুনরায় পাবেন না - আমি এটি অপ্রয়োজনীয় বলে মনে করি, যদিও সাধারণ হ্যান্ডলারের রুটিনে প্রতিটি বোতাম পরে শনাক্ত করার জন্য আমার এটি প্রয়োজন হতে পারে ( যেমন প্রতিটি পাঠ্য ভিউয়ের জন্য অনক্লিক () নামে পরিচিত একজন।


2
Button btn = new Button(this);
btn.setText("Submit");
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams buttonlayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(btn, buttonlayout);

1
Button myButton = new Button(this);
myButton.setId(123);
myButton.setText("Push Me");


LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
 myButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(DynamicLayout.this,
                        "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                        .show();
            }
        });

1

আপনি যদি গতিশীল বোতাম যুক্ত করতে চান তবে এটি ব্যবহার করে দেখুন:

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    for (int i = 1; i <= 5; i++) {
        LinearLayout layout = (LinearLayout) findViewById(R.id.myLinearLayout);
        layout.setOrientation(LinearLayout.VERTICAL);
        Button btn = new Button(this);
        btn.setText("    ");
        layout.addView(btn);
    }

}

0

আপনি আপনার বোতামের জন্য একটি বেস লেআউট তৈরি করতে পারেন এবং কেবলমাত্র নির্দিষ্টটি পরিবর্তনশীলভাবে পরিবর্তন করতে পারেন, যেমনটি আমি গ্রহণযোগ্য একটি মেটালিয়াল ডিজাইন কোর্স থেকে বিভিন্ন অনুশীলন চালানোর জন্য তৈরি এই প্রকল্পটির মতো:

এই উদাহরণে, আমি একটি পূর্বনির্ধারিত অ্যাপকম্প্যাটবটন ব্যবহার করি:

layout_base_button.xml

<android.support.v7.widget.AppCompatButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/btn_base"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="8dp"
    style="@style/RaisedButton"
    >

</android.support.v7.widget.AppCompatButton>


<style name="RaisedButton" parent="Widget.AppCompat.Button.Colored">
    <item name="android:textSize">11sp</item>
    <item name="android:textStyle">bold</item>
</style>

এবং MainActivityআমি কিছু দৃষ্টান্ত তৈরি করেছি এবং আমার যা প্রয়োজন তা পরিবর্তন করেছি, যেমন বোতামের পাঠ্য এবং অনক্লিক ইভেন্ট:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="udemy.android.materialdesign.MainActivity">

    <LinearLayout
        android:id="@+id/base_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

    </LinearLayout>


</ScrollView>



public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout baseLayout = findViewById(R.id.base_layout);

        baseLayout.addView(createButton("TextFields", baseLayout,
                view -> startActivity(createIntent(TextFieldsActivity.class))
        ));

        baseLayout.addView(createButton("Buttons", baseLayout,
                view -> startActivity(createIntent(ButtonsActivity.class))
        ));

        baseLayout.addView(createButton("Toolbar", baseLayout,
                view -> startActivity(createIntent(ToolbarActivity.class))
        ));

    }

    private View createButton(String text, LinearLayout baseLayout, View.OnClickListener onClickEvent) {
        View inflated = LayoutInflater.from(this).inflate(R.layout.layout_base_button, baseLayout, false);
        AppCompatButton btnBase = inflated.findViewById(R.id.btn_base);

        btnBase.setText(text);
        btnBase.setOnClickListener(onClickEvent);
        return btnBase;
    }

    private Intent createIntent(Class<?> cls) {
        return new Intent(this, cls);
    }
}

দেরি করার জন্য দুঃখিত...


-4

ইন mainactivity.xmlলেখ:

<Button
    android:id="@+id/search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Search" 
    android:visibility="invisible"/>

ইন main.javaলেখ:

Button buttonSearch;
buttonSearch = (Button)findViewById(R.id.search);
buttonSearch.setVisibility(View.VISIBLE);
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.