Adding a button for Android in Java code -


is possible add button activity layout java code. if possible, how? 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 possible have java-added button inside linearlayout inside of scrollview, if isn't possible possible in normal linearlayout.

the reason why want able buttons through java have array contains several objects. every object have button. array increase in size on time.

this 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 want add buttons through java since can't in xml. possible , if how?

yes possible, define id linearlayout in xml file. lets say:

android:id="@+id/buttoncontainer" 

then in activity java code find id after setting contentview:

linearlayout buttoncontainer = (linearlayout) findviewbyid(r.id.buttoncontainer); 

then create button:

button button = new button(); 

customize like, given methods provided.

and add layout:

buttoncontainer.addview(button); 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -