I add a graphview programatically :
LinearLayout.LayoutParams Lparams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, p.y/2);
//same result with fill_parent or match_parent
Lparams.gravity = Gravity.LEFT;
firstGraph.setPadding(10, 0, 10, 0);
firstGraph.setLayoutParams(Lparams);
LinearLayout RelLayout = (LinearLayout) findViewById(R.id.relLayout);
RelLayout.setGravity(Gravity.LEFT);
RelLayout.addView(firstGraph, 0);
I obtain this:
What i want is to eliminate that left gap of the graphview, I don't know why is there, I only set a padding of 10 that is what you have in right side.
Any Suggestions? Thansk in advance.
the layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
That space most likely appears because it is the space reserved for the axis labels(and as you probably don't set any labels it's an empty space). If you do need to remove it you'll have to modify the graph view's code and change the values in GraphViewConfig(as those seem to be the values used).
setPadding(int left, int top, int right, int bottom) is the sytax of setPadding. The first argument is for the left padding. You are giving 10. Try setPadding(0, 0, 10, 0)
Related
I'm trying to programmatically add many TextView to a RelativeLayout but I am unable to do that when TextView reach the end of the display right next TextView inflate in a new line.
RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/tag_cloud"
android:padding="10dp">
</RelativeLayout>
Code:
if (categoriesCursor.moveToFirst()){
do {
TextView tagElement = (TextView) getLayoutInflater().inflate(R.layout.tag, null);
tagElement.setText(categoriesCursor.getString(2));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.setMargins(0, 0, pixels, pixels); // llp.setMargins(left, top, right, bottom);
tagElement.setLayoutParams(llp);
tagCloudLayout.addView(tagElement);
} while (categoriesCursor.moveToNext());
}
tag.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:textColor="#ff000000"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>
Thanks
It´s not really clear what You are asking, but If I understand You the right way, You want to have only one line with textViews, or? Also, You are using wrong Layout Params, if You want to add some views to a relativeLayout side by side, I think You will get it with:
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
Where the last parameter stands for the layout weight attribute. With LayoutWeight and MATCH_PARENT, all views will be drawn with equal size.
Consider using a ListView which contains TextView, and that way you'll be able to take advantage of cell re-use etc. Here's a good tutorial
Finally I put one LinearLayout vertical oriented and inside LinearLayouts horizontal oriented with three TextView inside. Isn't the best solution but it works for me.
I am trying to dynamically create buttons, and they will be of varying size and in varying positions.
I have the code to create a button of varying size, but I am stuck on changing the position.
I am using linearlayout and am trying to use setMargins to move the button around, but it seems to be changing the margin within the button. My code is as follows:
public void button(int a, int b) {
newButton = new Button(this);
newButton.setText("HELLO");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
ViewGroup.LayoutParams params = layout.getLayoutParams();
params.width = a;
params.height = b;
layout.requestLayout();
layout.addView(newButton, layoutParams);
}
Here is my manifest for this bit:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="58dp"
android:layout_toRightOf="#+id/textView1"
android:text="Button" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="50sp"
android:layout_height="40sp"
android:orientation="vertical" >
</LinearLayout>
Do you understand what a LinearLayout is and why you're using it? Every child of the layout snaps in place. If I have a LinearLayout that's vertical and it has 3 children they will be on top of each other. I can change their gravity so they are attracted to different margins but to "change" its position is impossible depending on what you mean by "change".
Check out the other layouts. You may want to use a RelativeLayout.
Why don´t you use button.setX(float x) and button.setY(float y)? You'll need to use RelativeLayout instead of the LinearLayout. It's more easy but it's only available since api 11...
Hi i am using following layout structure inside LinearLayout
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv1"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textColor="#000" />
<TextView
android:id="#+id/tv2"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:paddingRight="10dp"
android:textColor="#000" />
</TableRow>
</TableLayout>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320px"
android:layout_height="320px"
android:gravity="center" >
</RelativeLayout>
and want to set relative layout width and height dynamically from java file instead of setting it to 320px in the xml file but not able to do that , Orientation change is not an issue for as i restricting it to only in Portrait mode. It is possible to set the relative layout on full screen by using match_parent but i have to put another views on the screen so is it possible or i have to achieve it another way...
Android does NOT refresh layout of views with wrap_content once it has been displayed.
Even with invalidate() or requestLayout().
So if you add a child view, or modify the content dynamically, you're screwed.
getLayoutParams().height = x plus requestLayout() are a good solution if you know the "x", and if you need to do it ONLY ONCE.
After that the wrap_content in LayoutParams is lost, since you have overridden it.
To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with wrap_content. The code and instructions to use are available here:
https://github.com/ea167/android-layout-wrap-content-updater
Enjoy!
try using this
getLayoutParams().height= x;
requestLayout(); or invalidate();
give an id to your relative layout in .xml :
android:id="#+id/relativelayout"
now in your java file write this:
RelativeLayout Rl = (RelativeLayout) findViewById(R.id.relativelayout);
RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedwidth,
RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedHeight);
Rl.setLayoutParams(layout_description);
From below code u can get device height and width:-
Display display = getWindowManager().getDefaultDisplay();
int width = (display.getWidth() );
int height = (display.getHeight() );
paramsTop ur realative layout:-
Now u can set height or width what you want.
paramsTop = new RelativeLayout.LayoutParams(width, height);
You can set views dimensions dynamically using LayoutParams.
Something like this:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
I faced this problem, the best solution for Change Relative layout width and height dynamically like this
RelativeLayout relMain = (RelativeLayout) convertView.findViewById(R.id.relMain);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width =200;
params.height =200;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relMain.setLayoutParams(params);
If you intending to change Height, then this would make a trick.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
I encountered same issue when working with LinearLayout which has wrap_content and one child as TextView which had match_parent.
Remove the TextView programatically and then add it again.
linearLayout.removeView(textView)
linearLayout.addView(textView)
I know it sound stupid but it works.
In my case calling invalidate didn't work, only this worked.
Depending on your implementation you need to take care of view index inside its parent
I'm programmatically changing the margin of a layout inside a framelayout. My goal is to make a sliding view like the Facebook app. Is it possible to avoid this layout to be resized? This is my layout moving:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#00a2ff"
android:gravity="center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivGPSSearching"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/actionbar_gps_searching" />
</LinearLayout>
<ImageView
android:id="#+id/ivStarsFilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/actionbar_star" />
</LinearLayout>
I don't want the left LinearLayout to be resized. I hope you will understand what I want :)
Thanks
To achieve something similar we extended a HorizontalScrollView - just overriding onInterceptTouchEvent and onTouchEvent to always return false.
Then you just need to put your menu in the left side and the content on the right (the content must match the screen width).
Finally setup the initial HorizontalScrollView scroll and bind to a button click a event to change the scroll position(horizontalScrollView.scrollTo or horizontalScrollView.smoothScrollTo).
I hope this helps.
The below is an example code for using TranslateAnimation and set listener for the animation and in
#Override
public void onAnimationEnd(Animation animation)
{
//Just set the layout params for your view (layout) which is animated,
//to make your view shift to the new position
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutWidth, LayoutHeight);
params.leftMargin = 100; //for example you want to shift 100 px from left
params.rightMargin = -Layoutwidth; //this will avoid your view
//from shrinking and make it as wide as its width was, and - negative value will
//move it towards right
otherLayout.setLayoutParams(params);
}
I hope it make you people clear how to move your view after animation
I've been scratching my head around this for the last few hours as well.
Turns out that if you change both opposite margins, the views inside the ReleativeLayout will not be resized nor moved around:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.setMargins(0, - yOffset, Integer.MIN_VALUE, yOffset);
This is crazy but it works...
My layout structure is like this
LinearLayout
FrameLayout
ImageView
ImageView
FrameLayout
TextView
LinearLayout
I have set margin's for the two ImageView which are inside FrameLayout. But FrameLayout margins are discarded and it always set's the Image to top left corner. If i change from FrameLayout to LinearLayout the margin's work properly. How to handle this ?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/inner1"
>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="24px"
android:layout_height="24px"
android:id="#+id/favicon"
android:layout_marginLeft="50px"
android:layout_marginTop="50px"
android:layout_marginBottom="40px"
android:layout_marginRight="70px"
/>
<ImageView
android:layout_width="52px"
android:layout_height="52px"
android:id="#+id/applefavicon"
android:layout_marginLeft="100px"
android:layout_marginTop="100px"
android:layout_marginBottom="100px"
android:layout_marginRight="100px"
/>
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title"
android:layout_marginLeft="10px"
android:layout_marginTop="20px"
android:textColor="#FFFFFF"
android:textSize = "15px"
android:singleLine = "true"
/>
</LinearLayout>
I had the same issue myself and noticed that setting the layout_ margins does not work until you also set your ImageView's layout gravity i.e. android:layout_gravity="top" in the XML resource file, or from code: FrameLayout.LayoutParams.gravity = Gravity.TOP;.
To make it more clear why. The FrameLayout.onLayout() call contains this (in api v2.3.4 at least):
// for each child
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int gravity = lp.gravity;
if (gravity != -1) {
// handle all margin related stuff
So if gravity is -1, there will be no margin calculation. And the thing is, gravity in FrameLayout.LayoutParams is defined by:
gravity = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity, -1);
So if gravity is not set, there will be no margin calculation.
add your xml this attribute and re run
android:layout_gravity="top"
everything is Ok!
and you dont set new layout params like this;
FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(WallpapersActivity.ScreenWidth/2, layH);
use like this:
FrameLayout.LayoutParams llp = (LayoutParams) myFrameLay.getLayoutParams();
llp.height = 100;
llp.width = 100;
myFrameLay.setLayoutParams(llp);
Taken from the FrameLayout docs (link)
The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits).
This seems to describe the fact that it'll strip margins out. Like boulder mentioned, you could try switching to padding as it can be used to produce a similar effect if done properly.
Out of curiosity, you mentioned that it does work fine when using a LinearLayout container, why the choice of FrameLayout?
Have you tried android:layout_gravity ? Try using android:padding in you ImageViews instead of android:layout_margin. AFAIK margins doesn't work properly on Frame layout. I even had to write custom layout for that purpose once. BTW, how do you want allign you ImageViews?
try setCropToPadding(true) to ImageView ,this should be helped!
you have to set your ImageView's layout gravity top i.e. android:layout_gravity="top" in the XML resource file, or from code: FrameLayout.LayoutParams.gravity = Gravity.TOP