Android : How to adjust Textview - android

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!

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.

Related

Clarification regarding setContentView

What is difference between:
setContentView(R.layout.activity_main);
TextView txtView = (TextView)findViewById(R.id.text);
And
TextView textView = new TextView(this);
setContentView(textView);
I found these two pieces of code. In first the setContentView has a I'd passed to it about layout. And in second case it has a view passed as argument. Is textView also an id. I think the difference is that in first case, it is layout of activity_main as described in XML file(which contains textView as well) and in second case it is id of textView. Tell me if I am correct.
Also tell me what does 'this' refer to here. Why we are using findViewById in first case?
In the first code,
setContentView(R.layout.activity_main);
TextView txtView = (TextView)findViewById(R.id.text);
you are setting the content view of the container and then trying to access the view with ID - text.
For ex:
<RelativeLayout android:height="match_parent"
android:width="match_parent">
<TextView
android:id="#+id/text"
android:text="Hello"
android:height="wrap_content"
android:width="wrap_content"/>
</RelativeLayout>
In this layout file TextView has the id - text
So,in order to access the textview programatically, we make use of findViewById() t to get reference to view.
TextView textView = findViewById(R.id.text);
now we can make use of this view reference to make changes to the view.
For example we can change text like,
textView.setText("This is a test");
As far as
TextView textView2 = new TextView(this);
is concerned, you're creating a TextView dynamically. This can be added to the parent container as and when required.
Activity.setContentView() has 2 signatures. One is taking a layout id as parameter, the other is taking a View as parameter. There is actually a third one taking a View and ViewGroup.LayoutParam as input.
All three methods take what they get (a View or a layout to inflate) and set it as their root element. So in short: There is no real difference here. Just a few options the developer can choose from to tell the Activity about its root UI element
Also see: setContentView description
The line TextView txtView = (TextView)findViewById(R.id.text); is then searching for a TextView with the id "text" within the Activitys Content (in that case every view in R.layout.activity_main).
The line TextView textView = new TextView(this); is creating a new TextView programmatically instead of inflating a layout xml. The this parameter is a Context instance. A Context instance is always needed to create a View. An Activity is a Context.
When you're using the following:
setContentView(R.layout.activity_main);
TextView txtView = (TextView)findViewById(R.id.text);
you're using the activity_main layout as the content of the activity. whenever you're trying to bind the view with findViewByid(), it only search for the views inside the layout and will giving you an error if you're trying to bind views outside the layout. See setContentView (int layoutResID) for details.
When you're using the following:
TextView textView = new TextView(this);
setContentView(textView);
You're creating a TextView with the activity (this) as the context with new TextView(this);. Please be noted that you always need a context whenever you're creating a View.
Then with setContentView(textView); you're setting the textView as the sole content of the activity. See setContentView (View view) for details.

Non-rectangular TextView

I tried searching for a solution, but I couldn't find anything of the sort.
My quest is: is it possible to have a non-rectangular TextView? I need to put a TextView and an ImageView inside a fragment.
Would look something like this:
I was thinking of maybe having two TextViews (lets call them tv1 and tv2) and an ImageView (Img1).
I could align the tv1 to the bottom of img1 and align the top of tv2 with img1. SOMEHOW play with the text in the TextViews to split it in both TextViews?
Flow textview will help to acheive what you want. Here are the few links
http://grishma102.blogspot.in/2013/12/how-to-make-text-flow-around-images-in.html
https://code.google.com/p/android-flowtextview/

Dynamiclly add a textview using existing textview layout parameters

I am trying to add a TextView from the activity when a button has been pressed. I have found how to add a new textview from the activity, however instead of coding the required layout parameters is it possible to copy an existing textviews parameters (in the xml layout) to the new textview?
I have tried:
TextView tv1 = new TextView(this);
TextView tv2 = (TextView) findViewById(R.id.basetext);;
// its this line below which doesn't work
tv1.setLayoutParams(tv2.getLayoutParams());
But it doesn't copy any of the layout parameters...
Any ideas?
You can do One thing.
If you are trying to set just the Value of the Same TextValue every time and if the Layout properties of that TextView is same at all the time then follow below steps:
First Create the One Layout that only contain he TextView layout only with your appropriate properties. (Let name it as layout_textView.xml)
Now, do add that layout_textView.xml dynamically to your main view as per your requirement.
How it will Solve your issue.
If any query then let me know.
Did you call requestLayout() after adding the textview?

How can I set the text of the TextView within a LinearLayout?

I have created a custom LinearLayout through program which have a custom TextView within it. I have not added the TextView using inflater. Now I want to use this LinearLayout multiple times in XMl layout. But the problem is that how can I set the text of these TextView within the LinearLayout?
Any ideas??
If I right understand it will help you. Good luck!
LinearLayout layout = findViewById(id_linear_layout);
((TextView) layout.findViewById(id_textview)).setText("Text");
When adding the customTextView set the tag using setTag method. And then to retrieve it use linearLayout.findViewWithTag method to retrieve the customTextView and as usual use setText to set the text
You an also alternatively set the id using setId and retrieve it using findViewById method
Use a reference of that TextView to to set the text.
Then add this TextView to LinearLayout.
ex -
TextView text = new TextView(yourActivity.this);
text.setText("Sample Text");
ll.addView(text);
// ll is your LinearLayout.

How to create TextView programmatically?

I have created some TextView in the view ml, about 5 of them. But the problem is that after I read from xml file the values come, more than 5 sometimes, and there is no limit. It could be better more than 10. However if I can create the TextView from the activity, then I think I can do it somehow.
Assuming you want it add to a view called containingView,
TextView myTextView = new TextView(containingView.getContext());
myTextView.setText("alshareef");
containingView.addView(myTextView);

Categories

Resources