I need to dynamically add a 'template' layout to my activity at runtime based on the click of a button.
Shown in the code block below is a simplified version of my layout. The contents of HEADER C (i.e. BODY C1, BODY C2 ... BODY Cn) are the components that need to be added at runtime. They are all of the same format and are defined in a separate xml file with a relative layout. How do I accomplish this task? I have tried a LayoutInflater, but was unsuccessful. Does LayoutInflater require a LinearLayout to inflate?
<SCROLLVIEW>
<RELATIVE_LAYOUT>
____________
HEADER A
____________
BODY A
</RELATIVE_LAYOUT>
<RELATIVE_LAYOUT>
____________
HEADER B
____________
BODY B
</RELATIVE_LAYOUT>
<RELATIVE_LAYOUT>
____________
HEADER C
____________
BODY C1
____________
BODY C2
.
.
.
____________
BODY Cn
<RELATIVE_LAYOUT>
</SCROLLVIEW>
Thanks, any guidance is appreciated!
LinearLayout Parent = (LinearLayout) findViewById(R.id.linearLayout);
View child = getLayoutInflater().inflate(R.layout.main_new,null);
Parent.addView(child);
use
View headerView = View.inflate(this, R.layout.class_name, null);
to inflate your layout.
/**rootView represent the RelativeLayout*/
View headerView = LayoutInflater.from(context).inflater(R.layout.header_view, rootView, false);
rootView.add(headerView);
You can do that like so:
general = (LinearLayout) findViewById(R.id.general_tab); // get the id of the layout you wish to add your view to
TextView programs = new TextView(this); // create a new view/widget to add
programs.setText("Program(s): " + prgdisp); // set the text
general.addView(programs); // add the new view/widget to the existing layout
EDIT
I missed that you had an xml layout for it already. Change the above to:
general = (LinearLayout) findViewById(R.id.general_tab); // get the id of the layout you wish to add your view to
View header = View.inflate(this, R.layout.class_name, null); // inflate your layout
general.addView(header); // add the new view/widget to the existing layout
Related
I followed the below link to dynamically add a layout multiple times using inflater and AddView()
Is there a way to programmatically create copies of a layout in android?
I used a loop to create multiple entries. But only one entry is comming up which is the result of last loop index
Below is my C# code
I can see only one child inside the parent which is the result of last loop.
What I missed?
var parent = FindViewById<RelativeLayout>(Resource.Id.ParentLayoutWrapper);
for (int i = 0; i < 4; i++)
{
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, parent, false);
var txtView = view.FindViewById<TextView>(Resource.Id.textViewSample);
txtView.Text = i.ToString()+ " Android application is debugging";
txtView.Id = i;
parent.AddView(view, i);
}
The original post you worked from had a LinearLayout as the parent layout, not a RelativeLayout like you have. When you add a view (or another layout) to a LinearLayout, it gets positioned below (when LinearLayout has vertical orientation) any existing elements in the layout. However, the elements in a RelativeLayout need to use positioning properties to determine where they will be in the RelativeLayout, so every time you add the new layout, RepeatingLayout, since you are not changing the layout options, the view/layout is added over the existing view/layout. So change the parent layout to a LinearLayout in your layout file and then this should work:
LinearLayout parent = FindViewById<LinearLayout>(Resource.Id.parentLayout);
for (int i = 0; i < 4; i++)
{
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, null);
var tv = view.FindViewById<TextView>(Resource.Id.textViewSample);
tv.Text = i.ToString() + " Android application is debugging";
parent.AddView(view);
}
Trying to do the same with a RelativeLayout as the parent layout highly complicates things unnecessarily.
I have xml file with linear layout with id
android:id="#+id/layout_id.
in my other java file which does not have this layout as its layout,
I have called
View view = MessageIndex.this.getLayoutInflater().inflate(R.layout.layout_id, parent, false);
to set values of widgets in layout_id linear layout.
TextView tv1 = (TextView)view.findViewById(R.id.abc);
Now I would like to give two background images to this layout for two conditions.
like
if(a)
{
LinearLayout right=(LinearLayout)findViewById(R.id.layout_id);
right.setBackgroundDrawable(getResources().getDrawable(R.drawable.image1) );
}
else
{
LinearLayout right=(LinearLayout)findViewById(R.id.layout_id);
right.setBackgroundDrawable(getResources().getDrawable(R.drawable.image2)
}
But then, the app crashes when I write the above two if-else statements.
I am trying to programmatically add LinearLayouts inside an existing RelativeLayout. Each LinearLayout will contain some buttons and I want to be able to toggle the visibility of each set of buttons by setting the visibility of the container LinearLayout.
// We iterate over a list and call the following to create the new
// layout assigning an index from a int counter
LinearLayout LL = new LinearLayout(MainActivity.this);
LL.setOrientation(LinearLayout.VERTICAL);
LL.setId(nextId);
LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
LL.setWeightSum(6f);
LL.setLayoutParams(LLParams);
LinearLayout mll=((LinearLayout) findViewById(R.id.menuLayout));
mll.addView(LL);
My problem comes when I try to retrieve these layouts later, for instance to be able to toggle their visibility on/off. I thought I would be able to use
LinearLayout ll = (LinearLayout) findViewById(layoutIndex);
But findViewById() gives me an error when I try to supply an int, it wants a resource ID. Is there an easy way I can convert the ints that I have assigned as the Ids for these layouts to R.id.XXX ids?
Thanks,
Andy
findViewById(id) looks up elements that were included as part of the XML defining a layout.
You will probably have better luck with getChildAt(index), which returns the View at the passed index.
Yes! Find all LinearLayout in a container without using ID.
LinearLayout mll= (LinearLayout) findViewById(R.id.menuLayout);//container
for(int i=0; i<mll.getChildCount(); ++i) {
View nextChild = mll.getChildAt(i);
if (nextChild instanceof LinearLayout ) {
//TODO add your code here nextChild is a LL that you wanna find
}
}
I want to add some spacing between the actionbar's back navigation button, icon and title. Anyone knows how to do it?
int actionbarTitleId = getResources().getIdentifier("android:id/action_bar_title", null, null); //TextView hosted in LinearLayout
int upImageViewId = getResources().getIdentifier("android:id/up", null, null);//ImageView hosted in LinearLayout or HomeView
int homeId = android.R.id.home; //ImageView hosted in HomeView
Use findViewById with each of the ids to get View (or appropriate cast) for each.
Try not to modify the LayoutParams. But add padding between them or just use existing LayoutParams to modify the margins.
I don't know how to write code to add a layout into a layout in android, because when I read the reference, I saw it only have "AddView" method, when I try to use "AddView" method to add a layout, it don't work. Can you suggest me some solutions, I don't use XML file to add layout?
Here's a snippet that creates a new layout (newL), adds an image to it, and then adds newL to a layout (layout) that was defined in the XML
ViewGroup.LayoutParams lp_fullWidth =
new ViewGroup.LayoutParams(lWidth, lHeight);
ViewGroup.LayoutParams lp_wrap =
new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, lHeight);
LinearLayout layout = (LinearLayout) findViewById(R.id.viewNowPlaying);
LinearLayout newL = new LinearLayout(context); // create layout
ImageView arrowRT = new ImageView(context); // create image
arrowRT.setImageResource(R.drawable.arrowrt); // set source file
newL.addView(arrowRT, lp_wrap); // add image to newL
layout.addView(newL,lp_fullWidth); // add newL to layout