Add a layout inside the list view - android

I wanted to display a web view in between in the list view and bottom of the list view.
I was able to add footer to the list view to display the web view in the bottom, but I don't know how to display in the middle
Code where I have added the footer:
ListView list = (ListView)view.findViewById(android.R.id.list);
WebView web;
LayoutInflater inflaterOf = getActivity().getLayoutInflater();
LinearLayout listFooterView =
(LinearLayout)inflaterOf.inflate(R.layout.list_footer, null);
web = (WebView)listFooterView.findViewById(R.id.webview_footer);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("file:///android_asset/news_footer.html");
list.addFooterView(listFooterView);
Please let me know how to insert the layout with web view in-between the list view

You should create a custom object and in your ArrayAdapter of listview, change the layout to inflate when getCount is equal to list.size()/2.
That's the way I'll handle this.

Related

Dynamically add a button to a view from a listview Adapter GetView method

As a little eperiment, I'm trying to do the following.
I have an AXML describing a vertical linear layout which contains a listview (only filling 200dp of the vertical linear layout ). The AXML is inflated when the activity starts with SetContentView. Then the listview is correctly populated with values using its Adapter.
In the GetView method of the listview Adapter, I am trying to also dynamically create a button and add it to the linear layout, but for some reason the button is not added.
If I try to add the button in the constructor method of the Adapter instead, it is correctly added.
Can you tell me what could be possibly going wrong?
Let me add some code:
class TracksAdapter : BaseAdapter<string> {
Activity context;
List<Dictionary<string,string>> trackList;
// constructor
public TracksAdapter (Activity context, List<Dictionary<string,string>> trackList) {
this.context = context;
this.trackList = trackList;
// Just as a little test, if I create the button from here it will be correctly added to linear layout:
var ll = context.FindViewById<LinearLayout>(Resource.Id.linLayForResultsActivity);
Button b1 = new Button(context);
b1.Text = "Btn";
ll.AddView(b1);
}
public override View GetView(int position, View oldView, ViewGroup parent) {
// if I create the button from here it will not be added to the layout
var ll = context.FindViewById<LinearLayout>(Resource.Id.linLayForResultsActivity);
Button b1 = new Button(context);
b1.Text = "Btn";
ll.AddView(b1);
// this other code is working
View view = context.LayoutInflater.Inflate(Resource.Layout.ResultItem, null);
var artistLabel = view.FindViewById<TextView>(Resource.Id.resultArtistNameTextView);
artistLabel.Text = trackList[position]["trackArtistName"];
return view;
}
}
Update: adding some more context information because I know this can be a bit weird to understand without it:
In GetView, I don't need to return the new button I am trying to create there. GetView only need to return a listview view item, but, along its execution, GetView also has to create and add a button to the linear layout containing the listview.
The real code is much more complex than that. I have simplified it in the question. In the real code, the listview items are made of text and a button. The GetView also attaches event handlers to the buttons. Then what I need is, when a user clicks a button in any of the listview items, another button is added below the listview. So I need the code for adding another button to be in GetView, and the button needs to be added outside of the listview, ie. to the linear layout containing the listview.
Use the LayoutInflator to create a view based on your layout template, and then inject it into the view where you need it.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
I looked in you code, you are returning view, while you add the button to ll, you should return ll
what you return in getView() is what you see in the list item layout, since you're adding the button to ll and returning view, the button won't appear.
you can add the button to view as you implementation
Also check this:
Try using boolean addViewInLayout (View child, int index, ViewGroup.LayoutParams params)
http://developer.android.com/reference/android/view/ViewGroup.html#addViewInLayout(android.view.View, int, android.view.ViewGroup.LayoutParams)
It's working... Without making any changes now it's working as it should... ! Ugh!
I really don't know what I was doing wrong here... probably it was because of some sort of caching of older version of the installed APK.. ? I know this sort of stuff can happen, and that's why I've always been uninstalling the app before deplyoing the new version to the device... but still...!

How to move ViewGroup when scrolling ListView?

I have a ViewGroup in top of Activity, 3 buttons and 3 ListView ( they are placed horizontally ). When I scroll ListView from down to up, my buttons and ViewGroup should be scrolled accordingly, from down to up.
My question - how can I implement it? ( scrolling ViewGroup and buttons )
Updated: buttons scrolls ListViews left to right and right to left ( like tab bar ), but ViewGroup and button doesn't moves;
ListView has very convenient method addHeaderView. So put your ViewGroup and buttons into separate xml layout. Inflate view from this xml and add it to your ListView using addHeaderView.
From the top of my head:
1)Make a list adapter for the list view. This class must extend SimpleAdapter, BaseAdapter, etc.
2)Make a XML layout for the list`s item, that includes the buttons and other elements you may desire. (your_item_layout.xml). This layout implements a listview if you want.
3)The list adapter needs an data structure to storage information for the list view, like an ArrayList, Map, Array, etc.
For example: private ArrayList al;
4)This list adapter function
public View getView(int position, View v, ViewGroup vg){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.your_item_layout,null);
/*set as you wish the elements of the item layout*/
Button b1 = v.findViewById(R.id.button1_item_layout);
b1.setText("OPEN FILE " + al.get(position));
b1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/*do something*/
}
});
}
I hope that helps!

Add a header/footer in a listview in xml

I know how to add a header or a footer in JAVA, but I was wondering if I could add it straight in the XML.
I wouldn't want to simulate this, but really add it as footer or header!
No, I don't think that it is possible. Based on ListView source code there are only overScrollHeader/overScrollFooter are available from XML attributes. But these attributes accept only drawables.
If you don't want to use tricks with layouts above/below ListView. You can extend ListView and implement your own footer and header support in customized View. It is not so hard because of footer and header are already implemented. You only have to add XML attributes parsing in your customized View's constructor.
I was just trying to achieve the same thing (to keep my code cleaner and use XML for markup & source code for logic), but the only solution I found is to define the header view with XML somewhere in your layout and then detach it and put into ListView as header.
For example, having this XML:
<ListView android:id="#+id/myListView">
</ListView>
<LinearLayout android:id="#+id/myHeader">
....
</LinearLayout>
You can do this in your code:
ListView myListView = (ListView) findViewById(R.id.myListView);
LinearLayout myHeader = (LinearLayout) findViewById(R.id.myHeader);
// Let's remove the myHeader view from it's current child...
((ViewGroup) myHeader.getParent()).removeView(myHeader);
// ... and put it inside ListView.
myListView.addFooterView(myHeader);
Basically, what we do here is just detach the inflated LinearLayout from its parent and set it as ListView header child.
This is not an ideal solution, but it is still easier than creating/inflating header manually. Also this utilizes the power of XML inflation & view reusing if you're using this inside some "holder" pattern.
Hope this helps somebody.
This is how it worked for me, in my Adapter class which extends the BaseAdapter. I am targeting API 23:
#Override
public View getView(int position, View view, ViewGroup parent) {
if (position == 0) {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
view = layoutInflater.inflate(R.layout.test_results_header, parent, false);
}
} else {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
view = layoutInflater.inflate(R.layout.test_result_item, parent, false);
}
}
Pretty simple, I inflate a header XML for position 0 and the content XML for the rest. If you know the position where you want a header or any other XML, in your logic you would need to check the position, and inflate the respective XML for that position.
I created an xml resource same as my adapter rows xml (so the title is fit) and added it to the listview after addind the adapter:
listView.setAdapter(myRowsAdapter);
listView.addHeaderView(View.inflate(getContext(), R.layout.title_row, null));

How can I inflate a View subclass from XML?

I am currently populating an Adapter on startup with views inflated from XML using
private void addView(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.deal_tile, this, null);
mViews.add(view);
}
However, I've found that storing the views in a list inside the AdapterView creates problems with controls within those views, so I want to change over to use the recycling functions in Adapter#getView(int position, View recycle, ViewGroup container).
For this reason I want to use a custom view class so I can do a sanity check (if(recycle!=null && recycle instanceof CustomView)) before I repopulate it in the adapter. However, I can't find out how you inflate a custom view class from XML. I can find out how you add an inflated view to a custom view, I can find out how you insert a custom view into an XML layout, etc, and obviously I am quite happily inflating these things directly using LayoutInflater, but I can't find an equivalent for generating the custom view itself. I want to reuse the XML I already have; consequently I don't want to program in the elements (and how they look) directly.
I used this to create my own slide gallery, i think it would help.
LinearLayout internalWrapper = new LinearLayout(getContext());
internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
addView(internalWrapper);
this.mItems = items;
LinearLayout generalLayout = new LinearLayout(this.getContext());
generalLayout = (LinearLayout) View.inflate(this.getContext(), R.layout.galleryrow, null);
// inside linear layout
LinearLayout generalLinear = (LinearLayout) generalLayout.findViewById(R.id.rowgenerallin);
// set height & width to the LINEAR
generalLinear.setLayoutParams(new LayoutParams(reference_width, reference_height));
ImageView ivl = (ImageView) generalLayout.findViewById(R.id.arrow_left);
ImageView ivr = (ImageView) generalLayout.findViewById(R.id.arrow_right);
internalWrapper.addView(generalLayout);
In my case, R.layout.gallery_row contains the two images I want to manage, nested by a LinearLayous (rowgenerllin), the internal wrapper is an empty LinearLayout declared in the main layout of your activity.
Double check the LayoutParams code or you will get a big NULL :)
Cheers!

can we bind 3 controls into a single control in android?

I have 2 textviews and one imageview and i want to bind them into a single control so that i can implement horizontalScrollView on them...
Is there a way to merge different controls so that we can use them???
Or is there a way to implement horizontalScrollView on multiple controls simulataneously??
Thanks in advance
You can build an xml layout that you inflate into a GaleryView. Android allows you to build any mashup of controls into a layout and then use that layout for each row in a listview or each item in a sliding galery.
in a ListAdapter you can inflate the layout and set all of the properties.
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater factory = LayoutInflater.from(context);
View row = factory.inflate(R.layout.list_row, null);
TextView main = (TextView)row.findViewById(R.id.labelMain);
TextView details = (TextView)row.findViewById(R.id.labelDetails);
ImageView icon = (ImageView)row.findViewById(R.id.imgIcon);
main.setText(_items.get(position).Title);
details.setText(_items.get(position).Description);
icon.setImageResource(R.drawable.pin);
return row;
}

Categories

Resources