I am having an issue with my app, basically i have few layouts like this:
|ABC|
each letter describe one layout(A is the relative layout).
When i am adding an image view to the relative layout, this image view
can move over all the layout. But the relative layout became stretched to whole the width of the screen.
|A|
so basically you can`t see the B,C layouts any more.
This is my function:
public void startGame(){
int i;
for(i=0;i<currentLevelObs.size();i++){
if(currentLevelObs.get(i)==Globals.OBS_ENTRY_INDEX)
break;
}
lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, (i+1));
lp.addRule(RelativeLayout.RIGHT_OF, i);
lp.leftMargin = 25;
ImageView img = new ImageView(this);
img.setLayoutParams(lp);
img.setBackgroundResource(R.drawable.hero);
gameLayout.addView(img);
}
This function called from onCreate method.
You have given layout width to fill parent. So it will take up the whole screen. Thats the problem. Change it to wrap-content and then check.
Add your ABC layouts to a ScrollView, that way you can scroll down to see B and C.
it should be like this:
<ScrollView
..
...
....>
<D
..>
A
B
C
</D>
</ScrollView>
a ScrollView can contain only 1 child, so keep your ABC in a D layout which is in ScrollView
Related
Suppose I have a container like LinearLayout and it has 10 Element(TextView,editText,..etc), so I want to give top margin on each element inside linear layout. But I don't want to give margin individual each element, I want give top Margin to container ( LinearLayout) which apply to its all element . is it possible in android?
LinearLayout layout = yourLayout;
for (int i = 0; i < layout.getChildCount(); i++) {
View view = layout.getChildAt(i);
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
layoutParams.setMargins(0, 10, 0, 0);
view.setLayoutParams(layoutParams);
}
May be you can use paddingTop in LinearLayout but I m not sure.
Try This in your XML file
android:paddingTop="10dp"
Late answer but...
For vertical LinearLayout and layout_marginTop you only need to set it to the first children.
For horizontal LinearLayout and layout_marginTop (or any property that is relative to the parent and not a sibling element), you need an additional layout as child with the layout_marginTop value, another LinearLayout or ConstraintLayout for example.
I have the following method which I call a few times to create a list of buttons. It works well and creates the buttons.
public void CreateButton(int i) {
LinearLayout btnLayout = (LinearLayout)findViewById(R.id.btnLayout);
Button btn = new Button(this);
btn.setId(i);
btn.setText(String.valueOf(i+1));
btnLayout.addView(btn);
}
But each created button is fitting the screen in width, and I would want it to stay side by side, two buttons per row. I managed to set the button to half the screen size using this:
int displaywidth= getResources().getDisplayMetrics().widthPixels;
btn.setLayoutParams(new LayoutParams((int)(displaywidth/2), LayoutParams.WRAP_CONTENT));
It makes the button's width to be half the screen's size, but I can't figure out how to place them side by side. Any help is appreciated.
Change your orientation in your LinearLayout to horizontal.
<LinearLayout
...
android:orientation="horizontal"
... >
...
</LinearLayout>
If you only have a single LinearLayout that is to hold multiple side-by-side buttons you can make horizontal LinearLayouts to hold your button pairs and either nest them in the main veritical layout or utilize another layout, for example RelativeLayout, to get the desired results.
Try using weight property of LinearLayout. If in each row you want only two buttons then give
android:weightSum="1"
to LinearLayout and
android:layout_weight="0.5"
to each button or you can set weight dynamically in your code by
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, xf);
where x represents the float value of layout weight of your button.
for more details about layout weight http://developer.android.com/guide/topics/ui/layout/linear.html
One way to solve the problem is by using their weight and width. Make sure the Linear Layout has a horizontal orientation. Then, use Layout_params to set their width to "Wrap_Content" and their weight to "1". Then both will automatically take up the same amount of space.
I have a RelativeLayout in my Android App. Now I want to show an ImageView in front of that Layout. The problem is that the ImageView is not in the front, it's a bit transparent and I can see things like EditText and Button. I can't change the Layout (setContentView), because the Layout is created dynamically and after setContentView, the Controls are away.
You can add view programmatically, and in the way that it will be on the top!
create id for you top level layout
Now some code (in my case it's relative layout):
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout_id);
ImageView imageView = new ImageView(context)
Drawable rightArrowBlackDrawable = getResources().getDrawable(R.drawable.image);
imageView.setLayoutParams(getLayoutParams());
relativeLayout.addView(imageView);
imageView.bringToFront();
//here just example layout params, use yours params ;-)
private RelativeLayout.LayoutParams getLayoutParams() {
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
return layoutParams;
}
You can bring it to the front once you insert it.
imageView.bringToFront();
If the image is transparent, you can set a white background to prevent things below it from showing.
imageView.setBackgroundColor(0xFFFFFF);
In Android, I have a RelativeLayout named A. It has a view named B in the top of A. It has a view named C in the bottom of A.
Code I have so far:
RelativeLayout A = new RelativeLayout(context);
View B = new View(context);
LayoutParams paramsB = new LayoutParams(LayoutParams.FILL_PARENT, 40);
paramsB.addRule(RelativeLayout.ALIGN_PARENT_TOP);
View C = new View(context);
LayoutParams paramsC = new LayoutParams(LayoutParams.FILL_PARENT, 40);
paramsC.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
A.addView(B,paramsB);
A.addView(C,paramsC);
How do I adjust the RelativeLayout size when the keyboard shows?
I think you can set layout parameters of 'A' relative layout to WRAP_CONTENT so that when keyboard comes from bottom then it will adjust the height with relative layout.I doubt about the rules you added to 'B' and 'C' relative layout because you have added Align_parent_bottom and Align_parent_top to true.
There are two ways to achieve that:
First you can define the following attribute in of AndroidManifest.xml
android:windowSoftInputMode="stateVisible|adjustResize"
Second you can add scrollview as your parent view of your whole view.
I have a relativeLayout like below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/parent" >
<ListView
android:layout_width="360dp"
android:layout_height="600dp"
android:id="#+id/list"
android:inputType="text"
android:maxLines="1"
android:layout_margin="50dp"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
In the java code, I want to add a view to the left of the listview, but it didn't worked:
m_relativeLayout = (RelativeLayout)findViewById(R.id.parent);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.LEFT_OF, m_listView.getId());
Button button2 = new Button(this);
button2.setText("I am button 2");
m_relativeLayout.addView(button2, layoutParams);
only if I set the listview to alignParentRight, it will work. Is this an android bug or I'm missing something?
I always try addView(View child, int index, LayoutParams params), but it might only work in the linearlayout. So is there an normal solution to make the RelativeLayout.LEFT_OF work?
EDIT
I have tried RelativeLayout.BELOW and RelativeLayout.RIGHT_OF, and they worked perfectly, so it means I don't have enough place to get the button? I tried to give more space, but it still not work.
I use Toshiba AT100 (1280*800) and landscape, so the space is enough. Test below and right just same as the left. I think If i put an control A in the relativelayout, then I add control B and decalare it's on the left of the control A, the result should be the control B will push the control A to its right, right?
I think If i put an control A in the relativelayout, then i add control B and declare it's on the left of the control A, the result should be the control B will push the control A to its right, right?
Your assumption is incorrect, the control A will not be pushed to the right unless you specified this with a RelativeLayout.LayoutParams rule. RelativeLayout places its children one one top of each other starting at the top-left corner of the screen if you don't specify placement rules for them. When you add the View A to the RelativeLayout without any rules(like layout_alignParentRight) it will be placed starting from the top-left corner of the screen. Then, when you add the View B, the rule to_leftOf will apply to this View position but this rule doesn't mean anything for the View A who will maintain its position on the screen. This will make View B to be place to the left of View A but outside of the screen as View A bounds start from the left border of the screen.
The Button will be placed to the left of the ListView when you use layout_alignParentRight="true" because there is now space to actually see the Button(it's not outside anymore). addView(View child, int index, LayoutParams params) works in a LinearLayout because the LinearLayout arranges its children in a row or column(depending on orientation) so when you add a View at a specific position, it will push the other Views after it to the right or below(depending on orientation)(there is no relative positioning of the views in a LinearLayout, the only rule is that the children come one after the other).
Starting with the ListView without any rules set on it, here is an example on how to make the Button to appear on the left of the ListView:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button button2 = new Button(this);
button2.setText("I am button 2");
button2.setId(1000);
m_relativeLayout.addView(button2, layoutParams);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) m_listView
.getLayoutParams();
rlp.addRule(RelativeLayout.RIGHT_OF, button2.getId());
The Button will be added as normal to the screen and it will appear starting from the top-left corner of the screen. Without the two lines from the code above the Button and ListView will overlap as this is the normal behavior of RelativeLayout for children without any rules on them. We then explicitly modify the position of the ListView to move it to the right(with the last two line from the code above).
If your variable names are indicative, it's because you are adding the widget to a LinearLayout, so tags for a RelativeLayout get ignored.
This line is the one I'm talking about:
m_linearLayout.addView(button2, layoutParams);
EDIT
You say alignParentRight works... the only difference there is that ot doesn't take an anchor parameter. Perhaps m_listView.getId() isn't returning the proper id. You could step through with the debugger and see if it's returning a proper value.
Maybe you could try calling the id specifically...
layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.list);
To perform it, use predefined view ID or declare one. In values folder create ids.xml then add a Item like this:
<item name="imageViewID" type="id"/>
use this id in your code where you are creating new Instance of view like this:
RelativeLayout layout=new RelativeLayout(context);
ImageView imageView = new ImageView(context);
imageView.setId(R.id.imageViewID);
RelativeLayout.LayoutParams layoutParams = new LayoutParams(50, 50);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(imageView, layoutParams);
TextView textView = new TextView(context);
RelativeLayout.LayoutParams textViewParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
textViewParams.addRule(RelativeLayout.BELOW, imageView.getId());
layout.addView(nameView, nameLayoutParams);
or we can directly use this function View.generateViewId() to perform the same. Like this:
imageView.setId(View.generateViewId());
I think you might have forgotten to add m_listView to the RelativeLayout or m_listView's visibility would be GONE.
Can you please check for that?
setId before align is called, especially for the new object view.
If you are using a custom id and not a regular generated Android id (eg. R.id.my_id), make sure that the id is not equal to 0 (or negative), otherwise the rule will be ignored.