Dynamically add widget / TextView to a RelativeLayout in code


/ Published in: Java
Save to your folder(s)

This example adds a horizontally-centered TextView to the bottom of the RelativeLayout called 'layout'.


Copy this code and paste it in your HTML
  1. TextView tv = new TextView(this);
  2. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  3. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  4. tv.setLayoutParams( layoutParams );
  5. tv.setText(getString(R.string.loading));
  6. tv.setGravity(Gravity.CENTER_HORIZONTAL);
  7. layout.addView(tv);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.