SoCo
SoCo

Reputation: 1954

Android : How to adjust Textview

Hello i am trying to display the content i receive in an activity using TextView but it seems that TextView is overlapping a button that i have put in activity's UI.My goal is to put TextView and the button side by side. I have put the TextView in the UI dynamically like this:

String display = extras.getString("EXTRA_MESSAGE");


    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setWidth(20);
    textView.setHeight(20);
    textView.setText(display);

    setContentView(textView);

I know i miss something but i cant find what it is,so can you please suggest a way how to fix that?

Thanks a lot in advance!

Upvotes: 0

Views: 59

Answers (1)

codeMagic
codeMagic

Reputation: 44571

When you call setContentView(textView); it changes your layout to just the textView so it isn't overlapping your Button but your Button isn't shown anymore. You need to add it to your layout and put it where you want it.

You can do this by getting a reference to your root View in your xml and calling addView(textVie) on that root View and use addRule() to position your TextView where you want. However, if it isn't necessary to add your TextView dynamically then it is much easier to declare it in your xml.

If you do want to add it dynamically still, then this SO answer, and many more, covers it.

Upvotes: 1

Related Questions