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);
Related
When creating an ImageView for example, I know I can create it within a Layout:
Example a LinearLayout:
<LinearLayout...>
<ImageView
android:id="#id/hello_world_id"/>
</LinearLayout>
But can I define an View outside a layout, and then add it to any other layout?
I want to have a RelativeLayout which adds/removes views programmatically and dynamically, so that the RelativeLayout starts off with no views inside it, then I add some, remove some etc. Would there be any way to do this? Or is it better just to have these views already inside some other Layout, and then I add the Layout (whatever it is--containing my view(s)) to my RelativeLayout?
What you are searching for is LayoutInflator
Create a xml file - buttons.xml
<LinearLayout...>
<ImageView
android:id="#id/hello_world_id"/>
In your activity access it by
final LayoutInflater inflater
= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.buttons,
null);// this is a layout in your master activity
lLayout = (LinearLayout)findViewById(R.id.layout1);
lLayout.addView(itemView);
Hope this helps.
How to create a whole layout (Relative/Linear) multiple times in Android? I want the same layout to be created multiple times inside a horizontal scroll view.
You can use RecyclerView for Horizontal scrolling-
or-
Take horizontal scrollview reference in java code by findViewById.
Create one other xml for view which you want to display multiple
time.
inflate that view by getlayoutinflator. Create a loop in
view.
create a linearlayout at runtime and add those view to it by add
view
and add linearlayout to horizontal scroll view. by addview()
take a idea and modify the below code
scrollview = findViewByID(scrollview);
LinearLayout ll = new LinearLayout(this);
for(your loop){
View v= getLayoutInflator().inflate(R.layout.xml);
ll.addView(v);
}
scrollview.addView(ll);
Either you need to add inflated child views to the root view like below
RelativeLayout rootView = (RelativeLayout)findViewById(R.id.rootView);
View child = getLayoutInflater().inflate(R.layout.child, null);
rootView.addView(child);
OR you can define and include that layout multiple times inside other.
Check this link http://developer.android.com/training/improving-layouts/reusing-layouts.html
Include your reusable layout like this
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/reusabelLayout" />
I'm trying to create a scrollable layout with an embedded list, similar to the Spotify application for Android one shown here (One image at the top followed by a list and the whole layout scrolls). I'm aware that the Android developer guidelines state that you shouldn't put a ListView inside of a ScrollView layout so I'm wondering how this is accomplished. Do I need to make each list item manually using layouts and TextViews?
Yes as the List view has default scroll able functionality . If you place a listview inside the scroll view . The list scroll will hamper.
To Avoid such thing , list view has the concepts of header and footer views.
You can add n number of header and footers to the list.
Below is a sample code snippet how to add header/footer by inflating any xml layout to it
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout)inflater.inflate(
R.layout.footer_layout, null);
list.addFooterView(listFooterView);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.header_layout, null);
list.addHeaderView(listHeaderView);
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.
folks!
I need to make such layout: I've got listview and I need to put buttons on top and on the bottom of it, i.e. when user scrolls list to the end, he can see bottom button, and when user is on the top of list, he can see top button. But when user is 'in the middle' of listview, he can't see those buttons. I've got no idea how to do it. Thanks for help.
UPDATE
listView=(ListView)findViewById(R.id.listSearchResults);
LayoutInflater inflater=this.getLayoutInflater();
View header=inflater.inflate(R.layout.list_header, null);
btnBack=(Button)header.findViewById(R.id.btnBack);
btnBack.setOnClickListener(this);
btnBack.setEnabled(false);
listView.addHeaderView(header);
View footer=inflater.inflate(R.layout.list_footer, null);
btnForward=(Button)footer.findViewById(R.id.btnForward);
btnForward.setOnClickListener(this);
btnForward.setEnabled(false);
listView.addFooterView(footer);
First create two layout file. like as footer_layout.xml & header_layout.xml and add footerview-headerview in list view
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout)inflater.inflate(
R.layout.footer_layout, null);
list.addFooterView(listFooterView);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.header_layout, null);
list.addHeaderView(listHeaderView);
You can use addHeaderView() and addFooterView() on your ListView
Check here:
Android Dynamically load Listview at scroll end?
This is with the help of list's footer and header. Basically, you use addHeaderView()
/addFooterView (). Check all versions of the methods, since they allow you to have the view not being selectable.