Android, find childviews of parent view - android

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

Related

Android : Looping through big numbers of ImageButtons

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

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

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.

Android: Adding multiple Views to a Linear Layout dynamically

I checked the solution here:
Adding multiple views of the same type
Its given that, create a new View everytime you add it instead of changing only 1 view.
But i am doing this:
for (int i = 0; i < 10; i++) {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(
CommentsActivity.LAYOUT_INFLATER_SERVICE);
View cv = vi.inflate(R.layout.item, null);
TextView textView1 = (TextView) cv.findViewById(R.id.tv1);
textView1.setText("-" + i);
TextView textView2 = (TextView) cv.findViewById(R.id.tv2);
textView2.setText("--" + i);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.layout);
insertPoint.addView(cv, 0, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
so its like creating a new inflater and view for every i. But i am only getting the last item.
ie.., only 1 inflatedView with tv1 as -9 and tv2 as --9
seems like everytime i go into the for loop, the old view is being replaced by the new view. How to add all the 10 views??
ThankYou
usually I use this
private void renewDetail(){
llDetail.removeAllViews();
for (int i = 0; i < 10; i++) {
llDetail.addView(new ChildDetailNotePieDiagram(context, "Name", 1000, 10));
}
}
the logic is first I clear all view from the parent layout and then add view to the parent layout.
where llDetail is a linear layout and I create a linear layout class ChildDetailNotePieDiagram and add it to the linear layout so basically it's a different solution from what you use now
but I think you can try my solution if you want :)
feel free to ask any question about this in the comment

Categories

Resources