int width=30;
int height=30;
LottieAnimationView lottieanimation=findViewById(R.id.ltanms);
lottieanimation.setLayoutParams(new RelativeLayout.LayoutParams(width,height));
But this isn't working for me.
You can try this:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) LottieAnimationView.getLayoutParams();
params.width = width;
params.height = height;
it worked for me.
Related
I have an overlay and a text field in it. But when I want to enter text in the field, the virtual keyboard is not displayed.
I have tried all the flags (FLAG_LOCAL_FOCUS_MODE, FLAG_ALT_FOCUSABLE_IM, FLAG_NOT_FOCUSABLE), but it did not bring any result.
Here is my code:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
width - 100,
height - 100,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
//WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
//WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.RIGHT;
params.x = 50;
params.y = 50;
ConstraintLayout rootView = (ConstraintLayout) LayoutInflater.from(this).inflate(R.layout.overlay_layout, null);
windowManager.addView(rootView, params);
How to fix it?
I am new to android development and I am trying to add margins to my view but I have not been able to get it to work.
Here's my code:
ConstraintLayout layout = new ConstraintLayout(this);
final float scale = getResources().getDisplayMetrics().density;
layout.setMinHeight((int)(100*scale));
layout.setMaxHeight((int)(100*scale));
CircleImageView icon = new CircleImageView(this);
icon.setImageResource(image);
icon.setBorderWidth(3);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)(90*scale), (int)(90*scale));
icon.setLayoutParams(layoutParams);
ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(icon.getLayoutParams());
marginParams.setMargins(100, 0, 0, 0);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(marginParams);
icon.setLayoutParams(params);
layout.addView(icon);
Why isn't this working? Thanks!
You should do like this:
LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
param.setMargins(left, top, right, bottom);
view.setLayoutParams(param);
After you have created the icon, you should first generate an id for it:
icon.setId(View.generateViewId());
Then, remove the setMargins line and add these lines below layout.addView(icon):
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.constrainWidth(icon.getId(), (int)(90*scale));
set.constrainHeight(icon.getId(), (int)(90*scale));
set.connect(icon.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set.connect(icon.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 100);
set.applyTo(layout);
I haven't tested it but it should be a step in the right direction.
Let me know how it goes.
Hi I wrote an app using drag'n drop option, Im using android studio 3 & sdk 26
When I get to the touch event I get the error message:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
Thats the code:
private View.OnTouchListener OnTouchListener() {
return new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
final int x = (int) motionEvent.getRawX();
final int y = (int) motionEvent.getRawY();
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)
view.getLayoutParams();
xDelta = x - lParams.leftMargin;
yDelta = y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = x - xDelta;
layoutParams.topMargin = y - yDelta;
layoutParams.rightMargin = 0;
layoutParams.bottomMargin = 0;
view.setLayoutParams(layoutParams);
break;
}
mainlayout.invalidate();
return true;
}
};
}
I used that code before and it worked, I cant understand whats wrong here.
Based on the error message the issue has nothing to do with the drag and drop, it looks like this part of code:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)
view.getLayoutParams();
and
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
should be changed to :
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams)
view.getLayoutParams();
and
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view
.getLayoutParams();
You didn't put your xml, but I am supposing that parent of "view" is a LinearLayout right?
i wanna add a view with a 100% width and X%(e.g: lets make it 40%) to the parent view
i have this:
private RelativeLayout getParentView(){
return (RelativeLayout) MyActivity.this.getWindow().getDecorView().getRootView();
}
i should cast parent view to LinearLayout or RelativeLayout can achieve this ?
and how to set 'myView' width and height in percentage programmatically ?
Thanks
You can do this by simply getting the screen width
public static int getScreenWidth(Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
}
Multiply that by the percentage and give the parameters to view like this:
//For 40% height according to the screen width.
view.getLayoutParams().height = (int) (getScreenWidth(mContext) * 0.40);
Hope this helps.
private static void updateDimen(Activity activity){
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
screenHeight = displayMetrics.heightPixels;
screenWidth = displayMetrics.widthPixels;
}
Use above code to get screen height and screen width.
Then get the instance of your Layout, get its LayoutParams instance and update the dimension
params.height = screenHeight * 0.6;
params.width = screenWidth * 0.6;
In Android, I create an ImageView in the Java code and set its layout parameters: width, height and top margin before adding it to the main layout (RelativeLayout). The width and height are applied successfully, but the margin doesn't have any affect on the image view position. The actual top margin stays 0.
How to apply the top margin to views? The code is below.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initClouds();
}
private void initClouds() {
addCloud(R.drawable.cloud1, R.dimen.cloud1_top_margin);
addCloud(R.drawable.cloud2, R.dimen.cloud2_top_margin);
}
private void addCloud(int imageResId, int topMarginResId) {
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView cloud = new ImageView(this);
int height = (int) getResources().getDimension(R.dimen.cloud_height);
int width = (int) getResources().getDimension(R.dimen.cloud_width);
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(width, height);
params.topMargin = (int) getResources().getDimension(topMarginResId);
cloud.setImageResource(imageResId);
mainLayout.addView(cloud, params);
}
}
For setting the margin for a view inside RelativeLayout you should use RelativeLayout.LayoutParams . Change your code like this ,
private void addCloud(int imageResId, int topMarginResId) {
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView cloud = new ImageView(this);
int height = (int) getResources().getDimension(R.dimen.cloud_height);
int width = (int) getResources().getDimension(R.dimen.cloud_width);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(width, height);
param.topMargin = (int) getResources().getDimension(topMarginResId);
cloud.setImageResource(imageResId);
mainLayout.addView(cloud, param);
}