I have an xml which contains circular layout and a relative layout like below
<RelativeLayout
android:id="#+id/relativelayout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.andreilisun.circular_layout.CircularLayout
android:id="#+id/circular_layout"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I am filling my circular layout dynamically through text views,my layout does not show any thing. Actually i am unable to show latest circular view. Any idea on how to add my circular layout to relative layout through code?
I have done with a simple example.
create xml with
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relative_layout_id"/>
implement in .jav file
RelativeLayout relativeLayout = findViewById(R.id.relative_layout_id);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams(220, 220);
CircularLayout circularLayout = new CircularLayout(this);
circularLayout.setCapacity(12);
circularLayout.setLayoutParams(layoutParams);
for (i in 0 until 12) {
TextView textView = new TextView(this);
textView.setText(i.toString());
circularLayout.addView(textView);
//circularLayout.invalidate();
}
relativeLayout.addView(circularLayout);
Note: If you want to add Margin, padding, parent_center etc, to the CircularView. Make changes to the layoutParams as per your requirement
Related
I have an Android Flow layout hash_tag_layout
<org.apmem.tools.layouts.FlowLayout
android:id="#+id/hash_tag_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/view_padding"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin">
</org.apmem.tools.layouts.FlowLayout>
The problem is I cannot change the margins on dynamically added views.
int pixels = convertDensityPixelsToPixels(5);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0,pixels,pixels); //<---<< This line has no effect
TextView tv1 = new TextView(ctx);
tv1.setPadding(pixels,pixels,pixels,pixels); //int left top right bottom
tv1.setBackground(ctx.getResources().getDrawable(R.drawable.hash_tag_bubble_format));//drawable
tv1.setText("# Asd");
tv1.setLayoutParams(lp);
layout.addView(tv1);
If I change hash_tag_layout layout to a regular linear layout, than I get margins!
<LinearLayout
android:id="#+id/hash_tag_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
So I am lead to conlude that Android Flow layout does not allow me to add margins to views added dynamically!
Any idea how to change the source code or find a way around this? Maybe add a transparent border around the dynamically added views?
Just change your LinearLayout.LayoutParams to FlowLayout.LayoutParams.
Here is an issue discussion on GitHub.
I see that you asked a question 3 years ago, but I have just faced the same problem :)
I want to create a custom layout to reduce redundancy in the code. Currently every layoutfile has about 30 Lines of code which are identical.
My goal was to create a custom layout/view which can hold in itself children.
<BaseLayout xmlns:...>
<!-- Normal Content -->
<Button />
<Label />
</BaseLayout>
While the above xml holds most of the content, the BaseLayout is in itself an xml containing other views and functionality:
<FrameLayout xmlns:...>
<LinearLayout><!-- contains the Header--></LinearLayout>
<LinearLayout><!-- INDIVIDUAL CONTENT HERE--></LinearLayout>
<FrameLayout><!-- contains the loading screen overlay --></FrameLayout>
</FrameLayout>
So all children from the above xml should be inserted into second linear-layout. I have already succeeded into doing so. But am confronted with layout problems (match parents does not match parents and only wraps)
My approach was extending the LinearLayout with following logic:
/**
* extracting all children and adding them to the inflated base-layout
*/
#Override
protected void onFinishInflate() {
super.onFinishInflate();
View view = LayoutInflater.from(getContext()).inflate(R.layout.base_layout, null);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.base_layout_children);
while(0 < getChildCount())
{
View child = getChildAt(0);
LinearLayout.MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
removeViewAt(0);
linearLayout.addView(child, layoutParams);
}
this.addView(view);
}
Is there a better, cleaner approach to capsule the xml and reuse a basis layout? How do I fix the match_parent issue?
While writing this post and thinking hard how to explain best, the solution for the match_parent issue became clear. Though the question remains if there is a better approach for the whole problem.
//Solution:
this.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//wrong:
this.addView(view);
Suppose you have two layout files. common_views.xml and layout_main.xml. You can include content of one layout file into another like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/common"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/common_views" />
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/common"
>
</WebView>
</RelativeLayout>
I'm doing this in Xamarin, so there will be slight deviations in the casing and names of methods.
I have a RelativeLayout that has ads and is placed at the bottom. Unfortunately, the ads block part of the playable map, so I'm attempting to move it to the top when the player moves near the bottom. I initialize the banner with the following code:
_banner = new RelativeLayout(this);
_lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
_lp.AddRule(LayoutRules.AlignParentBottom);
_lp.AddRule(LayoutRules.AlignParentLeft);
AddContentView(_banner, _lp);
I'm now attempting to move it to the top, but am failing. I've tried removing and re-adding it, but that does nothing.
var lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
((FrameLayout)(_banner.Parent)).RemoveView(_banner);
lp.AddRule(LayoutRules.AlignParentTop);
lp.AddRule(LayoutRules.AlignParentLeft);
AddContentView(_banner, lp);
I've also tried setting the LayoutParameters, but that throws an exception. What am I doing wrong?
Thank you.
-Nick
From your code I gather you are nesting a RelativeLayout (contains the ad) inside a FrameLayout. Your RelativeLayout only matches your parent in width, not height.
As such the RelativeLayout doesn't have the same size as your FrameLayout.
Your ad is thus touching both top and bottom of the RelativeLayout. And that RelativeLayout is always aligned to the top of the FrameLayout.
To solve this you have three options:
Make the RelativeLayout match the entire height of the FrameLayout and position your ad within that RelativeLayout.
Change the FrameLayout into a RelativeLayout, that way your alignParentBottom parameter will start working.
Use a 'layout_gravity' parameter on the RelativeLayout to tell the FrameLayout you want this View sent to the bottom.
Effectively your code is doing:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:alignParentBottom="true"
android:background="#FF0000"
android:layout_width="match_parent"
android:layout_height="240dp"
/>
</FrameLayout>
But for stuff to work you need to use android:layout_gravity which is a FrameLayout.LayoutParameter.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_gravity="bottom"
android:background="#FF0000"
android:layout_width="match_parent"
android:layout_height="240dp"
/>
</FrameLayout>
Just paste the two pieces in a layout.xml and use the Android Designer to view the difference.
I need to create one full screen android activity programatically as shown in the image below:
The two buttons should remain at the bottom of the screen.
Dummy content will consist of different components (textviews, radio buttons, checkboxes...) and will be populated dynamically.
This is the code I have so far:
//Main Layout
FrameLayout lLayout = new FrameLayout(this);
lLayout.setBackgroundColor(Color.parseColor("#0099cc"));
lLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//Navigation layout
LinearLayout l = new LinearLayout(this, null, R.style.ButtonBar);
LayoutParams bottomLayout = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
l.setLayoutParams(bottomLayout);
l.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
l.setBackgroundColor(Color.parseColor("#66000000"));
l.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams buttLayout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//previous section button
previousButton = new Button(this);
previousButton.setLayoutParams(buttLayout);
previousButton.setText("Previous section");
previousButton.setOnClickListener(this);
l.addView(previousButton);
//next section button
Button nextButton = new Button(this);
nextButton.setLayoutParams(buttLayout);
nextButton.setText("Next section");
nextButton.setOnClickListener(this);
l.addView(nextButton);
//add components
TextView tView = new TextView(this);
tView.setText("Dummy text");
tView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
lLayout.addView(tView);
lLayout.addView(l);
setContentView(lLayout);
Here is what the code produces:
Tthere are several points that do not work as intended:
1. Buttons are at the top and not the bottom.
2. Buttons do not spread out nicely
3. TextView I added as a test is shown behind the buttons. I will have many different widgets on the screen and expect them to be larger than one screen. I would like to have a scroll option but with all those widgets not to be seen behind the two buttons that are supposed to be at the bottom of the screen.
The following xml is exactly what you would need:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/dynamiclayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/navigationlayout"
android:orientation="vertical" >
</LinearLayout>
<RelativeLayout
android:id="#+id/navigationlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Previous Section" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Next Section" />
</RelativeLayout>
</RelativeLayout>
Now programatically inflate dynamiclayout and add all your dynamic views into it.
Your root view is a FrameLayout, which is intended for only one child View. It is also frequently used to create overlapping Views, as all of a FrameLayout's children will be drawn in the same place on screen.
Replace your FrameLayout with a RelativeLayout. Make sure you update your LayoutParams references to use RelativeLayout.LayoutParams. You will also need to set the navigation LinearLayout to align with the parent View's bottom like so:
RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lps.addRule(RelativeLayout.LayoutParams.ALIGN_PARENT_BOTTOM, true);
Though I really would suggest using XML. It will make your life far simpler.
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