0

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!

SoCo
  • 1,934
  • 2
  • 15
  • 20

1 Answers1

1

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.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • thank you very much for you help :) do you know where i can find a solution for this using the xml way? – SoCo Sep 04 '13 at 18:03
  • I'm not sure what you mean. What part don't you know how to do? Create a `TextView` as you have your `Button`, create a reference to it and initialize it like you would, then call `setText()` on it – codeMagic Sep 04 '13 at 18:08
  • sorry silly question ! i am very new to android development and i am easily confused:P anyway thanks a lot for your time! – SoCo Sep 04 '13 at 18:12
  • No problem...it happens. I still get confused – codeMagic Sep 04 '13 at 18:14