Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. button.xml
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>
  4.  
  5. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  6.     android:orientation="vertical" android:layout_width="match_parent"
  7.     android:layout_height="match_parent">
  8.  
  9.  
  10.     <Button xmlns:android="http://schemas.android.com/apk/res/android"
  11.         android:id="@+id/hashtagbutton"
  12.         android:textColor="#000000"
  13.         android:textSize="12sp"
  14.         android:text="xxx"
  15.         android:background="@drawable/buttonshape"
  16.         android:layout_width="wrap_content"
  17.         android:layout_height="wrap_content"
  18.         />
  19. </LinearLayout>
  20.  
  21. buttonshape.xml
  22.  
  23. <?xml version="1.0" encoding="utf-8"?>
  24. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
  25.     <corners
  26.         android:radius="5dp"
  27.         />
  28.     <solid
  29.         android:color="#ffffff"
  30.         />
  31.     <padding
  32.         android:left="0dp"
  33.         android:top="0dp"
  34.         android:right="0dp"
  35.         android:bottom="0dp"
  36.         />
  37.     <size
  38.         android:layout_width="wrap_content"
  39.         android:layout_height="wrap_content"
  40.         />
  41.     <stroke
  42.         android:width="1dp"
  43.         android:color="#cccccc"
  44.         />
  45. </shape>
  46.  
  47. main.java
  48.  
  49.  
  50.  
  51.     @Override
  52.     protected void onCreate(Bundle savedInstanceState){
  53.         super.onCreate(savedInstanceState);
  54.         setContentView(R.layout.activity_gifly);
  55.  
  56.         String[] Hashtags = new String[]{"a", "b", "c", "d"};
  57.  
  58.         createButtons(Hashtags);
  59.  
  60.     }
  61.  
  62.     private void createButtons(String[] Hashtags) {
  63.  
  64.         ArrayList<Button> buttons = new ArrayList<Button>();
  65.         RelativeLayout bg = (RelativeLayout) findViewById(R.id.Hashtags);
  66.  
  67.         View view = (View)LayoutInflater.from(this).inflate(R.layout.button, null);
  68.         Button buttonByXml = (Button) view.findViewById(R.id.hashtagbutton);
  69.  
  70.         Integer i = 0;
  71.         for(String Hashtag : Hashtags){
  72.  
  73.             Button newButton = new Button(this);
  74.             newButton.setId(i + 1);
  75.             newButton.setText("#" + Hashtag);
  76.             newButton.setLayoutParams(new LayoutParams(buttonByXml.getLayoutParams()));
  77.  
  78.             buttons.add(newButton);
  79.  
  80.             RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
  81.             RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  82.  
  83.  
  84.             if(i > 0) {
  85.                 int id = buttons.get(i - 1).getId();
  86.                 lp.addRule(RelativeLayout.RIGHT_OF, id);
  87.             }
  88.             i++;
  89.  
  90.             bg.addView(newButton,lp);
  91.         }
  92.  
  93.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement