How to know linearlayout has reached its last child item - android

Actually In my project I am dynamically adding a child item inside linearLayout but when last child is reached inside linearLayout i have to load other child same as facebook scrolling. But to know when the last element of linearLayout is arrived?

getChildCount() and also getChildAt() like this may be help you -
int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
View v = ll.getChildAt(i);
if(i == (childCount -1)){
// Do your Task here
}
}

Code snip :
LinearLayout myLayout;// ur layout
int childCount = myLayout.getChildCount();
View v = null;
for(int i=0; i<childCount; i++) {
v = layout.getChildAt(i);
//do something with your child element
}

Related

How to get view of hidden menu?

I must implement pop up tooltips in my android app, so I need to get View objects of interesting for me screen elements.
But I have some problem with View from toolbar menu. I need to get an object View menu item, which is shown below.
Someone knows how to get the object View of this UI element? Thanks.
I found the next is not very good, but working way to implement it.
int count = mToolbar.getChildCount();
for (int i = 0; i < count; i++) {
View view = mToolbar.getChildAt(i);
Log.e("View type ", mToolbar.getChildAt(i).getClass().getSimpleName());
if (view instanceof ActionMenuView) {
int childCount = ((ActionMenuView) view).getChildCount();
for (int y = 0; y < childCount; y++) {
View overFlowMenuButton = ((ViewGroup) view).getChildAt(y);
Log.e("ActionMenuView type", ((ViewGroup) view).getChildAt(y).getClass().getSimpleName());
if (overFlowMenuButton.getClass().getSimpleName().equalsIgnoreCase("OverflowMenuButton")) {
Log.e("Need view ", overFlowMenuButton.getClass().getSimpleName());
}
}
}
}

Android - Set onEditListener for all EditTexts under a parent

Is there any easy way to set onEditListener to all EditTexts under a parent instead of making them one by one?
My idea is to show a save button only when the users change their profile info on EditTexts.
Hmmm, you can call this recursion in onCreateView() if parent is fragment or onCreate() if parent is activity:
private static void enumLayout(LinearLayout ll) {
int childcount = ll.getChildCount();
for (int i = 0; i < childcount; i++){
View v = ll.getChildAt(i);
if (v instanceof LinearLayout) {
enumLayout((LinearLayout) v);
} else if (v instance of EditText) {
// set listener here
}
}
}
ll is root layout in your fragment/activity.

Recycle an inflated layout in Android

I have to add multiple view programatically to a HorizontalScrollView but i can not add the same inflated view twice. So i have to re-inflate my XML layout N times. An adapter solution is not an option.
There is a way to recycle this layout without re-inflating?
Thanks to all.
UPDATE: My code is something like this.
View view = getLayoutInflater().inflate(R.layout.my_view, null);
HorizontalScrollView h = (HorizontalScrollView)findViewById(R.id.scroll);
for(int i = 0; i < 25; i++)
{
// Process textview and images inside the view
h.addView(view);
}
I am not sure this what you are looking for we can inflate layout like this
LayoutInflater.from(context).inflate(R.layout.abc_action_bar_decor, parent, true);
HorizontalScrollView h = (HorizontalScrollView)findViewById(R.id.scroll);
LinearLayout parent= new LinearLayout(context);
parent.setOrientation(LinearLayout.HORIZONTAL);
h.addView(parent);
for(int i = 0; i < 25; i++)
{
View view = getLayoutInflater().inflate(R.layout.my_view, parent, true);
// Process textview and images inside the view
}

Disable all items inside linear layout including items inside nested linear layouts

I want to disable all the field inside the scroll view shown in the picture. I tried using the code below but the code only disables the direct child of linear layout and doesn't disable child for the nested linear layout. How can I disable all child including the children of nested layouts?
LinearLayout myLayout
= (LinearLayout)v.findViewById(R.id.addEditSection1);
for (int i = 0; i < myLayout.getChildCount(); i++) {
View view = myLayout.getChildAt(i);
view.setEnabled(false);
}
Try this recursive function:
public void disableAllViews(View v){
v.setEnabled(false);
if(v instanceof ViewGroup){
for (int i = 0; i < ((ViewGroup)v).getChildCount(); i++) {
View view = ((ViewGroup)v).getChildAt(i);
disableAllViews(view);
}
}
}
And call it as
LinearLayout myLayout
= (LinearLayout)v.findViewById(R.id.addEditSection1);
disableAllViews(myLayout);
Quiet trivial: You got to check recursive.
E.g.:
protected void disableViewElements(ViewGroup container) {
for (int i = 0; i < container.getChildCount(); i++) {
if(container.getChildAt(i) instanceof ViewGroup ) {
disableViewElements((ViewGroup) container.getChildAt(i));
}
else {
View view = container.getChildAt(i);
view.setEnabled(false);
}
}
}
This is really easy with droidQuery:
$.with(myLayout).selectAll().disable();
I am using this in my project
public void setAllViewsEnabled(View view, boolean enabled) {
if (view instanceof ViewGroup)
for (int i = 0; i < ((ViewGroup)view).getChildCount(); i++)
setAllViewsEnabled(((ViewGroup)view).getChildAt(i), enabled);
view.setEnabled(enabled);
}
You can check to execute the last line weather the current view is a TextBox. You can also decide to enable it after using this function.
Remember that if you disable a nested ViewGroup vg1 and then you disable the containing ViewGroup vg0, re-enabling vg0 will result in enabing also vg1. To prevent this you have to save a boolean for each ViewGroup and to change the method.

getting checkBox element count

Working with apis, I am uncertain of the number of checkBoxes that will appear in the application. How can get the count of the number of checkBoxes produced in the application ?
Also, these checkBoxes are declared inside a listView.
You can loop though all the child views in you layout and find out how many of them are checkboxes. Looping and checking is done in following way:
ll is LinearLayout here
int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
View v = ll.getChildAt(i);
if (view instanceof CheckBox) {
count++;
}
}

Categories

Resources