I have the following problem. This is a little sketch of my layout (only the intereseted part):
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
>
<LinearLayout
android:id="@+id/horizontalLayoutContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
Now I have added four buttons to the horizontalLayoutContainer ID layout. I want to be able to disable the buttons within the container, but of coarse I'm not :(( How can I get the views related to the Buttons that have been added dinamically? (I can't use the generateID on the buttons because it is available from API 17).
I have also tried to set the ID manually, starting from 0, and the result is that I'm able to catch the one with ID = 0 but not the others :(( Please help
Thanks
EDIT: ViewGroup mLayout = (ViewGroup) ((FragmentActivity) mContext).findViewById(rootLayoutID);
if(mLayout!=null){
for(int i = 0; i < mLayout.getChildCount(); i++)
{
View view = mLayout.getChildAt(i);
if(!(view instanceof Button))
setViewsEnabled(mContext, view.getId(), enable);
else{
Log.debug(tag, "I'm a button with ID: "+ view.getId());
view.setEnabled(enable);
view.setClickable(enable);
}
view.setEnabled(enable);
}
mLayout.setEnabled(enable);
}
}
With this portion of code I'm able to disable everything into my layout except for the buttons. As you can see I've also created a log that is printed 4 times (exactly the number of the button) but with ID = 0;
SOLVED: I've found a solution by assign programmatically (incrementally starting from 0) an ID and then making an if/else as shown below in order to recognize the right activity in which buttons are defined:
/*
* Se l'istanza e' MainActivity disabilito/abilito manualmente i bottoni
*/
if(mContext.getClass() == MainActivity.class){
ViewGroup horizontalScrollViewViewGroup = (ViewGroup)
((FragmentActivity) mContext).findViewById(R.id.horizontalLayoutContainer);
if(horizontalScrollViewViewGroup!=null){
Log.debug(tag, "Buttons Number: "+horizontalScrollViewViewGroup.getChildCount());
for(int i = 0; i < horizontalScrollViewViewGroup.getChildCount(); i++){
View view = ((FragmentActivity) mContext).findViewById(i);
if(view!=null){
view.setClickable(enable);
view.setEnabled(enable);
}
}
}
}
Whenever you create dynamic view you can assign an id to that view as
final int ID=1001;
Button btn=new Button (this);
btn.setId(ID);
and get the view using
findviewbyid(ID) ;