getting checkBox element count - android

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++;
}
}

Related

Android: How to programmatically select a group of Views and modify it?

My requirement is to apply '6' different colors to '6' ImageViews on programatically. In my Layout i can have more than six ImageViews and these colors has to be applied only to those specific ImageViews.
for (int i = 0; i < view.getChildCount(); i++) { }
But how do i identify those specific 6 ImageViews among all the views?
You can mark specific ImageViews with setTag() function (eg. imageView.setTag("Specific")) on their initialization and after that do next
for(i = 0; i < view.getChildCount(); i++){
//check for view is ImageView
if(view.getChildAt(i) instanceof ImageView){
//check for tag
if(((String) view.getChildAt(i).getTag()) == "Specific"){
//code for set color
}
}
}
Hope it helps

ForEach element in layout or screen

I have been trying to hack a foreach to all elements of type label in screen or layout, with no luck!
My goal is to translate all screen1.labels.text, the translation are in list which has lists of pair (label.text, translation).
Is this possible in App-inventor?
Have you thought of getting an array list of sub view and iterating thought them. the below will work for direct children of the screen. if you need sub view as well you can use recursion
RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_screen_to_search);
for (int i = 0; i < layout.getChildCount(); i++)
{
View child = layout.getChildAt(i);
if(child instanceOf TextView)
{
txtView = (TextView)child;
if(txtView.getText().length!=0)
{
yourTranslateFunction(txtView.getText());
}
}
}

Get count of child views?

I have a LinearLayout which includes x amount of LinearLayouts (children) and a bunch of other views such as TextViews, Relative views etc..
How does one return the amount of children LinearLayout views only?
Normally you would use ViewGroup.getChildCount(), but since you want only the count of LinearLayouts you would need to create an algorithm to do so:
public int linearLayoutChildCount(ViewGroup parent) {
int count = 0;
for (int x = 0; x < parent.getChildCount(); x++) {
if (parent.getChildAt(x) instanceof LinearLayout) {
count++;
}
}
return count;
}
Of course, doing something like this is not ideal since it uses reflection which can be slow on Android.

How to know linearlayout has reached its last child item

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
}

Restrict the scroll up of list view after a particular row

The title is little confusing let me explain my problem ,i have some data in list view and its is scrollable and there is no issue in that sort of problem.my requirement is while i scroll and for example the list views first visible portion is 2 then it should stop the scroll up.But we can scroll down,its like restring the scroll up of list view after a particular row is the first visible item in that list view.i tried some of the code but it has a problem it disable the entire scroll i cant scroll down the list view i use this method to disable the scroll of listview
public void enableDisableView(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int idx = 0; idx < group.getChildCount(); idx++) {
enableDisableView(group.getChildAt(idx), true);
}
for (int idx = 0; idx < group.getChildCount(); idx++) {
enableDisableView(group.getChildAt(idx), true);
}
}
}
i also used
mListView.setSelectionFromTop(1, 0);,smooth scroll etc
but it does not works.is there any idea or solution to achieve this aim.
Thank you

Categories

Resources