remove dynamic radio buttons - android

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

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.

How to make a set of views invisible in android

I'm trying to make a set of views (that include several textviews and buttons - all in different parent layouts, but in the same activity) invisible if a particular condition is evaluated to false.
The conventional way to do that would be:
findViewById(R.id.myview).setVisibility(View.INVISIBLE);
My question is, will I have to do this for all the views one by one that I want to be invisible, And then toggle them back when I want them visible?
Or, is there a way to avoid the code-repetition?
If the Views are in different parents , you can't do it directly, but you can implement a method to change the visibility of a bunch of Views if you want to keep your code clean:
List<View> relatedViews = new ArrayList<>();
// ...
relatedViews.add(view1);
relatedViews.add(view2);
relatedViews.add(view3);
// ...
changeVisibility(relatedViews, View.INVISIBLE);
// ...
private void changeVisibility(List<View> views, int visibility) {
for (View view : views) {
view.setVisibility(visibility);
}
}
As a side note, you may want to change the visibility to View.GONE instead of View.INVISIBLE so it doesn't take any space in the layout.
you can use something like this. It's not the most elegant solution but works.
The idea is give to each view that you want to hide a same content description, because in the same layout you can not use same id for multiple view. With the same content description you can find all views in your layout and hide them.
That's an example considering the first layout as Linear. You can change obviously ;)
public class TestActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout rootLayout = (LinearLayout)findViewById(R.id.rootLayout);
int childcount = rootLayout.getChildCount();
for (int i=0; i < childcount; i++){
View v = rootLayout.getChildAt(i);
if(v.getContentDescription() != null && v.getContentDescription().equals("invisibleView")){
v.setVisibility(View.INVISIBLE);
//I suggest you to use GONE instead of INVISIBLE to remove the space of the view
}
}
}
}
in your xml give to the object that you want to hide this property
android:contentDescription="invisibleView"
Use varargs method for show or hide multiple views.
for example if you have views like view1, view2.....etc
then just call setVisibility(View.VISIBLE,view1,view2)
public static void setVisibility(int visibility, View... views){
for (View view : views){
view.setVisibility(visibility);
}
}

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

How do I make a ZoomControl on a MapView transparent?

I have a bunch of buttons on my MapView already transparent so I would like to make the built in zoom control at the bottom transparent also. The getZoomControl() on MapView is deprecated. Anyone have an idea of how to get a hold of the Buttons in the control without the getZoomControl?
Edit:
So I figured it out. It turns out that the ZoomButtonsController has a container that is just a ViewGroup. I can parse through that containers children to find the object that is an instanceof a ZoomControl, which is a down the line instance of a ViewGroup. I can parse through the children of the ZoomControl to get the ZoomButtons that it contains. getBackground() of the ZoomButton and setAlpha().
Here is my code:
android.widget.ZoomButtonsController zbc = mapView.getZoomButtonsController();
ViewGroup container = zbc.getContainer();
for (int i = 0; i < container.getChildCount(); i++) {
View child = container.getChildAt(i);
if (child instanceof ZoomControls) {
ViewGroup zoomC = (ViewGroup)child;
for (int j = 0; j < zoomC.getChildCount(); j++) {
View btn = zoomC.getChildAt(j);
if ( btn instanceof ZoomButton ) {
((ZoomButton)btn).getBackground().setAlpha(120);
}
}
break;
}
}
To my understanding it is deprecated in android.view.View but it resides in android.widget.ZoomButtonsController. Not sure if that helps. Let me know.
Here is some documentation.
remove the built in zoom buttons and use your own buttons and set their actions to perform zoom actions. you can make the buttons look the way you want.
Try using setBuiltInZoomControls(boolean) as suggested in the doc.

Go through XML and set all TextView to be invisible

Hey this is my first time trying anything like so I don't know if this is even close to the best way to do this, but I thought it would work. I'm trying to go through the XML layout file and set all TextView's to be INVISIBLE. When the following method is called I get a NullPointerException
public void numPlayerSetup(){
{
for(int i = 3; i <= 6; i++)
for(int z = 2; z <= 10; z++){
int resID = getResources().getIdentifier("TextView"+Integer.toString(z) + Integer.toString(i), "id", this.getPackageName());
if(resID != 0){
TextView text = (TextView) this.findViewById(resID);
text.setVisibility(View.INVISIBLE);
}
}
Let me know if you have any suggestions. Thanks!
Well, are the ids going to change? If not, just set up an int[] of TextView IDs, and loop through those, e.g.:
int[] ids = {
R.id.tv1, R.id.tv2, R.id.tv3 //...
}
for(int i : ids) {
TextView tv = (TextView)findViewById(i);
tv.setVisibility(View.INVISIBLE);
}
I would definitely not try using reflection, it'd be a lot less efficient than doing it in other ways. If you don't know the IDs of the TextViews ahead of time, why not try something like this (assuming your root layout is a RelativeLayout):
RelativeLayout root = (RelativeLayout)findViewById(R.id.root);
for(int i = 0; i < root.getChildCount(); i++) {
View v = findViewById(i);
if(v instanceof TextView) {
((TextView)v).setVisibility(View.INVISIBLE);
}
}
Since you've already accepted, I'll assume method 1 worked, because I just realized I was horribly off on method 2. It should be getChildAt(i), not findViewById(i), as that would just be calling findViewById(0|1|2|...etc). Below is a corrected version:
RelativeLayout root = (RelativeLayout)findViewById(R.id.root);
for(int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if(v instanceof TextView) {
((TextView)v).setVisibility(View.INVISIBLE);
}
}
I haven't tested that, but it sounds good in theory. :)
Did you do some debugging?
For instance, have a look if the resIDs match. At least they're not zero, if your error occurs right there. (Have you checked that?).
It may sound odd, but you could also check if getResources() and this.findViewById() refer to the same object.
That's all I can think of for the moment.
As the others have indicated, the most obvious cause of a NullPointerException would be that you're not checking if text is null before calling setVisibility(). You'll want to look into why text would be null in the first place- but you should check your pointers regardless.
Or just go with kcoppock's alternative.
Any particular reason you're using setVisibility? It isn't always appropriate- I believe all it does is keep draw from being called, which can cause issues later if you hoped to treat it as drawn but invisible.

Categories

Resources