I to want get and set margin of my LinearLayout from java. I do not want set like right,left, top, bottom etc. I just want set simple margin from all side. I know I can do it via XML but I do know how can I do it via java.
I have done via xml is like below
android:layout_margin="20dp"
Anyone can please suggest me how can I do it via java?
To set margin to the view you can use this code:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
To get the margin of view use this code
View view = findViewById(...) //or however you need it
LayoutParams lp = (LayoutParams) view.getLayoutParams();
// margins are accessible via
lp.leftMargin;
lp.rightMargin;
lp.topMargin;
lp.bottomMargin;
// perhaps ViewGroup.MarginLayoutParams will work for you. It's a base class for //other LayoutParams.
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
Note: Sorry for using snippet...
It will work like a charm...
You can use following code to do so.
LinearLayoutview ll= findViewById(R.id.linearLayout); //or however you need it
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
margins are accessible via
lp.leftMargin;
lp.rightMargin;
lp.topMargin;
lp.bottomMargin;
Now you can use the code
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.setMargins(20,20,20,20);
ll.setLayoutParams(params);
Need use type of: MarginLayoutParams
Try this:
MarginLayoutParams params = (MarginLayoutParams) vector8.getLayoutParams();
params.width = 200; params.leftMargin = 100; params.topMargin = 200;
Code Example for MarginLayoutParams:
http://www.codota.com/android/classes/android.view.ViewGroup.MarginLayoutParams
Related
Im trying to create LinearLayout programmaticaly and I would want to allow user to put number range.
Now it looks like that:
But when I try to enter more digits eg. 100, 101 or 3,50 it dissapears.
I guess there is not enough space for it to be shown, but I can't figure out what is wrong. Generally I don't want to move + and - buttons when the user enters some values so I guess it should be hardcoded. There would be up to 5-6 digits only, so I need space just for it, but as I said, I can't find the place, where I can change it as my changes eiter move entire layout or doesn't do anything.
Below is my code:
LinearLayout horizontalLayout = new LinearLayout(mContext);
LinearLayout titleLayout = new LinearLayout(mContext);
LinearLayout countLayout = new LinearLayout(mContext);
ImageButton buttonAdd = new ImageButton(mContext);
ImageButton buttonSub = new ImageButton(mContext);
TextView titleTextView = new TextView(mContext);
EditText countEditText = new EditText(mContext);
final int[] currentCount = {defaultValue};
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams linearLayout = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f);
LinearLayout.LayoutParams utilParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setLayoutParams(params);
utilParams.gravity = Gravity.CENTER_VERTICAL;
titleLayout.setOrientation(LinearLayout.HORIZONTAL);
titleLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
titleLayout.setLayoutParams(linearLayout);
countLayout.setOrientation(LinearLayout.HORIZONTAL);
countLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
countLayout.setLayoutParams(linearLayout);
utilParams.setMargins(0,0,pxFromDp(mContext, 16f),0);
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
titleTextView.setText(title);
titleTextView.setLayoutParams(utilParams);
titleLayout.addView(titleTextView);
utilParams.setMargins(pxFromDp(mContext, 16f),0,pxFromDp(mContext, 16f),0);
buttonSub.setImageResource(R.drawable.ic_remove);
buttonSub.setLayoutParams(utilParams);
buttonSub.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
buttonSub.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
countLayout.addView(buttonSub);
countEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
countEditText.setText(String.valueOf(defaultValue));
countEditText.setLayoutParams(linearLayout);
countEditText.setGravity(Gravity.CENTER);
countLayout.addView(countEditText);
buttonAdd.setImageResource(R.drawable.ic_add_24);
buttonAdd.setLayoutParams(utilParams);
buttonAdd.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
buttonAdd.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
countLayout.addView(buttonAdd);
horizontalLayout.addView(titleLayout);
horizontalLayout.addView(countLayout);
What you can do is to create separate LinearLayout.LayoutParams for the edittext and add addTextChangedListener to that edit text and on onTextChanged
write switch statement to increase the weight of the edittext on increase of length of the input number.
I am trying to program the margin of the ImageView inside the ConstraniLayout2 with this code
ConstraintLayout.LayoutParams newLayoutParams = (ConstraintLayout.LayoutParams)
imageView.getLayoutParams();
newLayoutParams.topMargin = 140;
newLayoutParams.leftMargin = 400;
newLayoutParams.rightMargin = 0;
newLayoutParams.bottomMargin = 0;
imageView.setLayoutParams(newLayoutParams);
With this code I program the site of the imageView but of the entire screen of the smartphone.
How can I program the imageView to switch places with the constrainLayout2 references?
You can also try to set the margin using ConstraintSet.
Below is a snippet you can try
val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayoutId)
val marginTop = context.getDimension(10f)
constraintSet.setMargin(R.id. imageView, ConstraintSet.TOP, marginTop)
constraintSet.applyTo(constraintLayoutId)
fun Context.getDimension(value: Float): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, value, this.resources.displayMetrics).toInt()
}
I'm currently creating TextView like that:
LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.ll1);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int l=0; l<4; l++)
{
pairs[l] = new TextView(context);
pairs[l].setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
pairs[l].setLayoutParams(lp);
pairs[l].setId(l);
pairs[l].setText("asd");
myLayout.addView(pairs[l]);
}
Now I want set this attribute to all of this TextView:
FontFamily: cursive
SetTextSize not sp but dp (RESOLVED)
SetGravity: central_horizontal (RESOLVED)
I couldn't find a way for set those attribute when I create a TextView programatically, How can I do that?
Text size in dp can be set using setTextSize(TypedValue.COMPLEX_UNIT_DIP, <float>) - see documentation [here](https://developer.android.com/reference/android/widget/TextView.html#setTextSize(int, float)).
As for the font family, I'm afraid I don't know - hopefully someone else can help you with this :)
In order to change the font family of a TextView use setTypeface
for instance:
Typeface tf = Typeface.create("cursive", Typeface.NORMAL);
for(int l=0; l<4; l++)
{
pairs[l] = new TextView(context);
pairs[l].setTypeface(tf);
...
}
Also, this may interest: How to change fontFamily of TextView in Android
You can set the font using Typeface object.
TextView tv = new TextView(context);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/filename.ttf");
tv.setTypeface(face);
Put the font in the assets folder of your project.
I'm writing an image gallery with horizontal scrolling. Images must be added programatically.
I use a custom horizontal scroll view to process and add images as in the following code:
public void setViewList(Integer linearLayoutId, Integer[] imageIdList,
Activity activity) {
displayMetrics = ImageUtils.getDisplayMetric(activity);
LinearLayout ll = (LinearLayout) findViewById(linearLayoutId);
for (Integer imgId : imageIdList) {
ImageView imageView = new ImageView(getContext());
imageView.setImageResource(imgId);
imageView.setScaleType(ImageView.ScaleType.MATRIX);
Integer[] displayMetrics = ImageUtils.getDisplayMetric(activity);
ImageUtils.scaleImage(imageView, displayMetrics[0], displayMetrics[1]);
Integer[] dstDimension = ImageUtils.createDimension();
ImageUtils.getImageSize(imageView, dstDimension);
getImageSizeList().add(dstDimension);
ll.addView(imageView);
}
}
As you can see I scale an image with use the following method (call ImageUtils.scaleImage(imageView, displayMetrics[0], displayMetrics[1])):
public static void scaleImage(ImageView imView, int screenWidth, int screenHeight) {
Drawable temp = imView.getDrawable();
Bitmap imBitmap = ((BitmapDrawable)temp).getBitmap();
int imWidth = imBitmap.getWidth();
int imHeight = imBitmap.getHeight();
float xScale = ((float) screenWidth) / imWidth;
float yScale = ((float) screenHeight) / imHeight;
float scale = xScale <= yScale ? xScale : yScale;
Matrix scaleMatrix = new Matrix();
scaleMatrix.postScale(scale, scale);
Bitmap scBitmap = Bitmap.createBitmap(imBitmap, 0, 0, imWidth, imHeight, scaleMatrix, true);
BitmapDrawable scDrBitmap = new BitmapDrawable(imView.getResources(), scBitmap);
imView.setImageDrawable(scDrBitmap);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
layoutParams.gravity = Gravity.CENTER;
imView.setLayoutParams(layoutParams);
}
My main.xml layout is pretty simple:
<?xml version="1.0" encoding="utf-8"?>
<android.lessons.custom_horizontal_scroll.CustomHorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="horizontal"
android:id="#+id/mainLayout"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.lessons.custom_horizontal_scroll.CustomHorizontalScrollView>
Where the android.lessons.custom_horizontal_scroll.CustomHorizontalScrollView is implementation of a simple custom HorizontalScrollView.
For testing I use Samsung Galaxy S3 and images with the following resolution: (1) 1290*990 (2) 1221*900. What it looks like:
In many cases everything is displayed fine but from time to time I get the wrong result: the first image divides a screen with the following one at app start time and I don't have any idea why it happens.
Thanks for help.
I think you should use ViewPager from the support library v4 rather than this custom horizontal scroll view in this case. It's designed for that purpose.
Here is how to use it https://developer.android.com/training/animation/screen-slide.html.
The problem was resolved with use gravity = Gravity.FILL_HORIZONTAL | Gravity.CENTER_VERTICAL, that i set programmatically on a image view (method ImageUtils.scaleImage).
I am trying to add multiple components into linear layout programatically. Here are the codes:
private View createCalloutView(Graphic graphic) {
LinearLayout ret = new LinearLayout(this);
ret.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TextView reportContent = new TextView(this);
reportContent.setText(eventName + "\n" + eventBy + "\n" + eventAddress + "\n" + eventDesc
+ "\n" + eventDate + "\n" + eventTime);
reportContent.setTextColor(Color.BLACK);
reportContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
reportContent.setPadding(1, 0, 1, 0);
Button viewDtlEventBtn = new Button(this);
viewDtlEventBtn.setText("View details");
viewDtlEventBtn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ret.addView(reportContent);
ret.addView(viewDtlEventBtn);
return ret;
}
With these codes, I only manged to see the textview and my button is missing. Any ideas? Thanks in advance.
that depends on how do you want to arrange the items in the LinearLayout. If you want to arrange the button next to the TextView, then, probably, the button width should be WRAP_CONTENT instead of FILL_PARENT. If you want to show the button under the TextView, then, your LinearLayout should have vertical as orientation (default is horizontal). Imo, the easiest way is to have your layout defined in a xml file. At least you can see the output at compile time, and use an inflater to retrieve the View's object at run time
The default orientation of linear layout is horizontal. You need to set the orientation first.
LinearLayout ret = new LinearLayout(this);
ret.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ret.setOrientation(LinearLayout.VERTICAL);
This will solve your problem of missing button.
You have forgot to set Layout Orientation for Linear Layout, just set it as below:
ret.setOrientation(LinearLayout.VERTICAL);