I have a FrameView that's created in my XML layout, and in my code, I'm trying to create a series of new ImageViews and add them as children of this FrameView. The ImageViews are small, only about 15 pixels square, and I want them to show up in various positions around the FrameView (I'm trying to implement what looks like a radar screen). I'm able to create them and add them just fine, and they show up on the screen. However, when I try to set their margins, it doesn't seem to have any effect. No matter what I set, all the ImageViews show up in the top left corner of the FrameView, as opposed to offset by the appropriate margins. My XML layout is listed below, along with the code that generates the child views. Am I doing something wrong? How can I get the margins to show up properly? Or is there a better way to do this than by using margins.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RadarBackground"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/radar_bg">
<FrameLayout
android:id="#+id/RadarFrame"
android:layout_width="320dip"
android:layout_height="320dip"
android:layout_marginTop="25dip">
</FrameLayout>
</LinearLayout>
Java:
for (int i = 0; i < getData().getTargetCount(); i ++) {
int id = getData().getTargetId(i);
Log.d(T.TAG_A, "Radar: plotting target " + id);
TargetView tv = new TargetView(this, id, getData().getTargetName(id));
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(
radarCenterX + (int) (getData().calcTargetRadarX(id, radarSize) / radarScale) - (targetSizeX / 2),
radarCenterY - (int) (getData().calcTargetRadarY(id, radarSize) / radarScale) - (targetSizeY / 2),
0, 0);
tv.setImageResource(R.drawable.radar_civilian);
tv.setOnClickListener(this);
mTargets.add(tv);
mFrame.addView(tv, lp);
}
FrameLayout does not consider the margin parameters if you don't specify the view Gravity. Try specifying the gravity for all the ImageViews.
HTH !
Related
I got a problem with my for loop / with its content.
I want to have this loop in which an image is repeated n times.
In addition these images should have margins at the top so they have some space between each other.
For now these images are laying on top of each other or won't be generated as they should (Result: Drawable "bg_circle" is only displayed once).
The "userinput" will be filled from an EditText in an Alert Dialog.
Here is my code:
int n = Integer.parseInt(userInput.getText().toString());
RelativeLayout layout = findViewById(R.id.TableView);
for(int i = 0; i <= n; i++){
ImageView image = new ImageView(mContext);
image.setImageResource(R.drawable.bg_circle);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(100,100,0,0);
lp.height = 100;
lp.width = 100;
image.setLayoutParams(lp);
layout.addView(image);
}
Where is the mistake?
Use LinearLayout with vertical orientation instead of RelativeLayout. Also, you should use a ScrollView as root element of the layout:
<ScrollView ...>
<LinearLayout android:orientation="vertical" ...>
... programmatically added images...
</LinearLayout>
</ScrollView>
I'd like to use a GridLayout (not GridView) as board for a game like chess or checkers. As I'm a little reluctant to use an xml file with 64 child Views, I've tried adding them programmatically.
To keep things simple, I started with using TextViews as child Views for the GridLayout.
My problem is that the Views are not distributed evenly, and that I don't know how to get an even distribution in my java code. There is no method like "setWeight()" for setting layout_columnWeight and layout_rowWeight.
At present, this is my activity_dynamic_grid_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/ivLogo"
android:background="#ff0000"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<android.support.v7.widget.GridLayout
android:id="#+id/grid_layout"
android:background="#004080"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/ivLogo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp">
</android.support.v7.widget.GridLayout>
I've set the GridLayout width and height to match_parent here, but I'm changing them at runtime using a ViewTreeObserver.OnPreDrawListener in order to get a square board. This works, the colored background is showing a square space as intended.
My onCreate()
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic_grid_layout);
GridLayout gl = (GridLayout) findViewById(R.id.grid_layout);
gl.setColumnCount(8);
gl.setRowCount(8);
for(int i=0; i<gl.getRowCount(); i++)
{
GridLayout.Spec rowSpec = GridLayout.spec(i, 1, GridLayout.FILL);
for(int j=0;j<gl.getColumnCount();j++)
{
GridLayout.Spec colSpec = GridLayout.spec(j,1, GridLayout.FILL);
TextView tvChild = new TextView(this);
tvChild.setText("[ " + i + " | " + j + " ]");
tvChild.setTextSize(18f);
tvChild.setTextColor(Color.WHITE);
tvChild.setGravity(Gravity.CENTER);
GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
myGLP.rowSpec = rowSpec;
myGLP.columnSpec = colSpec;
gl.addView(tvChild, myGLP );
}
}
final View rootView = findViewById(R.id.dynamic_root);
rootView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
{
#Override
public boolean onPreDraw()
{
int w = rootView.getMeasuredWidth();
int h = rootView.getMeasuredHeight();
int min = Math.min(w, h);
ViewGroup.LayoutParams lp = gl.getLayoutParams();
lp.width = min - min % 9;
lp.height = lp.width;
gl.setLayoutParams(lp);
rootView.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
});
}
What I've tried already:
I put one TextView child in the layout file and tried to copy the layout_columnWeight and layout_rowWeight from its GridLayout.LayoutParams:
<android.support.v7.widget.GridLayout
...>
<TextView
android:id="#+id/clone_my_params"
android:text="[ 0 | 0 ]"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_column="0"
app:layout_row="0"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
/>
</android.support.v7.widget.GridLayout>
Additional code in onCreate(), before the double loop:
TextView v = (TextView)gl.findViewById(R.id.clone_my_params);
v.setGravity(Gravity.CENTER);
GridLayout.LayoutParams gridLayoutParamsToCopy = new GridLayout.LayoutParams(v.getLayoutParams());
Inside the loop, I skipped (i,j) = (0,0) and changed
GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
to
GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams(gridLayoutParamsToCopy);
Before the change, all elements were in the upper left corner, the excess space was given to the last row/ column. After the change, the first row/ column had the excess space, no change for the other elements.
Calling gl.invalidate() and/or gl.requestLayout() after the double loop had no effect.
So it seems that I did not manage to set the desired weight by using the copy constructor.
-Here you are ! :>
Button button = new Button(this);
GridLayout.LayoutParams param= new GridLayout.LayoutParams(GridLayout.spec(
GridLayout.UNDEFINED,GridLayout.FILL,1f),
GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
param.height = 0;
param.width = 0;
button.setLayoutParams(param);
To set the weight on the children of your Gridlayout use one of the spec()(like this one) methods that takes a float value.
Another approach would be a either make a custom View(in which case you'll need to manually draw the pieces) or a custom ViewGroup(in which case the custom ViewGroup will just take care of the pieces positioning, this will be appropriate if you plan to have more complex view hierarchies than a simple TextView).
I have a tabWidget defined as:
<TabWidget
android:layout_alignParentBottom="true"
android:id="#android:id/tabs"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:weightSum="5"
android:background="#drawable/bottom_bar_without_bg"
/>
Now I am adding custom ImageViews in it:
Drawable d = getResources().getDrawable(R.drawable.tabbar_button_home);
ImageView img1 = new ImageView(this);
img1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
img1.setImageDrawable(d);
TabHost.TabSpec tSpecHome = tHost.newTabSpec("timeline");
tSpecHome.setIndicator(img1);
tSpecHome.setContent(new MyTabContent(getBaseContext()));
tHost.addTab(tSpecHome);
The problem with this code is that all the images are spaced with each other in middle.
e.g abcdef
I want them to stretch to full width and have space in between them e.g a b c d e
Update the code as follows:
Drawable d = getResources().getDrawable(R.drawable.tabbar_button_home);
ImageView img1 = new ImageView(this);
img1.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f)); // this line changed
img1.setImageDrawable(d);
Explanation:
The third parameter in the LayoutParams constructor is the weight. Each image view now has a weight of 1.0f. So they will spread across the layout.
You have them with WRAP_CONTENT width.
Instead they should be set to a width of 0dp and each one have a layout gravity of 1.
I have a Table Layout defined within LinearLayout as follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#E6E6E6"
android:orientation="vertical" >
<TableLayout
android:id="#+id/fbTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#android:color/darker_gray"
android:stretchColumns="*" >
</TableLayout>
</LinearLayout>
I am adding dynamic rows to the TableLayout as follows:
fbTableLayout = (TableLayout) findViewById(R.id.fbTableLayout);
for (int i = 0; i < 4; i++) {
fbTableRow = new TableRow(this);
fbTableRow.setBackgroundColor(Color.WHITE);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
int leftMargin=10;
int topMargin=2;
int rightMargin=10;
int bottomMargin=2;
layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
fbTableRow.setLayoutParams(layoutParams);
ImageView iv = new ImageView(this);
iv.setBackgroundDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
iv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT,
0.25f));
TextView tv = new TextView(this);
tv.setText("Album "+ i);
tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT,
0.75f));
fbTableRow.addView(iv);
fbTableRow.addView(tv);
fbTableLayout.addView(fbTableRow, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
But I am not able to generate spaces between the rows generated. The layout is as shown in the figure attached.
I have gone through a number of solutions given in stackoverflow to resolve this issue but none of them are working for me. Not sure what I am missing.
Any help would be greatly appreciated.
Thanks in advance.
Add another table row (in the loop) with a blank image. Specify the size of the image to create the size needed for your space.
Have you tried this when you're adding the table row?
fbTableLayout.addView(fbTableRow, layoutParams);
If not, you can try setting the margins on the individual views within the table row, but I'm pretty sure the above should apply the layout params when the row is being added to the table layout.
The docs for the setMargins method include this note:
A call to requestLayout() needs to be done so that the new margins are taken into account. Left and right margins may be overriden by requestLayout() depending on layout direction.
...so one thing to try would be to call fbTableLayout.requestLayout() after you've added all the rows. This probably won't make a difference, since the view should be getting invalidated in the code you've already posted, but it wouldn't hurt to try.
If that doesn't work, you could use another viewgroup (e.g. a FrameLayout) within each table row to contain your ImageView and TextView and set your padding there.
I want to create an input box with a submit button to the right. Between them they should span the width of the screen. Currently I have:
LinearLayout row= new LinearLayout(context);
row.setOrientation(HORIZONTAL);
row.setGravity(Gravity.RIGHT);
EditText input = new EditText(context);
Button submit = new Button(context);
submit.setText("Submit");
row.addView(submit);
row.addView(input,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
myView.addView(row,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
This results in the correct distribution of space: The submit button taking up as much space as it needs, the input button taking up the remaining space, however they are the wrong way round (the submit button is on the left, despite setting the gravity). If I take away the gravity, and reverse the order of adding the elements to the row, the input box takes up the whole width of the screen, and the submit button is not visible. What am I doing wrong?
I'd say it is better to use relative layout and place input to left of the button. But if you really need this with Linear layout you can just use weight parameter:
LinearLayout row= new LinearLayout(context);
EditText input = new EditText(context);
Button submit = new Button(context);
submit.setText("Submit");
LayoutParams inputParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
inputParams.weight = 1;
row.addView(input,inputParams);
LayoutParams buttonParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.weight = 0;
row.addView(submit, buttonParams);
Try adding EditText first setting its width to fill parent and its weight to 1, then the button (width = wrap content)
Items stack in a LinearLayout in the order in which you added them. Switch your two addView calls.
Its typically easier to achieve the right layout with the layout xml files. I.e:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
If you also need to line up buttons on the next line, you can also use a TableLayout. Look at the apidemos for code sample.