0

I had fetch xmlData to my app sucessfully, and I need to transform these data as button for user to select.

The question is... If my app just get 10 of data, I only want to show up 10 buttons in my scrollView. I had try to set button as invisible, but there will show those invisible button as a "blank button"...

Please give me a help, thanks guys.

himaChen
  • 67
  • 9

3 Answers3

0

You need to dynamically add the button for these kinds of operation.

Example

<ScrollView>

<LinearLayout
android:id="@+id/buttons_layout/>

</ScrollView>

inflate the buttons programatically

Linearlayout buttonsLayout = (LinearLayout) findViewByID(R.id.buttons_layout)

buttonsLayout.removeAllViews();

for(int i= 0; // based on how mych data you have
{
  Button button = new Button(context);
  button.setOnClickListener() //

  buttonsLayout.addView(button);
}
Prasanna Anbazhagan
  • 1,693
  • 1
  • 20
  • 37
0

You should use a custom listview instead of scrollview with a button as a row of listview and set adapter of listview when your data is receive from server.

vishal jangid
  • 2,967
  • 16
  • 22
-1

Set the Visibility of the button to "gone" and it will hide the button

button.setVisibility(View.GONE).

Here is a nice stackoverflow post about the difference between View.GONE and View.INVISIBLE: Android : difference between invisible and gone?

BUT

In your case if you want to dynamically display buttons you should use a ListView or a RecyclerView.

Here is a nice tutorial about how to create a RecyclerView.

http://www.truiton.com/2015/02/android-recyclerview-tutorial/

Community
  • 1
  • 1
Nika Kurdadze
  • 2,502
  • 4
  • 18
  • 28
  • @himaChen It is not the right way to to use visibility to dynamically display buttons – Prasanna Anbazhagan Nov 13 '15 at 09:16
  • In the first part of the answer I showed the right way to hide a view. In the second part I mentioned the best solution in this particular situation. So I don't get your comment and the downvote too. – Nika Kurdadze Nov 13 '15 at 10:52