Assign fixed layout to dynamic textview - android

Hello
I have a loop that creates n-1 textviews, and for each of these textviews I would like a fixed layout defined in an XML file.
The code is as follows:
for(PInfo P : P_array)
{
TextView tv = new TextView(this);
tv.setText(P.getName());
tv.setWidth(P.getLength());
tv.setHeight(70);
tv.setPadding(10, 5, 10, 5);
masterView.addView(tv);
}
I would like something like tv.setLayout(R.id.textviewlayout); ... How is that done, im sure its easy (it should be) but I cant find any info on it.

This is called inflating. Try this:
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView tv = li.inflate(R.layout.textview, null);
Second parameter is optional parent for inflated view:
help article

Not sure exactly what you are trying to achieve but have a look in the SDK reference for ListView and ArrayAdapter.
This will enable you to build a list of custom views from an XML file.

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.

How to display dynamic view in android

I have a title as processed with three textview and an images
and then another title with same three textview.
How to create this in dynamic layout? Since I need this to generate in dynamic in android.
I am new to this. please give me an idea.
Thanks
LinearLayout mContainer;
public void onCreate(Bundle) {
setContentView(R.layout.container);
mContainer = (LinearLayout) findViewByid(R.id.coainter);
View view = layoutInflater.inflate(R.id.inflaterView);
view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mContainer.addView(view,1);
Info -
create the XML you need to add the Views inside.
create the view you need to Dyanmiclly add (inflate or create it)
set the LayoutParams according to the Conatiner.
add the View to the Conatiner the number 1 is the location inside the Container.
of crouse this is an Example i just followed your image.
you can use mConatiner.addView(view) this will insert them according to the Container Choice
Hope it Helps.

Styling Android Views from existing XML using an Inflator

How should I write my XML file, where should I put it and how should I reference them in the activity?
This is what I got:
myView = getLayoutInflater().inflate(R.???someplace???.???somename???, null);
What should be set instead of someplace and somename ? And if I have created this XML with 2 elements (a TextView and a LinearLayout, for example) how can I make myView look like the first element and mySecondView look like the other element in that XML?
After solving it, will mainView.addView(myView) make myView appear in the Activity with the pre-defined style?
I've been reading that it is the best solution for defining style in a separate XML file and then applying it to a View created programmatically.
See this sample code for inflating view..
{
View headerView = inflater.inflate(R.layout.icms_article_detail_header, null, false);
articleDetailDataProvider = new IcmsArticleDetailDataProvider(mContext);
txtPageIndicator=(IjoomerTextView)headerView.findViewById(R.id.icmsTxtIndicator);
imgFavorite = (ImageView) headerView.findViewById(R.id.icmsImageFavorite);
imgShare = (ImageView) headerView.findViewById(R.id.icmsImageShare);
list.addHeaderView(headerView);
}

How do I programatically add a line below a textview layer

I am looking to add a line below a textView programmatically in java and not in xml.
I have the textView as follows:
textView.setText(DisplayName);
How do I go about the same? I have a textview supporting text and checkbox and I would like ot add a line below the same. Any clue?
Thanks!
Justin
You are probably looking for a separator. You could achieve this by 'faking' a line-seperator. add a normal View to your parent view with a height of 1dp.
Android - Dynamically Add Views into View
If you load a view from xml and want to add the line programmatically:
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup layout = vi.inflate(R.layout.MyParentLayout, null);
View separator = new View(Context context);
set the view its layoutparams set its height: http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)
layout.addView(separator);
If you create your layout programmatically:
new LinearLayout layout = new LinearLayout(context);` //or whatever kind of layout you want
I am writing this post on my mobile phone so it could contain some errors.
Add line view to the parent layout after textview.
View ruler = new View(myContext); ruler.setBackgroundColor(0xFF00FF00);
parentLayout.addView(ruler, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 2));
If you're trying to underline the text, try this (from https://stackoverflow.com/a/10947374/413254):
textview.setPaintFlags(textview.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
If you're trying to create a divider of sorts, go with what RajaReddy suggested.

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?

Categories

Resources