I need to get an dynamically added view position in LinearLayout with vertical orientation.
For example i have 4 TextViews added dynamically on LinearLayout, then i need to change position of text colour at 3rd position will be in different color.How can i achieve it by getting position of added views.
You can do it just like that
ViewGroup parent;
int position;
for(int i = 0; i < parent.getChildCount(); ++i) {
int currentViewId = parent.getChildAt(i).getId();
if(currentViewId == wantendViewId) {
position = i;
}
}
That's (in my opinion) the simplest way
If you always know the number of TextViews in your LinearLayout, you can just use the function getChildAt( int position ). This returns a View which you can then cast to a TextView to be able to perform the desired operations.
If you do not know the number of elements you could set the id of each TextView (in order to be able to identify a particular one) and then run through them like this:
for( View view : myLinearLayout )
if( view instanceof TextView && view.getId().equals( idToSearchFor ) )
//Do what needs to be done.
I see following options:
Declare some id's in resources in form of <item type="id">first</item> and assign them to
views in adding to layout, after that use normal findViewById() mechanism
Assign some tags to views you're adding to a layout via setTag method and after that use findViewWithTag mechanism
Remeber position of your views and use them vie getChildAt method
I got simple option.
suppose you add
View v;//any view
linearlayout.addview(v);//add in layout
While u want to modify view.
simpaly remove old view.
linearlayout.removeView(v);
add new update view object
v-updated new view
linearlayout.addview(v);
Related
i looking for id of linearlayout child but when call getid it's return -1
here is my code.
LinearLayout layout = (LinearLayout)findViewById(R.id.schedul1);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
int id=layout.getChildAt(i).getId();
i trace and found that child variable not null but getId return -1.
why?
any suggestion for get id of child of linearlayout view.
Unless you're setting the ID of each view in your layout XML, or when you programmatically create the View, it will return the default (-1).
Views may have an integer id associated with them. These ids are typically assigned in the layout XML files, and are used to find specific views within the view tree.
http://developer.android.com/reference/android/view/View.html
The javadocs for getId() in the Android source code also clearly state this behavior:
a positive integer used to identify the view or NO_ID if the view has no ID
http://developer.android.com/reference/android/view/View.html#getId()
And following through, NO_ID is equal to -1:
http://developer.android.com/reference/android/view/View.html#NO_ID
The following piece of code is inflating the same view for 20 times. Since inflating is costly. I want to inflate it only one, and use the same view for 20 items, i just want to change the visible data in the UI.
LinearLayout ll = new LinearLayout(context);
for (int i = 0; i < 20; ++i) {
View itemView = inflater.inflate(getLayoutId(), parent, false);
itemView.setText(data.getName(i);
ll.add(itemView);
}
I want something like this.
LinearLayout ll = new LinearLayout(context);
View itemView = inflater.inflate(getLayoutId(), parent, false);
for (int i = 0; i < 20; ++i) {
itemView.setText(data.getName(i);
ll.add(itemView);
}
But am not able to use the itemView obj this way.
Can anyone tell me how to use the view many times once it inflated.
You cannot do that. If you think its costly then find another way to create your layout.
But consider gridViews for example. They create a ton of views and show them and that works great.
You cannot add the same object of a view 2 times to a layout. Every object has its own state which in your case in a way says that you will share the state between all your 20 views which doesn't make sense to do, meaning changing the text on one textView will change it on all the rest...
Just inflate 20 seperate views and fill them appropriately.
Also consider using ListView or GridView if you actually have the exact same view it can offer some nice features like view recycling.
You should use ViewHolder patern:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
it should do all things You want
Is this a TableLayout? If it is, how to make this underline under first row and different color per row?
Yes this is the table layout you have to set background color of the table row please refer for following link click here
Assuming it is derived from a ViewGroup (object containing children, for example TableLayout or ListView), it is easy to access all of its children (rows) and do something with it. For example alternating backgrounds:
final int childCount = myGroup.getChildCount();
for(int i = 0; i < childCount; i++) {
View child = myGroup.getChildAt(i);
if(i % 2 == 0) {
child.setBackgroundColor(color1);
} else {
child.setBackgroundColor(color2);
}
}
Same goes for changing the first row, just use myGroup.getChildAt(0) and modify that particular child.
You can use listview with custom item view. Just add header in listview (also footer can be added):
ListView list = (ListView) findViewById(R.id.listView);
View headerView = inflater.inflate(R.layout.header, list, false);
list.addHeaderView(headerView);
Yes it is a Table layout
i have created the similar very easily.
You can also add click events for the text in each row to perform different action.
I have a grid view which I populate using a custom adapter.
While populating the gridview I give each element inside a unique tag.
Once this gridview is populated, I wish to find a specific element inside the gridview with its tag. How do I find this?
Currently I'm doing this:
gridviewobject.findViewById(thetag);
// gridview object is the object of the gridview that i have populated.
What you have written above will work, except be aware that a) searching for a view by its tag is probably the slowest method you could use to find a view and b) if you try requesting a view with a tag and that view is not currently visible, then you will get null.
This is because GridView recycles its views, so essentially it only ever makes enough views to fit on screen, and then just changes the positions and content of these as you scroll about.
Possibly a better way might be to do
final int numVisibleChildren = gridView.getChildCount();
final int firstVisiblePosition = gridView.getFirstVisiblePosition();
for ( int i = 0; i < numVisibleChildren; i++ ) {
int positionOfView = firstVisiblePosition + i;
if (positionOfView == positionIamLookingFor) {
View view = gridView.getChildAt(i);
}
}
Essentially findViewWithTag does something similar, but rather than comparing integers it compares the tags (which is slower since they're objects and not ints)
when my ListView is being filled by a custom Array Adapter, I check for a certain parameter in the current list item, and if it is false, I want to return an empty row. At the moment, I am doing this by inflating an empty xml file, but that creates ugly list item dividers when a number of items should not be shown, and is obviously not the best way ;-)!
if (list.get(position).equals("i-dont-want-this-in-the-list-view")){
View empty=inflater.inflate(R.layout.empty_row, parent, false);
return(empty);
}
I have tried to return "null", but it clearly expects a View to be returned. Is there anything I could do without having to filter the list that is being used for the ListView beforehand (I want to keep it intact as I am doing other things with it, too).
Thanks,
Nick
To fix the issue of line dividers, remove the line dividers from the ListView and put your own in inside the individual items.
Inflate a View and setVisibility to VIEW.GONE
What's about:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
//use a global Array to set the rows visible/hidden and return empty rows
if(disArray[position] == false) return new View(getContext());
... // continue with your getView() implementation
return convertView;
}
to update the ListView use
adapter.notifyDataSetChanged();
What're in the rows that aren't empty? Images can be replaced by empty, transparent pngs and text views can be set to "", etc.
View.Gone will give u empty list row. try using layout params and set to zero value like this:
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(0,0);
viewholder.programRow.setLayoutParams(layoutParams);
Layoutparams should be used as according to parent viewgroup used