I tried to disable the children widgets by disabling the wrapping relativeLayout, but that doesn't work, is there any other way to do that? I think this is necessary because sometimes we don't want to setEnable(xx) on every children widgets. So is it possible for me to do that? or is there some similar way ? thx a lot~
Maybe you can try something like this:
RelativeLayout myLayout = (RelativeLayout findViewById(R.id.linearLayout1);
for ( int i = 0; i < myLayout.getCount(); i++ ){
View view = myLayout.getChildAt(i);
view.setEnabled(false); // Or whatever you want to do with the view.
}
I believe you can extend the layout class and implement a new method that does the job. Also, you can check this question: How to disable all content inside linear layout in android?
Related
I want to do something like this:
View v1= new View(this);
v1.setBackgroundResource(R.drawable.pic1);
View v2 = new View(this);
v2.setBackgroundResource(R.drawable.pic2);
v1.addView(v2);
RelativeLayout.LayoutParams params;
rl = (RelativeLayout) findViewById(R.id.activity_main);
rl.addView(v1,params);
I know the code is wrong. It just show how I want to do.
Some websides said that viewGroup may help me to achieve this.
I had tried but never can v2 be shown on the screen.
Does someone can tell me how to achieve this?
Views cannot contain other Views. It simply doesn't work this way in Android.
If you want to place a View inside a different View, the containing View must extend the ViewGroup class.
There are several classes that can help you achieve this:
LinearLayout - if you want your views to be aligned vertically or horizontally.
RelativeLayout - if you want your views to be positioned relative to each-other and/or the container
There a many more.
Hope this helps.
Looks ok but the problem might be that you create a View with no params in a relativelayout. Also I don't know if you can create a View object think it have to be TextView, ImageView or something like that :)
I have a horizontal scroll view, I also have a relativelayout as it's child. I am adding child views of this relativelayout dynamically. I have a header text which should be update when I scroll according to respective child views. How can I do this because I am able to get the current focused item in horizontal scroll. Please give me some suggestion or examples which can be helpful for me, thanks..
You should specify an OnTouchListener for your HorizontalScrollView and in it's onTouch() method detect the type of MotionEvent and change your TextView's color to the appropriate
If you are creating this childs dinamically you can set a tag to them with the content you want to show in the header TextView.
//Creating RelativeLayout childs
TextView newChild = new TextView(this).
newChild.setTag(textToShowWhenThisItemIsFocused);
Then if you know which item is focused you just have to get the tag.
// "selected" is the focused view
header.setText((String) selected.getTag());
When to use the second code depends on your implementation. Since you didn't provide any code it's hard to know how to monitor the scroll, but i.e. you could control Touch Events and update the header when the user is moving his finger over the screen (you should also take into account the inertia after the user stops tapping).
EDIT: How to get the focused View
First of all, I barely have experience doing things like this, so I'm not sure if this is going to work or if there are better ways to do it. I'll just tell you the way I would approach this problem.
To be able to know the focused View you need to know the coordinates where this View should be. Since it's an horizontal ScrollView we will need the X coordinate. Since we want the View in the middle of the ScrollView I would do it like this:
private int centerSV;
private ScrollView mScrollView;
...
centerSV = mScrollView.getWidth()/2;
Now we have the center of the ScrollView. Now we need to know which child is in this position:
private int getFocusedChildId(){
for(int i=0; i<mChilds.length; i++){
int childLeftCoord = mChilds[i].getLeft() - mScrollView.getScrollX();
if(childLeftCoord <= centerSV && centerSV <= childLeftCoord + mChilds[i].getWidth())
return mChilds[i].getId();
}
// No view found in the center, maybe ScrollView wasn't full. Return the first one
return mChilds[0].getId();
}
Again, I'm not sure if this is going to work, it's just an idea of how to approach your issue. Also, you should take this into account:
getWidth() and getHeight() of View returns 0
I am adding multiple XML Views programmatically. I'm using a layout inflater to add them and there are no problems with that.
But I'm not able to modify the TextView in each of them.
For example, consider I am adding a LinearLayout three times in my final View. I have a TextView in that linear layout. I extract it using findViewById and if I setText("hello"); it is being reflected in the first layout, but not the second and third.
Will the inflater create new ids dynamically when adding multiple XML elements?
To answer part of your question, no: Ids are not dynamically generated by LayoutInflater.
When you say you are adding the views, where are you adding them? You mention a final View.
You don't include your code, but I assume you are calling Activity.findViewById. If you call this.findViewById from your Activity, you are traversing the entire View hierarchy and finding the first view with such an Id.
What you need to do, is iterate through all of the LinearLayouts that contain your TextViews and call findViewById on each of them.
for (LinearLayout layout : <fill in>) {
((TextView) layout.findViewById(R.id.yourid)).setText("hello");
}
As for the <fill in>. Hard to tell how to fill it in without your code. It seems like you are adding these into a parent view right? Let's assume we have a parent View parent = some view;.
You just have to get all of its children and iterate. There are a few ways you can do it depending on which subclass of View the parent is. Let's keep it simple and assuming the parent is just another LinearLayout.
In that case the for loop changes to something like:
for (int i = 0; i < parent.getChildCount(); i++) {
final LinearLayout layout = (LinearLayout) parent.getChildAt(i);
((TextView) layout.findViewById(R.id.yourid)).setText("hello");
}
I would like to do the same effect with linearlayout like when you call showDialog. It is easy to disable all components of linear layout, but how can I change the color?
Is it possible to throw some shadow on layout?
Thank you for answer.
Thanks to Asok advice I found this:
Set Alpha/Opacity of Layout
It is working correct for me.
The way how I setClickable to ALL child:
TraverseChildren(GetChildren(_llRest), false);
where
private void TraverseChildren(ArrayList<View> childrenList, boolean b) {
for (View view : childrenList) {
view.setClickable(b);
view.setEnabled(b);
if (view instanceof ViewGroup)
TraverseChildren(GetChildren((ViewGroup)view), b);
}
}
private ArrayList<View> GetChildren(ViewGroup view) {
ArrayList<View> children = new ArrayList<View>();
for (int i = 0; i < view.getChildCount(); i++)
if (view.getChildAt(i) != null)
children.add(view.getChildAt(i));
return children;
}
I don't think I understood well what you want to do. If you just want to change the color of the layout you can do it in the XML declaration using the android:background attribute or programmatically using the method setBackground or setBackgroundResource on your layout object. If you want to make the activity that you are going to display look somewhat like a dialog (with a translucent background and such) the easiest way is to leave everything on that layout like you would normally do and apply a theme to the activity on the manifest:
<activity android:theme="#android:style/Theme.Dialog">
You can setAlpha to the parent to apply a transparency to all of the Views children.
Here is an example of the approach I took when I created a View to overlay my primary layout:
RelativeLayout mainRelativeLayout = (RelativeLayout)findViewById(R.id.mainlayout);
mainRelativeLayout.setAlpha((float).45);
My mainRelativeLayout, in this case, contains many ImageViews and TextViews which all inherited the transparency. Which gave me the effect of having a shadow.
setAlpaha of the View class is only supported for API 11+ or Platform Version / Android OS Ver 3.0+
Im adding other layout to my current layout like this:
LinearLayout parent = (LinearLayout)findViewById(R.id.average_layout_salaryListContainer);
for (String month : mVacation.getMonthNameList()) {
LinearLayout childElement = (LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.salary_list_element, null);
TextView monthTextView = (TextView) childElement.findViewById(R.id.salary_list_element_textView_month);
monthTextView.setText(month);
parent.addView(childElement);
}
While I have a lot of month (12 or more) some of added elements are invisible, but I can't scroll down. How to avoid this?
You should put your main layout inside a ScrollView that way you will be able to scroll down when stuff gets too long
more info here
You should wrap your entire layout inside a ScrollView that way your layout should fit nicely and scroll when necesary.You should take a further look here : ScrollView
Try wrapping your parent LinearLayout view with a ScrollView. Check out this post:
Android - Way to set the page to scroll if it doesn't fit?