set LayOut Dynamically - android

i want to add Layout Dynamically on Add button click and on Dynamic Layout show Datepicker,Timepicker Dialog and set value in given Edit Text. show in image on Date Click set Date Right side . Here problem Start when Add Second Same layout and set date it set only on newly created layout

For example you need to create xml layout file with ScrollView and LinearView inside.
Then in your Activity class:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = inflater.inflate(R.layout.your_layout, null);
setContentView(main);
LinearLayout linear = (LinearLayout)main.findViewById(R.id.linear_layout);
and then in onClick method just:
View yourView = inflater.inflate(R.layout.yourView, null);
// Do whatever you want with your View, set up some variables etc.
and to add your view to main view:
linear.addView(yourView);
I know that this is not a direct answer to your question, but maybe will help you with dynamically adding Views.

Related

ImageButton on the recyclerview

[When click on the button which is marked as circular line of image 1 then next functionality will be happen which is second image 2
is what you want to happen is that when you click the image button that row layer will hide?
You can add a layout at runtime using below snippet onClick of button.
LayoutInflater layoutInflater = getLayoutInflater();
View row = layoutInflater.inflate(R.layout.row, null);
ViewGroup main = (ViewGroup) findViewById(R.id.child_layout);
main.addView(row, 0);
R.layout.row is the row which you want to add on click of arrow.
R.id.child_layout will be an empty LinearLayout placeholder in your parent xml in which above row will be added.
You might want to look at various implementation of addView(), as it is overloaded with various parameters.
You can use removeView to remove the view as well.
There is one more option in which you can the add the row as part of layout set its visibility as GONE
e.g.
mainLayout.setVisibility(LinearLayout.GONE);
mainLayout.setVisibility(LinearLayout.VISIBLE);
And then you register or deregister click listeners.

Append new item to LinearLayout programmatically?

I'm trying to create something like editable UITableView on iPhone.
I added a button to add new items at the end of the list which would be the equivalent of the "+" button on iphone, this takes user to a list of items to add. My problem is that when they select one I don't know how to add it to the list.
<LinearLayout
android:key="application_preferences"
android:title="Applications">
</LinearLayout>
How can I append a view inside the LinearLayout programatically?
You can use addView to add a child view to a ViewGroup, but also consider just using ListView instead of a LinearLayout.
Your question is not very clear but if you try to add a view to the linear layout programmatically just use Layout Inflater
LayoutInflater localLayoutInflater =(LayoutInflater)getBaseContext().getSystemService("layout_inflater");
View myView = localLayoutInflater.inflate(R.layout.custom_layout, null);
final LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayoutID);
ll.addView(myView);

Dynamically add layout on button click and get text from the layout?

So I have a layout that gets added to the main layout every time the user presses a button. I got that working fine, but that layout happens to consist of several EditTexts. What would be the best way to get the text from the EditText? I only have the id of the layout itself, not the EditTexts inside the layout.
I thought of just adding EditTexts dynamically one by one, but is there a more efficient way of doing it? I'd much rather just inflate an xml layout every the button is clicked.
I assume you're adding new views by inflating them and then adding them to the main view similar to below.
LinearLayout newView = (LinearLayout)this.getLayoutInflater().inflate(R.layout.my_layout, null);
LinearLayout mainView = (LinearLayout)this.findViewById(R.id.mainLayout);
mainView.addView(newView);
You can use findViewById() on the newView to access each EditText as required.
EditText editText = (EditText) newView.findViewById(R.id.myButton);
String text = editText.getText().toString();
Since it's a fixed layout you inflate from a particular XML file, you can also use getChildAt(int index) to find a particular view of the ViewGroup.

Android extending linear layout and inflate

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.

Android: Add view onclick after the view itself

I have a LinearLayout View with a OnClickhandler and I want to add a View after the LinearLayout programatically when the OnClick event is fired.
public void onClick(View view) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout info = (LinearLayout) li.inflate(R.layout.infolayer, null);
// view.addViewAfter(info)
}
info is the View i want to add. view is the View on which the click goes and after which I want to add info.
How can I do that?
IF u want add a view after the current linear layout then first get the id of the parent layout in which the linear layout is .
for example let u have the the Linear Layout with id "ll" in relative layout(having id parentlayout) and on a button click u want add the text view under the liner layout
public void onClick(View view) {
RelativeLayout rl=new RelativeLayout(this);
TextView tv=new TextView(this)
//set param value ur requirement
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW,R.id.ll);
}
Assuming you have a known amount of linearlayouts could you just place them inside the XML resource and mark them as 'GONE'. When the event occurs make them visible. When they are marked as gone they shouldnt be taking any screen space up.
You can insert through coding and you don't need to inflate. You can create a view of any type calling the constructor and passing the context. A reference to the context can be stored on the view as a field, when the view is being constructed. This way you can always create your view on the fly.
To add the view to the LinearLayout, you just need to call addView. And afterwards, if you would want to remove it, just call removeView.
But the onClick event is inside the LinearLayout object? This might be a problem because the views inside the the LinearLayout might consume the event before it reaches your method. See this post to learn about that.

Categories

Resources