Posted By

BenClayton on 03/18/12



Versions (?)

Dynamically add widget / TextView to a RelativeLayout in code

 / Published in: Java

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

  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  

You need to login to post a comment.