I am creating a widget with a few textviews and a few image views.
And when i want to apply a font to all the text views, i need to get one by one with the ids
and set the font or color accordingly.
But how do i get all the text view components from the layout so that
i can set the color to them all by iterating through the components ?
Try this
for( int i = 0; i < myLayout.getChildCount(); i++ ){
if( myLayout.getChildAt( i ) instanceof TextView ){
(TextView) myLayout.getChildAt( i ).setTextColor(#FF0000);
}
}
with myLayout is your ViewGroup
Related
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.
My requirement is to apply '6' different colors to '6' ImageViews on programatically. In my Layout i can have more than six ImageViews and these colors has to be applied only to those specific ImageViews.
for (int i = 0; i < view.getChildCount(); i++) { }
But how do i identify those specific 6 ImageViews among all the views?
You can mark specific ImageViews with setTag() function (eg. imageView.setTag("Specific")) on their initialization and after that do next
for(i = 0; i < view.getChildCount(); i++){
//check for view is ImageView
if(view.getChildAt(i) instanceof ImageView){
//check for tag
if(((String) view.getChildAt(i).getTag()) == "Specific"){
//code for set color
}
}
}
Hope it helps
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());
}
}
}
I have an Android astronomy app where I need to tint the UI reddish for use at night. Although I have a scheme that works well for many (most??) UI elements, I'm having trouble with the CompoundButtons: CheckBox and RadioButton.
The basic idea is to retrieve the Drawable for the UI element and if it has one to set a color filter on it. My problem is finding the appropriate Drawable for the compound buttons. I would think that getCompoundDrawables() would be what I'd need, but the returned array for the 4 drawables always contains nulls for the 4 elements.
Here is the recursive code I call on the top level view to try to colorize the UI elements.
public static void setNightVisionBkg( View view )
{
if ( view instanceof ViewGroup )
{
Drawable drawable = view.getBackground();
if ( drawable != null )
drawable.setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
ViewGroup group = (ViewGroup) view;
int numChildren = group.getChildCount();
for ( int i = 0; i < numChildren; i++ )
{
View v = group.getChildAt( i );
Drawable d = v.getBackground();
if ( d != null )
d.setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
if ( v instanceof ViewGroup )
{
setNightVisionBkg( (ViewGroup) v );
}
else if (v instanceof CompoundButton)
{
CompoundButton compBtn = (CompoundButton)v;
Drawable drawables[] = compBtn.getCompoundDrawables();
for (int j = 0; j < drawables.length; j++)
if (drawables[j] != null)
{
drawables[j].setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
}
}
}
}
}
Note that it is the latter part where it is getting the Drawables for the CompoundButton that fails to work (all drawables are null).
Any thoughts on how to do this? I know I could set my own custom drawables, but I'd prefer to use the standard ones and just set a colorFilter if possible.
I solved my problem in a slightly different way. Ended up subclassing CheckBox (and RadioButton). In the subclass I override:
protected boolean verifyDrawable( Drawable drawable )
and in this method I set the colorFilter on the drawable. Works great.
You don't progress "normal" non-ViewGroup Views. Furthermore your testing
for ViewGroup in a for loop which can ONLY be ran if the view is a ViewGroup.
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