Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] How do I add button to another activity in android studio using java code rather than xml code

Posted on November 14, 2022

Solution 1 :

I am guessing you need the button to be created when the activity pop_cart.java is in the foreground.
In that case why don’t you directly execute the button code in the onCreate() of pop_cart.java?

You can pass a boolean from pop_accompaniments.java to pop_cart.java via an intent if you want something in pop_accompaniments.java to decide whether the button will be created or not!

In pop_accompaniments.java:

Intent intent = new Intent(this, pop_cart.class);
intent.putExtra("someBool", true);

In pop_cart.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
Boolean bool = getIntent().getExtras().getBoolean("someBool");

//Now you can check if the bool is true or false
if(bool){
    //Execute button code here
}
}

Problem :

I want to add a button from 1 activity – “pop_accompaniments.xml” to another activity – “pop_cart.xml“, using java code. For this I created an instance of the class “pop_cart.java” in “pop_accompaniments.java“, to call the func – “cart_button_creator” which is in “pop_cart.java“.
But my app just crashes as soon as I open it.

pop_accompaniments.java

package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class pop_accompaniments extends AppCompatActivity implements View.OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pop_accompaniments);

        Button assorted_condiments = findViewById(R.id.assorted_condiments);
    }

    @Override
    public void onClick(View v) {
        setContentView(R.layout.pop_cart);
        pop_cart main = new pop_cart();
        int id;
        String name;
        Button myButton;
        switch (v.getId()) {
            case R.id.assorted_condiments:
                myButton = new Button(this);
                name = "Assorted Condiments";
                id = generateButtonId();
                main.cart_content_keeper(id, name, myButton);
        }
    }
}

pop_cart.java

package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class pop_cart extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.pop_cart);
    }

    public void cart_button_creator(String name, int id, Button myButton) {
        //setContentView(R.layout.pop_cart);
        LinearLayout ll = findViewById(R.id.cart_items);
        myButton.setText(name);
        myButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        myButton.setId(id);
        ll.addView(myButton);
    }


    public void cart_content_keeper(int id, String name, final Button myButton) {
        int n = 0;
        int[] id_arr = {};
        String[] name_arr = {};
        //int price = 0;

        name_arr = add_item(n, name_arr, name);
        id_arr = add_id(n, id_arr, id);
        n = n + 1;

        Button refresh = findViewById(R.id.refresh);
        final int finalN = n;
        final String[] finalName_arr = name_arr;
        final int[] finalId_arr = id_arr;
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.refresh) {
                    for (int i = 0; i < finalN; i++) {
                        cart_button_creator(finalName_arr[i], finalId_arr[i], myButton);
                    }
                }
            }
        });
    }


    public static String[] add_item(int n, String[] arr, String x)
    {
        int i;

        String[] newarr = new String[n + 1];

        for (i = 0; i < n; i++)
            newarr[i] = arr[i];

        newarr[n] = x;

        return newarr;
    }


    public static int[] add_id(int n, int[] arr, int x)
    {
        int i;

        int[] newarr = new int[n + 1];

        for (i = 0; i < n; i++)
            newarr[i] = arr[i];

        newarr[n] = x;

        return newarr;
    }
}

Sorry for such a long code but I have tried almost all the possible ways of calling a func from an instance of another class, passing the variables in a different manner, trying to call the whole variable generator as a function and only passing the view v as an argument in pop_accompaniments.java but the app keeps in crashing either when I click on the button “ASSORTED CONDIMENTS” at the very first step or if I just start the app it crashes.

READ  [ANSWERED] java - Retrieve data to ListView from API in android
Powered by Inline Related Posts

Also, the buttons are created just fine if I am creating them in the same activity that is “pop_accompaniments.xml” while calling the button_creator method from “pop_accompaniments.java“.

But I want them to show in the CART activity not the ACCOMPANIMENTS activity.

Comments

Comment posted by Vipul Bhardwaj

No still the same error persists. The app is crashing as soon as it is launched. It doesn’t even open. I have checked and there are no other errors in my code that android studio my highlight as red or even yellow warnings.

Comment posted by Harsh Savergaonkar

Can you please share the log when you run the app? That way I will be able to help you better.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress