Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- button.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/hashtagbutton"
- android:textColor="#000000"
- android:textSize="12sp"
- android:text="xxx"
- android:background="@drawable/buttonshape"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- buttonshape.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
- <corners
- android:radius="5dp"
- />
- <solid
- android:color="#ffffff"
- />
- <padding
- android:left="0dp"
- android:top="0dp"
- android:right="0dp"
- android:bottom="0dp"
- />
- <size
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <stroke
- android:width="1dp"
- android:color="#cccccc"
- />
- </shape>
- main.java
- @Override
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_gifly);
- String[] Hashtags = new String[]{"a", "b", "c", "d"};
- createButtons(Hashtags);
- }
- private void createButtons(String[] Hashtags) {
- ArrayList<Button> buttons = new ArrayList<Button>();
- RelativeLayout bg = (RelativeLayout) findViewById(R.id.Hashtags);
- View view = (View)LayoutInflater.from(this).inflate(R.layout.button, null);
- Button buttonByXml = (Button) view.findViewById(R.id.hashtagbutton);
- Integer i = 0;
- for(String Hashtag : Hashtags){
- Button newButton = new Button(this);
- newButton.setId(i + 1);
- newButton.setText("#" + Hashtag);
- newButton.setLayoutParams(new LayoutParams(buttonByXml.getLayoutParams()));
- buttons.add(newButton);
- RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
- if(i > 0) {
- int id = buttons.get(i - 1).getId();
- lp.addRule(RelativeLayout.RIGHT_OF, id);
- }
- i++;
- bg.addView(newButton,lp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement