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);
}
}
Related
I am applying images to my ImageButtons in code, but I'm wondering if there is a better way than shown in my example. I definitely know that it's not the correct way to do it as it can take way too much time with large numbers of views.
btn1.setImageResource(R.drawable.plus);
btn2.setImageResource(R.drawable.plus);
btn3.setImageResource(R.drawable.plus);
btn4.setImageResource(R.drawable.plus);
btn5.setImageResource(R.drawable.plus);
btn1.setAdjustViewBounds(true);
btn1.setPadding(0,0,0,0);
btn2.setAdjustViewBounds(true);
btn2.setPadding(0,0,0,0);
btn3.setAdjustViewBounds(true);
btn3.setPadding(0,0,0,0);
btn4.setAdjustViewBounds(true);
btn4.setPadding(0,0,0,0);
btn5.setAdjustViewBounds(true);
btn5.setPadding(0,0,0,0);
you need to get the root view in the activity, cast it to ViewGroup, get all children, check their type and update accordingly. Namely as follows,
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
int children = rootView.getChildCount();
for (int i = 0; i < children; i++) {
View view = rootView.getChildAt(i);
if (view instanceof ImageButton) {
((ImageButton) view).setImageResource(R.drawable.plus);
((ImageButton) view).setAdjustViewBounds(true);
((ImageButton) view).setPadding(0,0,0,0);
}
}
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
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);
...
}
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.
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.