Adding a button for Android in Java code
android, button, java
Solution
Yes it possible, define an id to your LinearLayout in your XML file. lets say:
android:id="@+id/buttonContainer"
Then in the Activity java code find this id after setting the contentView:
LinearLayout buttonContainer = (LinearLayout) findViewById(R.id.buttonContainer);
Then create your button:
Button button = new Button();
customize it as you like, given the methods provided.
And finally add it to your layout:
buttonContainer.addView(button);
Problem
Is it possible to add a button to an Activity layout with Java code. If this is possible, how? This is my current layout file: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ad_catalog_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" android:orientation="vertical" > <com.google.ads.AdView xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads" android:id="@+id/ad" android:layout_width="wrap_content" android:layout_height="wrap_content" googleads:adSize="IAB_BANNER" googleads:adUnitId="a14d7f7d2180609" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/menu_mods" android:textColor="#FFFFFF" android:textSize="25sp" /> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="enterPeacefulPack" android:text="@string/peacefulpack" android:paddingBottom="20dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="20dp" android:textColor="#FFFFFF" android:textSize="25sp" /> </LinearLayout> </ScrollView> ``` If it is possible I would like to have the Java-added button inside the LinearLayout that is inside of the ScrollView, but if that isn't possible it would also be possible to get it in the normal LinearLayout. The reason why I want to be able to get buttons through Java is that I have an Array that contains several objects. For every object I would like to have a button. This Array will increase in size over time. This is the Activity file I'm using ``` package com.wuppy.minecraftmods.mods; import android.annotation.SuppressLint; import android.app.ActionBar.LayoutParams; import android.app.Activity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.view.MenuItem; import android.view.View; import android.widget.Button; import com.google.ads.AdRequest; import com.google.ads.AdView; import com.wuppy.minecraftmods.R; public class Mods extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mods); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { getActionBar().setDisplayHomeAsUpEnabled(true); } AdView adView = (AdView) this.findViewById(R.id.ad); adView.loadAd(new AdRequest()); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } public void enterPeacefulPack(View view) { Intent intent = new Intent(this, ModPeacefulpack.class); startActivity(intent); } } ``` So I want to add buttons through Java since I can't really do this in xml. Is that possible and if so how?