align text center with android
Set also android:gravity
parameter in TextView
to center
.
For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.
use this way
txt.setGravity(Gravity.CENTER);