Wuppy29
Wuppy29

Reputation: 347

Adding a button for Android in Java code

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?

Upvotes: 4

Views: 43138

Answers (3)

Anas Imran Tasadduq
Anas Imran Tasadduq

Reputation: 594

This link gives a full tutorial on how to add widgets to your Android app using java code. So, here's a summary for your button thing:

1) You remove the setContentView(...) line from the onCreate() method

2) You define your button like this: Button myButton = new Button(this);

3) You define a layout like this: RelativeLayout myLayout = new RelativeLayout(this);

4) You add the button to the layout like this: myLayout.addView(myButton);

5) You set the layout like this: setContentView(myLayout);

To add a button to a layout that is already defined by the .xml file, which is in your case, you write the following code instead of the above five steps:

LinearLayout layout = (LinearLayout) findViewById(R.id.myLayout);
Button button = new Button(this);
layout.addView(button);

Upvotes: 2

No_Rulz
No_Rulz

Reputation: 2719

Add the appropriate import to your Activity:

import android.widget.Button;

Then create a new button object within the onCreate method:

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

Finally add the button to the layout:

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

Upvotes: 2

Emil Adz
Emil Adz

Reputation: 41099

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);

Upvotes: 5

Related Questions