0

I'm trying to create an android app where users can divide up the courses they took in each year of college. The idea is to have an opening screen with the button "Add Year", and when it's clicked, it creates a button above or underneath it (doesn't really matter) that they can create a custom name for. The "Add year" button has to stay so that they can add as many years/semesters/w/e as necessary. I've been following this tutorial: http://developer.android.com/training/basics/firstapp/starting-activity.html ignoring the parts about adding a text field. When it gets the the part about making your button do something, the tutorial takes the user-entered text and displays it in a new xml.

I have minimal experience in java, and turn to Google whenever I get stuck, but I haven't found anything that would help my dilemma. Here is my activity_main.xml file:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/main_background"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <Button
            android:id="@+id/button_add_year"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="39dp"
            android:text="@string/button_add_year"
            android:onClick="addYear" />

    </RelativeLayout>

And my .java

            package com.bkarpinski.macgpatrack;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.*;

    public class MainActivity extends Activity {

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


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        /** Called when the user clicks the addYear button */
        public void addYear(View view) {
            Intent intent = new Intent(this, CreateNewYearButton.class);
        }
    }

Please help me! I have to present this Wednesday, and am stuck!! D:

  • What exactly is the problem? What is/isn't working? – codeMagic Dec 02 '13 at 23:50
  • It's working for now, but I don't know how to make the button create another button on the same screen. It calls the CreateNewYear.class, but the class is empty as I don't know where to proceed from there – theunheardvoice Dec 02 '13 at 23:51

2 Answers2

1

The easiest method is to hide the second button and then on the click of the first button make the second button visible first make the second button invisible with this

 <Button
   android:id="@+id/button2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
   android:layout_centerVertical="true"
   android:text="2ndButton"
   android:visibility="invisible" />

then use the OnClickListener method with this

addYearButton = (Button) findViewById(R.id.add);
2ndButton = (Button) findViewById(R.id.button2);

addYearButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
         //when addYear is clicked show the new button
         2ndButton.setVisibility(View.VISIBLE);

    }
});
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • could i use an if statement to make it add subsequent buttons? something like if(2ndbutton.visibility == true) 3rdButton.setVisibility(view.VISIBLE); ? – theunheardvoice Dec 03 '13 at 22:22
0

To add a new Button you can create it programmatically with something like

addYearButton = (Button) findViewById(R.id.add);

addYearButton.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) 
  {
      //when addYear is clicked show the new button
      Button b = new Button(v.getContext());  // create Button
      b.setText("Some text on Button");      //  Set the text of the Button
      // set params and add button to layout

    }
});

The second answer in this question shows how to add the Button and add params to it such as height and width. Also see the LayoutParam Docs.

If you will need an indeterminate number of Buttons depending on how many times the Button is clicked then you will probably want to use this. If you have a few set number of Buttons that can be added then the changing Visibility would work.

Edit

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addYearButton = (Button) findViewById(R.id.add);  
    }

This is where you should initialize your first Button and declare it before onCreate() with Button addYearButton;. Also, you will need a Button in activity_main.xml with the id of add.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93