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?
Related
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.
I need to replace textview which is already define in xml layout file with dynamic textview created programmatically in my Activity class file.
You don't need replace it, just get the particular properties from textView (from Java) and set them on text_view (from XML).
For example:
text_view.setText(textView.getText().toString())
text_view.setColor(textView.getColor())
...
text_view.setAnything(textView.getAnything())
Maybe it helpfull to you
I didn't get why you would do this, but anyhow will layout the steps:
get reference to the TextView containing ViewGroup
call removeView with TextView Reference.
call addView with new TextView.
TextView old = (TextView) findViewById(R.id.old_text_view)
LinearLayout layout = (LinearLayout) findViewById(R.id.container)
TextView new = new TextView()
layout.removeView(old);
layout.addView(new, position);
I want to insert custom layout in android Preference class. My custom layout contains one ImageView and one TextView. There are 7 preferences and all preferences have same custom layout but different images and texts. Now one way to implement such Preference Screen is, make 7 .xml files one for each preference. Is there any other way so that I need to make only one .xml file and I can set image source and text string programatically ?
**EDIT : I need to show all 7 preferences simultaneously. **
You can use one layout with all your views for all preferences, but you need to control them at run time by using:
yourView.setVisibility(View.VISIBLE);
otherView.setVisibility(View.GONE);
thirdView.setVisibility(View.GONE);
fourthView.setVisibility(View.GONE);
. . .
You can use same layout and changing then at runtime.
For example,
TextView tv1 = findViewById(R.id.textview);
tv1.setText("tv1");
layout.addView(tv1);
TextView tv2 = findViewById(R.id.textview);
tv1.setText("tv2");
layout.addView(tv2);
.
.
TextView tv5 = findViewById(R.id.textview);
tv1.setText("tv5");
layout.addView(tv5);
Or you can just change their data at runtime by using only single reference to use same layout.
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.
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.