Problem with inflating xml-Layout on custom layout class - java

I have a custom ConstraintLayout class (Card.java) which overrides the onDraw() method to draw a hexagon in his background. On the foreground i try to have three TextViews to display three numbers.
For this I inflate a card.xml in the constructor of Card. The TextViews are displayed, but not at the right position. They should match the width and height of the Card and then position itself to the top-left and top-right corner and one to the bottom of the Card. But they do something like shrink itself and go to the top-left corner.
I have tried to change the root element of card.xml to "merge" instead of "...ConstraintLayout" but this doesn't change anything.
I also tried to use Guidelines to position the TextViews relative to its width. I try to prevent the use of fixed margins, so the Text is always at the right place, also when the size of the Card changes.
Card.java:
public class Card extends ConstraintLayout {
private int mSize;
private Path mHexagon;
private Paint mPaintHexagon;
private TextView mT1, mT2, mT3;
public Card(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
inflate(context, R.layout.card, this);
// Numbers
mT1 = findViewById(R.id.num1);
mT2 = findViewById(R.id.num2);
mT3 = findViewById(R.id.num3);
// Hexagon
mSize = Field.getHexSize(); // Size is used to calculate the
setPath();
mPaintHexagon = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintHexagon.setColor(0x50888888);
mPaintHexagon.setStyle(Paint.Style.FILL);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mHexagon, mPaintHexagon);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = 2 * mSize;
int height = (int) (Math.sqrt(3) * mSize);
Log.d(TAG, "Measure w:" + width + " h:" + height);
setMeasuredDimension(width, height);
}
}
card.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/num2"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:text="2"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/num1"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:text="1"
app:layout_constraintStart_toEndOf="#+id/num2"
app:layout_constraintEnd_toStartOf="#+id/num3"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/num3"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:text="3"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"/>
</android.support.constraint.ConstraintLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#color/colorAccentDark"
android:padding="5dp">
<de.portugall.markus.takeiteasy.Card
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="#+id/card"/>
</android.support.constraint.ConstraintLayout>
Screenshot Card in Layout-Debug mode

'onMeasure()` has some rules that you are not strictly following. I have seen these rules broken with impunity, but I think that you are being caught, but we will push on.
In onMeasure() you are setting the height and width of the custom layout but ConstraintLayout still understands the layout as wrap_content. You will need to set the layout params to the new height and width. Add the following code to the end of onMeasure():
// Although we have measured the layout, we need to tell ConstraintLayout in the
// LayoutParams that the size is not longer "wrap_content".
ViewGroup.LayoutParams lp = getLayoutParams();
lp.width = width;
lp.height = height;
setLayoutParams(lp);
The second issue that you have is that you are adding a ConstraintLayout (card.xml) to a ConstraintLayout (your custom layout), but you are not setting the constraints. In the constructor for Card.java, add the following to set the constraints:
ConstraintLayout layout = (ConstraintLayout) inflate(context, R.layout.card, this);
// We have added R.layout.card to a ConstraintLayout (this custom layout), so we need
// to make sure that it is constrained properly.
ConstraintSet cs = new ConstraintSet();
cs.clone(layout);
cs.connect(R.id.layout, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
cs.connect(R.id.layout, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
cs.connect(R.id.layout, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
cs.connect(R.id.layout, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
cs.applyTo(layout);
You will need to change the height and width of the layout in card.xml to 0dp. match_parent is never appropriate in ConstraintLayout.
This is a pictorial description of what is happening:
On a related note, you should consider using the merge facility to avoid nested ConstraintLayouts as other have mentioned.

Related

Android Studio: Calling multiple Canvas in ScrollView to display 1 drawing (canvas) after another

I'm writing a code for a school project where one has to load a data file (CSV file, text file, etc) and from the obtained data, the app will pass the data to a custom draw View and the onDraw method will draw/plot a graph based on the data.
My goal is for the app to display 2 graphs, one after the other (stacked). The first set of data is loaded and the 1st graph is drawn. The loaded data is then used for a different calculation in a different method. The custom draw View is then called again with the new data to draw the 2nd graph.
When I run the app, both charts are drawn but because the x and y-axis' of the graph are coded to be drawn at certain fixed pixels, the 2nd graph is drawn over the first one and therefore only the 2nd graph is visible.
Is there any way I can draw the 2 graphs so that it does not overlap and instead appears to be stacked in ScrollView?
My code is shown below but I've gotten rid of calculations that I think aren't very important. Any help and pointers would be very much appreciated!
MainActivity.java:
#Override
protected void onActivityResult(......) {
super.onActivityResult(......);
switch (1) {
case 1:
Graph graph = this.findViewById(R.id.graph1);
graph.setData(data); // the loaded data is passed to Graph View
Graph drawGraph2 = this.findViewById(R.id.graph2);
graph2.setData(this.newCalculate(data));
break;
}
}
Graph.java
public class Graph extends View {
private Paint mPaint = new Paint();
private final int zero = 700; // mark the 0 line of graph at 700 pixels
public void setData(data){
......
}
public Graph(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
#Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
plotUnit(canvas); // plot points on graph
axisLabel(canvas); // label axis
axisLine(canvas); // draw axis
xyAxisMarker(canvas); // mark axis
}
private void plotUnit(Canvas canvas) {
......
// Due to data having negative values, the graph is inverted and the 0 starts
// of the graph is defined at 700 pixels (private final int zero)
}
private void axisLabel(Canvas canvas) {
......
}
private void axisLine(Canvas canvas, int inset) {
......
}
private void xyAxisMarker(Canvas canvas) {
......
}
Update
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/loadbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Data File" />
<firstapp.drawtwograph.Graph
android:id="#+id/graph1"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<firstapp.drawtwograph.Graph
android:id="#+id/graph2"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
You cannot have two views' heights match parent height inside of a LinearLayout with vertical orientation. It is not possible because heights of these views must be equal to the parent height but at the same time, they must be ordered one after the other resulting in double of parent's height.
If you imagine parent's height as 10dp then each of the Graph views must be 10dp as well which means parent's height must be 20dp, not 10dp. That is going to cycle forever so the Android does a simple thing: views that are going below the first child view with android:layout_height="match_parent" will have height 0dp or if their height is fixed they will be drawn outside of the layout and will not be visible.
Example
Screenshot from Design tab of layout editor in Android Studio IDE.
Here you can see:
red view as a parent linear layout;
purple view as a first child with height matching it's parent height;
outlined view that is drawn outside of the layout because it is pushed out by the first child with android:layout_height="match_parent";
there is one more view that is crushed to 0 height and thus not visible. You can see it down in the XML code.
XML code of this sample:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/holo_red_light">
<LinearLayout
android:background="#color/colorPrimary"
android:layout_width="60dp"
android:layout_height="match_parent" <!-- this view's height is a problem -->
android:orientation="vertical"/>
<LinearLayout
android:background="#android:color/holo_green_dark"
android:layout_width="120dp"
android:layout_height="match_parent" <!-- height is not fixed, then it will be 0 -->
android:orientation="vertical"/>
<LinearLayout
android:background="#android:color/holo_green_light"
android:layout_width="120dp"
android:layout_height="40dp" <!-- height is fixed, it is outlined outside of a layout -->
android:orientation="vertical"/>
</LinearLayout>
How to fix the issue?
Set fixed height. As a test try to define a fixed height, e.g. 100dp;
Redesign your layout. Use RelativeLayout or ConstraintLayout to position views relative to each other so that they are always visible no matter what the screen size, ratio, density is.
Example of how to fix
I personally prefer ConstraintLayout as it is very powerful in terms of positioning and adaptation.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/loadbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Data File"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<firstapp.drawtwograph.Graph
android:id="#+id/graph1"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/graph2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/loadbutton" />
<firstapp.drawtwograph.Graph
android:id="#+id/graph2"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/graph1" />
</androidx.constraintlayout.widget.ConstraintLayout>
The result is (I used two buttons instead of Graph views):
Hints:
If you want to use ScrollView then setting fixed height or defining height at runtime will be required.
Get rid of private final int zero = 700; // mark the 0 line of graph at 700 pixels. Do not use pixel values directly as it will lead to error-prone UI. It will be the case of "work on my phone, does not work the other". Use view's height as the 0 line.

Align reclyerview childers from bottom

I'm trying to make chart with reclyerview. The issue that i'm having is i can't seem to make child items align from bottom, instead of the top of the reclyerview.
I have tried, reverseLayout and stackFromEnd
I have searched left and right. Still have no idea why does this happen.
Can anyone please help me.
Any helpfull ideas would be appreciated.
Layout managers align items to top-start by default because it's the most stable in case RecyclerView has height of wrap_content which could cause some layout issues.
If your RecyclerView has fixed size you can try to extend layout manager you're using to force it to lay out items on bottom:
// inside your activity or fragment where you're initializing RecyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) {
#Override
public void layoutDecoratedWithMargins(#NonNull View child, int left, int top, int right, int bottom) {
// calculate available space in recyclerView
int offset = getHeight() - getPaddingTop() - getPaddingBottom();
// subtract height of item being laid out
offset -= bottom - top;
// lay out item lower than usual
super.layoutDecoratedWithMargins(child, left, top + offset, right, bottom + offset);
}
});
I kinda figured it out.
As i have static height, i created another wrapper around child, that was alligned to bottom, and the parrent set as specific height.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="80dp"
android:layout_height="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginRight="8dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<TextView
android:id="#+id/tv_time"
style="#style/BoldWhiteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7.5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

How to make square imageButton?

Is there a file xml in the android studio:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<imageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent"
android:scaleType="fitXY" />
<imageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent"
android:scaleType="fitXY" />
</LinearLayout>
I need to have the full width of 2 square buttons, how can this be done?
You can make your custom view here by Extending ImageButton View.In onMeasure(), you can set the height of the ImageButton equal to its width. you can use this class in your XML as well.
Here is the snippet:-
public class SquareImageButton extends ImageButton {
public SquareImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width=getMeasuredWidth();
setMeasuredDimension(width,width);
}
}
Well firstly you'll need a square image to put into the imageButton. Then you'll set AdjustViewBounds to true. and then set the scaletype to FitXY. I use linear layouts and use the weights and views to adjust the size and position. Good for scaling and the imageButton will square. This is under my ImageButton in the xml:
android:scaleType="fitXY"
android:layout_weight="1"
android:adjustViewBounds="true"
1. Programmatically get the width of the display.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
2. Finally, use this width value to make Square Button by setting its new LayoutParams(width, width).
ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
ImageButton imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width);
imageButton1.setLayoutParams(params);
imageButton2.setLayoutParams(params);
3. Use ScrollView as a container of your LinearLayout to make your layout scroll-able to show both button on UI and update XML as below:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_light"
android:scaleType="fitXY" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_light"
android:scaleType="fitXY" />
</LinearLayout>
</ScrollView>
OUTPUT:
FYI, ImageButton1 is RED colored and ImageButton2 is BLUE colored. Both are in Square shape.
Hope this will help~

Android: Drawing rectangular imageViews over existing image

I am making an Android app and attempting to programatically draw some clickable, rectangular blocks with text on one of two custom views contained in a ScrollLayout. The XML for my ScrollLayout looks like this:
<ScrollView
android:layout_width="match_parent"
android:id="#+id/calendarGridContainerView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/employeeSpinnerContainer"
android:paddingBottom="72dp"
android:layout_height="match_parent"
android:overScrollMode="never"
android:fillViewport="true"
android:elevation="1dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:id="#+id/schedule_view_container"
android:layout_height="wrap_content">
<FrameLayout
android:layout_height="wrap_content"
android:id="#+id/schedule_time_frame"
android:layout_width="100dp"
android:layout_alignParentTop="false"
android:layout_weight="0.5">
<include
layout="#layout/schedule_time_view"
android:layout_height="wrap_content"
android:layout_width="100dp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/schedule_grid_frame"
android:minHeight="179dp"
android:layout_toRightOf="#+id/schedule_time_frame"
android:layout_centerHorizontal="false"
android:layout_weight="8">
<include
layout="#layout/schedule_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
</LinearLayout>
</ScrollView>
Note that the layout called scheduleGridLayout is where I am trying to draw these rectangles. It has the following XML:
<?xml version="1.0" encoding="utf-8"?>
<(package).ScheduleGridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:scheduleGridLayout="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
</(package).ScheduleGridLayout>
(When copying the above code, I removed the package name.)
In the constructor for ScheduleGridLayout.java, I call this.setWillNotDraw(false);. Before drawing the rectangles, I draw some horizontal gridlines on ScheduleGridLayout through its overridden onDraw() method:
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.LTGRAY);
final int width = getWidth();
final int rowHeight = ScheduleTimeLayout.SCHEDULE_VIEW_ROW_HEIGHT + ScheduleTimeLayout.TIME_LABEL_HEIGHT;
for(int i = 0; i < ScheduleTimeLayout.TIME_SEGMENT_COUNT + 1; i++){
if ((i % (int)(1.0/ScheduleTimeLayout.SEGMENT_SIZE)) == 0){
paint.setColor(Color.GRAY);
paint.setStrokeWidth((float)4.0);
}
else{
paint.setColor(Color.LTGRAY);
paint.setStrokeWidth((float)2.0);
}
int y = 178 + (int)(i*(rowHeight + 1) - rowHeight/2.0);
canvas.drawLine(0, y, width, y, paint);
}
Since I want the rectangles to be clickable, I have to add them as some sort of views rather than simply drawing their images onto the canvas. I've tried a few different ways to draw the rectangles-- currently I try to do it in onFinishInflate() in ScheduleGridLayout.java, like so:
Bitmap bmp = Bitmap.createBitmap(60,200,Bitmap.Config.RGB_565);
Bitmap tempBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
paint = new Paint();
paint.setColor(Color.RED);
tempCanvas.drawBitmap(bmp, 0, 0, null);
tempCanvas.drawRoundRect(new RectF(0,0,600,600), 2, 2, paint);
ImageView testBlock = new ImageView(getContext());
testBlock.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
addView(testBlock);
However, there the block does not appear when I do this.
In a separate attempt, I tried creating an XML layout file for the schedule block:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<com.dalc.dalccalendar.ScheduleBlock
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:scheduleGridLayout="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/schedule_block">
</com.dalc.dalccalendar.ScheduleBlock>
In ScheduleBlock.java, I override onMeasure() to set the size of the block using setMeasuredDimension, and I override onDraw() with the following code:
super.onDraw(canvas);
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
canvas.drawPaint(paint);
String bgColor = "#FF0000";
setBackgroundColor(Color.parseColor(bgColor));
I noticed that red rectangle does appear in the XML file's preview, but not when I try to add the block in ScheduleGridLayout:
FrameLayout blockFrame = (FrameLayout)LayoutInflater.from(getContext()).inflate(R.layout.schedule_block_container, this, false);
testBlock.setElevation(4);
addView(blockFrame);
Not only does the rectangle not appear when I run the app, but I've found that the onDraw() method for ScheduleBlock is not called. What am I doing wrong? How can I make these blocks appear inside my scrollview on top of the gridlines and be interactable with clicks? I'd prefer to be able to do with with custom views if possible, since each block needs to be associated with specific data.

Android: How to set horizontal scroll view image margins programmatically

I am creating a HorizontalScrollView in xml and inserting the images programmatically, but I am having trouble with margins. How do I set the horizontal margins for images in the HorizontalScrollView? Below is my code:
xml:
<HorizontalScrollView
android:id="#+id/HorizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp">
<LinearLayout
android:id="#+id/innerLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--Image Views here -->
</LinearLayout>
</HorizontalScrollView>
java:
ImageView imageView = new ImageView(getActivity());
scrollLinearLayout.addView(imageView);
//set height and other image properties.
Use this function to set runtime margin to view
public static void setMargins (View v, int left, int top, int right, int bottom) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(left, top, right, bottom);
v.requestLayout();
}
}

Categories

Resources