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.
Related
I want to get all views inside of an activity. like;
ArrayList<View> views = new Arraylist<>();
views.addAll(getAllViewsFromActivity());
what I want is "getAllViewsFromActivity()" function. I clearly want to get all the views even a single button. But I couldn't find any clear answer.
For the progress, it must be like this:
MainView : getWindow().getDecorView()
- -
RelativeLayout
- -
Button LinearLayout
-
TextView
How can I get this tree in Android programmatically? and also lets assume that I got this tree, Can I also identify the types of them like: view instanceof Button ?
the view you want to get is clear. so you can use the parent view(such as LinerarLayout) to get the children. so you can try this:
int count = parent.getChildCount();
for(int i =0; i< count;i++){
View child = parent.getChildAt(i);
//here you can to chage the view some proper.
//if you only want to change the background color, it belongs to view, don't
// need to cast.
}
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 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());
}
}
}
Is it possible to disable Touch events for all the views inside a Layout by targetting the ParentLayout. I know this is possible by going through each individual view contained in the Layout but it would be very helpful if there was a way to do it for complete Layout. I have already tried the following but all in vain.
ParentLayout.setClickable(false);
ParentLayout.onFilterTouchEventForSecurity(event);
ParentLayout.onTouchEvent(event);
ParentLayout.setOnTouchListener(l);
...
and others in similar fashion. Any help would be greatly appreciated.
Disable all touch event in parent view. dispatchTouchEvent() and
onInterceptTouchEvent() can all achieve this goal.
Set android:duplicateParentState="true" to all child view, and set
parent android:enable="false"
Consider taking following approach.
public void disableTouch(ViewGroup viewGroup) {
int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount; i++) {
View view = viewGroup.getChildAt(i);
view.setClickable(false);
if (view instanceof ViewGroup) {
disableTouch((ViewGroup) view);
}
}
}
It will traverse through all Child and disable touch event for each of them.
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