Hiiii there, I have an xml layout and when a button is clicked I need to add a TableLayout view to that xml layout. I know how to do this with simple stuff like text views but tables would seem to be a lot harder to define programmatically, Im not quite sure what inflating is but is that what I should be using? Thanks.
You can define the layout in an xml file and then simply inflate that layout using the LayoutInflater. This will give you a View (in your case this will be a TableLayout), then add this view and you are done.
Reference: LayoutInflater
In your xml layout, let's say you want to add the TableLayout inside a RelativeLayout with an id of relativeLayout1.
To add it, you would do the following code:
TableLayout table = new TableLAyout();
// Code to add rows and views to the table
R.findViewById("relativeLayout1").add(table);
Related
I have a Linear layout and inside which there are few dropdown and text views,now i want to inflate the layout each time on click of "add more" button, below is the code for inflating the layout. the problem i am facing is to assign the id to each inflated layout content .Inorder to handle the content the inlfated layout i need the unique id for each content for each time i am inflating the layout.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row_view;
row_view = (View)inflater.inflate(R.layout.inflatefloor,null);
inflatefloordetails.addView(row_view);
here inflatefloor is the layout to inflate and inflatefloordetails is the main root layout inside which layout is to be inflated and i am inflating the inflatefloor layout each time on button click.
New to android pardon if anyone finds the question silly.
Note:Check out link below if anyone is facing similar problem, I found this link useful.
How to set Id of dynamic created layout?
I think it will be helpful.
you can put any object when set the tag to be unique:
yourView.setTag(Object object);
and to retrive it:
object.indexOf(yourView.getTag())
Otherwise you have to use custom view.
I want solution of following:
I have create xml file in data/data/com.example.file/files/abc.xml so during running of app how I get this abc.xml in res/layout and should display on screen.
Thanks.
In general, the XML vocabulary for declaring UI elements closely
follows the structure and naming of the classes and methods, where
element names correspond to class names and attribute names correspond
to methods.
Courtesy goes to #Mike M : You cant create this .XML files used for layouts are pre-compiled .
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
the RelativeLayout item will be the container for your child view.
Your question needs to be a bit more specific, but here's a general answer:
If you are using fragments then you should re-create your fragment and then use:
inflater.inflate(R.layout.abc, container, false);
If you are using activities then you should use:
setOnContentView(abc);
I have a custom view called CustomImageView.
I would like to create an xml file with this view so I inflate the view and add it programmatically to my current layout. (depending on orientation , I will decide where to add it)
Is it possible to so something like this in the xml without a containing layout
<?xml version="1.0" encoding="utf-8"?>
<CustomImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="wrap_content"
android:height="wrap_content"
android:level="2"
android:anotherattr="adsad" />
After that I will inflate it. or should I put it in a Layout like LinearLayout? I just want an xml with my view so I can inflate it and add to my existing layout.. I am not sure what the containing layout would be!
This is perfectly legitimate. You can do it with built-in or custom views. Here is an example from the Android framework. It's an XML layout file containing a single TextView, representing a default ListView row layout. The TextView has several attributes assigned to it in XML, just as your custom view will.
Is it possible to so something like this in the xml
Yes you can inflate an XML that just contains one view. But why not just create the view programmatically? new CustomImageView(...)
depending on orientation , I will decide where to add it
Consider using the layout directories such as "layout-land" to automatically inflate different layouts based on orientation - that's what those are for. Read more about this Supporting Different Screens
I am not sure what the containing layout would be
Use the layout directories and it removes all doubt
Say I have a layout file that I would like to use as part of another layout, how would I do that?
For example, I have a table layout at /res/layout/table.xml. I want to use that table as a component inside a relative layout at /res/layout/relative_stuff.xml. Say my relative layout is to contain the table and two buttons.
The simple case is to do the combination completely inside the relative_stuff.xml file. But a better case would be the ability to set the table xml programmatically: the reality is I want to choose from many different tables, for now say two tables at: /res/layout/table_1.xml and /res/layout/table_2.xml.
So basically my main layout file is /res/layout/relative_stuff.xml. And I want to set either of two tables inside it programmatically.
You an re-use layouts using an include tag.
For example, using your example layout/table.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<include layout="#layout/table"/>
</LinearLayout>
If you don't want to do it in XML, you can use a LayoutInflater to inflate your XML and add it to whatever container you are using.
LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View tableLayout = mLayoutInflater.inflate(R.layout.table, (ViewGroup) findViewById(R.layout.root_id));
rootLayout.addView(tableLayout);
You can use Layout Inflator Service to add multiple add the activity.
I'm extending a linear layout wich contains 2 TextView.
The TextViews are inflated from template layouts I created.
My problem is that the backgrund that I set is covering the entire raw, instead only the part that contains text. But, when I'm setting the same layout not in the dynamic way then it is all working and clipping well.
final LayoutInflater inflater = LayoutInflater.from(this.getContext());
//The title.
title = (TextView)inflater.inflate(R.layout.text_view_day_title, null);
title.setText("bla bla");
//The info.
info = (TextView)inflater.inflate(R.layout.text_view_day_info, null);
info.setText("bla bla 2");
this.addView(title);
this.addView(info);
Thanks for helping.
Make a linear layout inside your xml and addview on it.
Make sure that inflated xml has no background background and it has margins between them to show the background of the main xml layout.