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

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.

Related

Send a view to back in Relative Layout

I am having so many views to in a relative layout which is able to zoom and rotate. what I am trying to achieve is, when I long click a view it should able to send that particular view to last .
what I do is
private void moveToBack(View currentView) {
ViewGroup vg = ((ViewGroup) currentView.getParent());
for (int i = 0; i < vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
if (!v.equals(currentView)) {
vg.bringChildToFront(v);
break;
}
}
}
This will set the another view above the selected view I need to send that particular view to last. MIn sdk is 17 and I can't change that.
We don't have something like bringToback() method so only we could modify the order of childviews. I tried once to achieve it using tricky way :
public void sendViewToBack(View child) {
final ViewGroup mParent = (ViewGroup)child.getParent();
if (null != mParent) {
mParent.removeView(child);
mParent.addView(child, 0);
}
}

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());
}
}
}
}

Is it possible to show the preview of the activity in another activity as a scrollable preview?

I have a long scroll view where it contains Text views,edit texts,radio buttons,check boxes,Sliders etc..
I want to show the preview of the scroll view and the contents whatever the user had entered and which should be non editable.
Is there any possibility in android that we can do this?.
I have two suggestions
1.Disabling the scroll view from entering inputs and
2.Showing the preview of the scroll view.
is there any possibilities of these two or else some other methods to achieve this in android.
need help, thanks in advance..
This method helped to disable the views.
private static void setViewAndChildrenEnabled(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
setViewAndChildrenEnabled(child, enabled);
}
}
}
hope this will help some one else.check this

Android, find childviews of parent view

I have problem finding childviews of parent view in android, i do the following
i have a relative layout with 2 textviews and 1 edit text view(for example call this whole view as c1)
i add the above view c1 to a linear layout using linearlyt.addView(c1)
i add multiple such child views to parent view
After adding many such child views to linearlyt, i start filling out the edit text of each child view, the question is How can i get the content/text set in Textview of child view whose edit text is being filled out.TIA
Try this..
public void findEditText(View parentview) {
if (!(parentview instanceof EditText)) {
System.out.println(((EditText)parentview).getText());
}
// If a layout container, iterate over children and seed recursion.
if (parentview instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) parentview).getChildCount(); i++) {
View innerView = ((ViewGroup) parentview).getChildAt(i);
findEditText(innerView);
}
}
}
The first answer is good. If you need something more specific to your case given the constant structure of your rows...:
for (int i = 0; i < yourLinearLayout.getChildCount(); i++) {
RelativeLayout relativeLayout = (RelativeLayout) yourLinearLayout.getChildAt(i);
TextView textView1 = relativeLayout.getChildAt(0);
TextView textView2 = relativeLayout.getChildAt(1);
EditText editText = relativeLayout.getChildAt(2);
...
}

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.

Categories

Resources