0

I am trying to create a button for each year since the person started using my app. So in my xml document I have

 <HorizontalScrollView
    android:id="@+id/yearScrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:layout_gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/currentYear"
            android:tag="01"
            android:text="2015"
            android:paddingLeft="8.0dip"
            android:paddingRight="8.0dip"
            android:height="24dp"
            android:textSize="18sp"
            android:textColor="#333333"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white"
            >
        </Button>


    </LinearLayout>

</HorizontalScrollView>

Then I have the following code

private List<Button> yearButtons;
private static final int[] YEAR_BUTTON_IDS = {
        R.id.currentYear,
};

Then I must find out what year it is and overwrite my current button

        int firstYear = Integer.parseInt(year);
                      yearButtons.get(0).setText(String.valueOf(CurrentYear));

then in my init class I substantiate the buttons, I understand I do not need a loop for only 1 button but leaving it like this for consistency with how the months buttons work

for(int id : YEAR_BUTTON_IDS) {
        Button button = (Button)findViewById(id);
        button.setOnClickListener(this);
        yearButtons.add(button);
    }

Then I have some logic to find out the first year they started called yearsOfTransactions

Then I have the following loop where I try create the buttons

 for (int i =0; i< yearsOfTransactions; i++){
        int yearToAdd = CurrentYear-i-1;
        Button myButton = new Button(this);
        myButton.setText(String.valueOf(yearToAdd));
        yearButtons.add(myButton);

    }

However this is not working..

Thanks for any help

2
  • not working means?? you need to add it in your linear layout.. then it will get visible..!! Commented Jun 14, 2016 at 10:24
  • I suggest instead of taking int[] for ids you take Arraylist<Integer>.Because You can not modify array size after it is created once..!! Commented Jun 14, 2016 at 10:26

4 Answers 4

1

I am making slight modification in your code :

<HorizontalScrollView
    android:id="@+id/yearScrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:layout_gravity="center">

    <LinearLayout
        android:id="@+id/button_parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/currentYear"
            android:tag="01"
            android:text="2015"
            android:paddingLeft="8.0dip"
            android:paddingRight="8.0dip"
            android:height="24dp"
            android:textSize="18sp"
            android:textColor="#333333"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white"
            >
        </Button>


    </LinearLayout>

</HorizontalScrollView>

Now make an array of years to add (in onCreate())

int[] yearToAdd = {2000, 2001, 2002, 2003};
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.button_parent);
for (int i =0; i< yearToAdd.lenght; i++){
        int yearToAdd = yearToAdd[i];
        Button myButton = new Button(this);
        myButton.setText(String.valueOf(yearToAdd));
        yearButtons.add(myButton);
        yearButtons.setOnClickListener(this);
        parentLayout.addView(myButton);
 }

Let me know in case of more clearation you need, hope it will help :)

Sign up to request clarification or add additional context in comments.

Comments

0

you have to add button to a linear layout. This is a function i use to add dynamic buttons in a linear layout.

public Button createButton(String label) {
    Button button = new Button(mContext);
    button.setText(label);
    mDynamicLayout.addView(button, mLayoutParam);
    return button;
}

Comments

0

You should add every button you create to the LinearLayout. First set and id in xml to LinearLayout. Find the view in your class and, finally, add the buttons.

LinearLayout container = findViewById(R.id.container);

//...

for (int i =0; i< yearsOfTransactions; i++){
    int yearToAdd = CurrentYear-i-1;
    Button myButton = new Button(this);
    myButton.setText(String.valueOf(yearToAdd));
    yearButtons.add(myButton);

   // you missed that
    container.addView(myButton);
}

Comments

0
please try this inside your for loop

   LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);

    //set the properties for button
    Button btnTag = new Button(this);
    btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    btnTag.setText("Button");
    btnTag.setId(some_random_id);

    //add button to the layout
    layout.addView(btnTag);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.