ForEach element in layout or screen - android

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

Related

Xamarin dynamically added views using inflater inside loop adding only one entry

I followed the below link to dynamically add a layout multiple times using inflater and AddView()
Is there a way to programmatically create copies of a layout in android?
I used a loop to create multiple entries. But only one entry is comming up which is the result of last loop index
Below is my C# code
I can see only one child inside the parent which is the result of last loop.
What I missed?
var parent = FindViewById<RelativeLayout>(Resource.Id.ParentLayoutWrapper);
for (int i = 0; i < 4; i++)
{
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, parent, false);
var txtView = view.FindViewById<TextView>(Resource.Id.textViewSample);
txtView.Text = i.ToString()+ " Android application is debugging";
txtView.Id = i;
parent.AddView(view, i);
}
The original post you worked from had a LinearLayout as the parent layout, not a RelativeLayout like you have. When you add a view (or another layout) to a LinearLayout, it gets positioned below (when LinearLayout has vertical orientation) any existing elements in the layout. However, the elements in a RelativeLayout need to use positioning properties to determine where they will be in the RelativeLayout, so every time you add the new layout, RepeatingLayout, since you are not changing the layout options, the view/layout is added over the existing view/layout. So change the parent layout to a LinearLayout in your layout file and then this should work:
LinearLayout parent = FindViewById<LinearLayout>(Resource.Id.parentLayout);
for (int i = 0; i < 4; i++)
{
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, null);
var tv = view.FindViewById<TextView>(Resource.Id.textViewSample);
tv.Text = i.ToString() + " Android application is debugging";
parent.AddView(view);
}
Trying to do the same with a RelativeLayout as the parent layout highly complicates things unnecessarily.

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

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

getting checkBox element count

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

remove dynamic radio buttons

Does anyone have a quick and easy method for removing dynamically added buttons from a Linear Layout in Android? They seem to be kept in the saved instancestate and I don't want them when I return to the activity.
You may clear ALL views in a linear layout by using the following code:
LinearLayout myLayout = (LinearLayout)findViewById(R.id.your_linear_layout);
myLayout.removeAllViews();
However, if you are looking to remove only the views that were dynamically added (and you have views in there that are not) this will not work.
If you need to do it this way you can do something like this
LinearLayout l = (LinearLayout)findViewById(R.id.linearLayout);
List<View> removeViews = new ArrayList<View>();
int count = l.getChildCount();
for (int i = 0; i < count; i++) {
View v = l.getChildAt(i);
if (v != null && v.getTag() != null
&& v.getTag().toString().equals("dynamicView")) {
removeViews.add(v);
}
}
for (View v : removeViews) {
l.removeView(v);
}
Please notice the v.getTag() != null && v.getTag().toString().equals("dynamicView") portion. You don't have to do it this way, however, this would be an easy way to differentiate between a view you added and a view that was statically created.
Edit in order for this to work when you create the view you need to call view.setTag("dynamicView"); of course

Categories

Resources