I get the data and add a TextView in a loop, I can specify the internal indent, but there is a problem with the external indent))

Here is the code as I do:

 for (int i = 0; i < response.body().getInterests().getInterests().size(); i++) { com.google.android.flexbox.FlexboxLayout interestsLayout = (com.google.android.flexbox.FlexboxLayout) findViewById(R.id.interestsLayout); TextView interestsTextView = new TextView(UserPageActivity.this); interestsTextView.setText(userData.getInterests().getInterests().get(i).toString()); interestsTextView.setBackgroundResource(R.drawable.circle_button_gray); interestsTextView.setPadding(10,10,10,10); LinearLayout.LayoutParams textViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewLayoutParams.setMargins(8,8,8,8); interestsTextView.setLayoutParams(textViewLayoutParams); interestsLayout.addView(interestsTextView); } 

Thank!

  • one
    textViewLayoutParams.setMargins(16,16,16,16); - McDaggen
  • Thank you very much)), did not work, the distance does not change ... something is not right - dev.android

2 answers 2

Set the indentation through your LinearLayout.LayoutParams

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); TextView interestsTextView = new TextView(UserPageActivity.this); interestsTextView.setText(userData.getInterests().getInterests().get(i).toString()); interestsTextView.setBackgroundResource(R.drawable.circle_button_gray); interestsTextView.setPadding(10,10,10,10); params.setMargins(8,8,8,8); interestsTextView.setLayoutParams(params); interestsLayout.addView(interestsTextView); 
  • In the question it is - dev.android
  • All that goes in LinearLayout.LayoutParams raise above TextView . That is, do it before the announcement TextView - McDaggen
  • Nothing has changed - dev.android
  • LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) interestsTextView.getLayoutParams(); replace, and now it should definitely work - McDaggen
  • Not quite sure what to change? - dev.android

Solved the question by simply adding an empty component with a slightly larger size, i.e. set the indents that I need)). Perhaps this is not correct, but it works and I did not find another way out.

 for (int i = 0; i < response.body().getInterests().getInterests().size(); i++) { com.google.android.flexbox.FlexboxLayout interestsLayout = (com.google.android.flexbox.FlexboxLayout) findViewById(R.id.interestsLayout); TextView interestsTextView = new TextView(UserPageActivity.this); interestsTextView.setText(userData.getInterests().getInterests().get(i).toString()); interestsTextView.setBackgroundResource(R.drawable.circle_button_gray); interestsTextView.setPadding(10,10,10,10); LinearLayout.LayoutParams textViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); textViewLayoutParams.setMargins(8,8,8,8); interestsTextView.setLayoutParams(textViewLayoutParams); interestsLayout.addView(interestsTextView); TextView nullTextView = new TextView(UserPageActivity.this); nullTextView.setText(""); nullTextView.setPadding(5,10,5,20); nullTextView.setLayoutParams(params); interestsLayout.addView(nullTextView); }