I've tabs as activities. In one of the tabs I have a search dialog. When I inflate the search result my tabs disappear. How can I inflate the result while preserving the tabs?
Here's how I inflate the result:
LinearLayout layout = (LinearLayout) findViewById(R.id.quick_search);
if(data==null) {
LinearLayout empty = (LinearLayout) mInflater.inflate(R.layout.empty, null);
layout.addView(empty);
}
else {
LinearLayout workRequest = (LinearLayout) mInflater.inflate(R.layout.quick_search, null);
layout.addView(workRequest);
}
Actually I want to display the searched result in place of LinearLayout 'quick_search' and make sure that the view above 'quick_search' is not lost. My current method inflates the result to occupy the whole screen. How can I avoid that?
See the below link
https://stackoverflow.com/a/2336047/1441666
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child);
item.addView(child);
If quick_search or empty have layout attributes of layout_width and/or layout_height set to match_parent, then they will occupy the whole screen when you inflate them. Either set them to wrap_content or some fixed value to avoid this.
You need a ActivityGroup within the Tab where you want to change the Activity. If you just go on and create a new Activity and display it, your Tab Layout is no longer visible.
See this: http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity
Related
I've got a LinearLayout which includes among others two ListViews. Each ListView has it's own ArrayAdapter. Now the Scrolling shouldn't be in the ListViews, the user should see the whole Lists and scroll the whole view. Sourrounding the LinearLayout with a ScrollView doesn't work because of the inerhit Scrolling Views... .
How can I expand the ListViews and let the user scroll only the outer view?
You should not use a ListView this way.
List Views are meant to recycle views, which it cannot do if its not the view that is scrolling.
You could simply use a LinearLayout and add every single view to the layout. This would be better than using a ListView.
(This does not mean it is the best solution)
This can be done easily in RecyclerView. But in this case you are using ListViews so try listView.setScrollContainer(false);
may be it works for you
You should inflate Views Like this.
// from content View of activity or fragment
listView = (LinearLayout) findViewById(R.id.sos_list_view);
listView.removeAllViews();
List<SosObject> sosList = batabase.getAllItems();
for(SosObject t: sosList) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Item layout
View view = inflater.inflate(R.layout.sos_prewivew_item, null);
TextView comment = (TextView) view.findViewById(R.id.sos_comment_text);
TextView date = (TextView) view.findViewById(R.id.sos_date_text);
TextView id = (TextView) view.findViewById(R.id.sos_answer_id);
TextView tittle = (TextView) view.findViewById(R.id.answer_tittle);
listView.addView(view);
}
}
Your xml Should look like:
<ScrollView>
<LinearLayout>
</LinearLayout > // Fist list
</LinearLayout > // Second list
</LinearLayout>
</ScrollView>
ListViews cannot be overriden to lose the scrolling ability. They seem to be designed as top-level objects that occupy the whole screen.
So basically you have 2 options here:
Convert all your elements into ListView's items
use LinearLayouts instead of ListViews and wrap all elements in a ScrollView
I am adding multiple XML Views programmatically. I'm using a layout inflater to add them and there are no problems with that.
But I'm not able to modify the TextView in each of them.
For example, consider I am adding a LinearLayout three times in my final View. I have a TextView in that linear layout. I extract it using findViewById and if I setText("hello"); it is being reflected in the first layout, but not the second and third.
Will the inflater create new ids dynamically when adding multiple XML elements?
To answer part of your question, no: Ids are not dynamically generated by LayoutInflater.
When you say you are adding the views, where are you adding them? You mention a final View.
You don't include your code, but I assume you are calling Activity.findViewById. If you call this.findViewById from your Activity, you are traversing the entire View hierarchy and finding the first view with such an Id.
What you need to do, is iterate through all of the LinearLayouts that contain your TextViews and call findViewById on each of them.
for (LinearLayout layout : <fill in>) {
((TextView) layout.findViewById(R.id.yourid)).setText("hello");
}
As for the <fill in>. Hard to tell how to fill it in without your code. It seems like you are adding these into a parent view right? Let's assume we have a parent View parent = some view;.
You just have to get all of its children and iterate. There are a few ways you can do it depending on which subclass of View the parent is. Let's keep it simple and assuming the parent is just another LinearLayout.
In that case the for loop changes to something like:
for (int i = 0; i < parent.getChildCount(); i++) {
final LinearLayout layout = (LinearLayout) parent.getChildAt(i);
((TextView) layout.findViewById(R.id.yourid)).setText("hello");
}
I have 3 relative layouts and i need to create a single setcontetntview which is a combination of these layouts each added 3 times and sharing equal screen space and it needs to be done programatically. I started by creating a new layout adding just 2 screens,
RelativeLayout primaryLayout = new RelativeLayout(this);
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
RelativeLayout newLayout = (RelativeLayout)layoutInflater.inflate(R.layout.layout3, null, false);
RelativeLayout newLayout1 = (RelativeLayout)layoutInflater.inflate(R.layout.layout4, null, false);
primaryLayout.addView(newLayout);
primaryLayout.addView(newLayout1);
setContentView(primaryLayout);
This is displaying only the layout4. Also, if i add same layout again, its giving error stating the specified child already has a parent, you must call removeview() on the child parent first. Please help!
THe reason its displaying only layout4 is that the primaryLayout is a relative layout. Unless you specify something telling it where they go in that layout, it all goes in the upper left corner. So everything is being put on top of one another.
You can't add the same layout multiple times. You'd have to reinflate it once for every version you want, and add the results. Its like OOP- the RelativeLayout is an instance of the layout, inflating instantiates a new one.
You must use addRule to the layout param to set the views with respect to each other. As for the complaint about re-adding a view, you can try two independent instances of the view. While it will be the same layout, the parent won't know that.
EDIT:
To make occupy half screen you have to tell the parent view how much space to allot for the child view:
RelativeLayout.LayoutParams forChild = new RelativeLayout.LayoutParams(30,40);
childView.setLayoutParams(forChild);
Im adding other layout to my current layout like this:
LinearLayout parent = (LinearLayout)findViewById(R.id.average_layout_salaryListContainer);
for (String month : mVacation.getMonthNameList()) {
LinearLayout childElement = (LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.salary_list_element, null);
TextView monthTextView = (TextView) childElement.findViewById(R.id.salary_list_element_textView_month);
monthTextView.setText(month);
parent.addView(childElement);
}
While I have a lot of month (12 or more) some of added elements are invisible, but I can't scroll down. How to avoid this?
You should put your main layout inside a ScrollView that way you will be able to scroll down when stuff gets too long
more info here
You should wrap your entire layout inside a ScrollView that way your layout should fit nicely and scroll when necesary.You should take a further look here : ScrollView
Try wrapping your parent LinearLayout view with a ScrollView. Check out this post:
Android - Way to set the page to scroll if it doesn't fit?
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.