Button that generates another Button in android studio

Button that generates another Button in android studio

android button onclick example
android button click event
android button xml
how to check which button is clicked in android
multiple onclick events for one button android
android studio button onclick new activity
android studio button design
image button in android

so the question is, how can i make a buttonGenerator in android studio (with a unique Text, unique ID and unique name [for exanple unsig a for loop, to pass their names]). I have been tested some coding, but I've got always some errors (for example: where on the layer should the created Button appear, ...) and every time that the user clicks on the ButtonGenerator, the created button should appear underneath the last created button ( for example in a LinearLayout). I've tried it with visible and invisible, but i assume it is not the right way to do it. I have already a Button and everytime that the user clicks on that button, another Button will be created.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //buttons
    btnCreate= (ImageButton)findViewById(R.id.btnCreateID);
    btnDelete= (ImageButton)findViewById(R.id.btnDeleteID);
    btnCheck=(ImageButton)findViewById(R.id.btnCheckID);
    getTime = (ImageButton) findViewById(R.id.getTimeID);


    //Textviews
    txt= (TextView)findViewById(R.id.txtID);
    timeText= (TextView)findViewById(R.id.timeTextID);

    //image
    numImage = (ImageView)findViewById(R.id.numImageID);

    //Edittexts
    getTxt= (EditText)findViewById(R.id.getTxtID);

    //change visibility
    btnDelete.setVisibility(View.INVISIBLE);
    getTime.setVisibility(View.INVISIBLE);
    btnCheck.setVisibility(View.INVISIBLE);
    txt.setVisibility(View.INVISIBLE);
    timeText.setVisibility(View.INVISIBLE);
    getTxt.setVisibility(View.INVISIBLE);
    numImage.setVisibility(View.INVISIBLE);



    btnCreateClick();
}

//hit Add
public void btnCreateClick() {

    btnCreate.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            getTxt.setVisibility(View.VISIBLE);
            getTime.setVisibility(View.VISIBLE);
            btnCheck.setVisibility(View.VISIBLE);

            btnCreate.setVisibility(View.INVISIBLE);

            onGetTimeClick();
            btnCheckClick();
            btnDeleteClick();

        }
    });
}

You can create a Button and add it to a Layout like the following:

Button myButton = new Button(this);
myButton.setText(name);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
ll.addView(myButton);

If the layout is a vertical layout the new buttons will be added underneath the existing Buttons.

How to generate buttons by clicking on another button in Android , Work with observable data objects · Generated binding classes A button consists of text or an icon (or both text and an icon) that For example, here's a layout with a button using android:onClick : The appearance of your button (​background image and font) may vary from one device to another,  Buttons. A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it. Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways: With text, using the Button class: <Button android:layout_width="wrap_content"


You can create a Button and add it to a Layout like the following:

Button myButton = new Button(this);
myButton.setText(name);
myButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
myButton.setId(generateButtonId());
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
ll.addView(myButton);

If the layout is a vertical layout the new buttons will be added underneath the existing Buttons.

These are functions for generating unique Id

private int generateButtonId(){
    int id = getRandomNumber();
   try {
       String name = getResources().getResourceName(id);
   }catch (Resources.NotFoundException ex) {
       return id;
   }
    return generateButtonId();

}

private int getRandomNumber() {
    int min = 10;
    int max = 3000;

    Random r = new Random();
    int id = r.nextInt(max - min + 1) + min;
    return id;
}

Below are references I utilized

Checking to see if an ID exists in resources (R.id.something)

How can I generate random number in specific range in Android?

Buttons, public class Button The above snippet creates an instance of View. which is often different from one version of the platform to another. You can also create another activity in the Android Studio using the File menu option. File -> New -> Activity -> Empty Activity & step 2. But the First method will be easy and quick when you are coding…. You can also create Activity by another method. This is more advanced if you are a beginner.


So, this worked for me andd thanks 4 ur answers:

Button btn;
private Button[] newBTN;
LinearLayout linearLayout;
static int i=1;
static LinearLayout.LayoutParams params;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn= (Button)findViewById(R.id.addBTN);
    btn.setText("Add");
    linearLayout = (LinearLayout) findViewById(R.id.linear);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);



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

            newBTN = new Button[99];
            newBTN[i] = new Button(MainActivity.this);

            //params.addRule(LinearLayout.BELOW, R.id.i);


            newBTN[i].setLayoutParams(params);
            newBTN[i].setText("Button "+i);
            newBTN[i].setId(i);

            //add button to the layout
            linearLayout.addView(newBTN[i]);


            Toast.makeText(MainActivity.this, "Button"+ +i+"/ ID "+String.valueOf(newBTN[i].getId()), Toast.LENGTH_SHORT).show();
            i++;
        }
    });

}

Button, To display a button in an activity, add a button to the activity's layout XML file: The above snippet creates an instance of android.view. button background, which is often different from one version of the platform to another. Another example is that when the application already knows that the expected input is not a valid  Button Tutorial With Examples In Android Studio. In Android, Button represents a push button. A Push buttons can be clicked, or pressed by the user to perform an action. There are different types of buttons used in android such as CompoundButton, ToggleButton, RadioButton.


4.1: Buttons and clickable images · GitBook, Another example is that when you first enter a room, you expect the light You use the Button class to make a button for an Android app. The individual fingers or other objects that generate movement traces are referred to as pointers​. Click Here to Download Open new activity on button click in android by existing activity project. Related Posts. Generate unique random number android example code. Pass EditText value to another activity in android. Create Option Menu on hardware button pressed in Android Studio example tutorial.


Creating a Button: Android Programming, Launch Android Studio; create simple hello-world app. Generate a log message when button Duration: 18:26 Posted: Jan 9, 2017 Android Button Maker is online tool to generate buttons code for Android Apps. Android API provide Drawable Resources where XML file defines geometric shape, including colors, border and gradients. These button is generating based on shape drawable XML code which load faster compare to normal PNG buttons.


Handling Click events in Button, It won't work if you use this event handler in fragment because onClick attribute only works in Activity or MainActivity. Example: filter_none. edit close. play_arrow. Implement the steps 3, 4 and 5 in the method responsible for handling the onClick event of the button already present in the layout. Make use of the parameterised constructor of Button. Button button = new Button(Context) Set the layoutParameters on the button. Call the addView() method on the parentLayout reference.