I'm trying to resize a layout over time, what I'm stuck on is trying to loop the decrease/increase in size, I've currently got it working to resize on a screen touch.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
// Gets linearlayout
layout = (RelativeLayout)findViewById(R.id.relativeOne);
params = layout.getLayoutParams();
params.height = width - width/2;
params.width = width - width/2;
sizeAfter = width;
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
sizeAfter = sizeAfter - 2;
params.height = sizeAfter;
params.width = sizeAfter;
return super.onTouchEvent(event);
}
Currently this works fine, however how would I go about implementing maybe a timer to loop after a set amount of time gradually decreasing the layouts size.
Related
I have a problem in relation to the relative location of Buttons in different devices. I have done a short application to show it. The Button, in relation to a red point, behaves as desire when dealing with wide measures (coordinate x). However, I don’t know why the same Button is progressively scrolling down from the red point (coordinate y) as I choose a smaller device. I choose the right size of the image for the different devices and for both, wide and height, I also choose the relative measure of the screen:
rel_btn.width = 4*width/100;
rel_btn.height = 7*height/100;
I show you the code and the resulting screen of the xlarge screen in case someone can help me:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//width and height of the screen
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
windowManager.getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels;
int height = metrics.heightPixels;
RelativeLayout layout = (RelativeLayout) findViewById(R.id.test); // id del XML
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rel_btn.width = 4*width/100; rel_btn.height = 7*height/100;
// x, y of the BUTTON
rel_btn.leftMargin = 15*width/100; rel_btn.topMargin = 66*height/100;
Button btnTag = new Button(this);
btnTag.setLayoutParams(rel_btn);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setId(0); // writes the ID
btnTag.setOnClickListener(prueba);
layout.addView(btnTag);
}
private View.OnClickListener prueba = new View.OnClickListener() {
#Override
public void onClick(View view) {
//width of the screen
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
windowManager.getDefaultDisplay().getMetrics(metrics);
}
int width = metrics.widthPixels;
Button button = (Button) view;
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(Math.round((float) (0.6*width/100)), Color.BLUE);
drawable.setColor(Color.TRANSPARENT);
button.setBackgroundDrawable(drawable);
}
};
}
I've tried this code to change randomly the button position. But i'ts just dimensions, on the first click the button change position but on the other click not. Why?? some help? Thanks
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button) findViewById(R.id.run_button);
RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn
.getLayoutParams();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;
Random r = new Random();
absParams.width = r.nextInt(width);
absParams.height = r.nextInt(height);
btn.setLayoutParams(absParams);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Random r = new Random();
btn.getX();
btn.getY();
btn.setX(r.nextFloat());
btn.setY(r.nextFloat());
}
});
I'm pretty sure the touch positions do not change when you call setX() and setY(). They stay at the original spot. You have to actually change the layout parameters so the layout is informed knows that your view is at that spot.
Like so:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //The WRAP_CONTENT parameters can be replaced by an absolute width and height or the FILL_PARENT option)
params.leftMargin = 50; //Your X coordinate
params.topMargin = 60; //Your Y coordinate
childView.setLayoutParams(params);
Taken from: Android - Use of view.setX() and setY in api 8
How I can get screen width and height for use this value on device with android version 2.2(API lv8).
I use this code:
public static Point Maximum(Context context)
{
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Point temp=new Point();
return temp;
}
And when i want check by print Max point to textview:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt= (TextView)findViewById(R.id.text1);
txt.setText(Math.Maximum(this).toString());
}
Point is (0,0). What problem?
Try this code. But this API is deprecated in the new SDK versions you can use this.
Display display = activity.getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Get Device Width and Height, return as point. Hope u like this.
public static Point Maximum(){
Display display = activity.getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Point temp=new Point();
temp.set(width, height);
return temp;
}
Try this code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Try this for getting your screen width. Similarly you can get screen height or can customize the same for returning both in a single function.
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
private int getScreenWidth() {
int width = 400;
int height = 600; // just intialising it to default value in case if there is any problem getting screen width
WindowManager wm = (WindowManager) activity // activity is the context if you are using this method in adapter. Otherwise in Activity you can simply ignore this
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if (android.os.Build.VERSION.SDK_INT >= 13) {
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
} else {
width = display.getWidth();
height= display.getHeight();
}
return width;//or height
}
i want ImageView's to look same on different screen sizes. To make it easier for you to understand how it should look there are some image:
Biggest problem is that my imageViews are created programmatically. ImageViews layout and size are set by this code
LinearLayout LinearLayoutas = (LinearLayout)findViewById(R.id.LinearLayout);
ImageView ImageViewas = new ImageView(this);
ImageViewas.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayoutas.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ImageViewas.setLayoutParams(params);
ImageViewas.getLayoutParams().height = 650;
ImageViewas.getLayoutParams().width = 950;
params.setMargins(0, 50, 0, 0);
Any ideas how to change this code that my app can look same on diffrent screen sizes?
Well, you can retrieve device resolution and set imageView width and height. Here is the solution.
private class ScreenResolution {
int width;
int height;
public ScreenResolution(int width, int height) {
this.width = width;
this.height = height;
}
}
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
ScreenResolution deviceDimensions() {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
// getsize() is available from API 13
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return new ScreenResolution(size.x, size.y);
}
else {
Display display = getWindowManager().getDefaultDisplay();
// getWidth() & getHeight() are deprecated
return new ScreenResolution(display.getWidth(), display.getHeight());
}
}
..................
LinearLayout LinearLayoutas = (LinearLayout)findViewById(R.id.LinearLayout);
ImageView ImageViewas = new ImageView(this);
ImageViewas.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayoutas.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ScreenResolution screenRes = deviceDimensions();
ImageViewas.setLayoutParams(params);
ImageViewas.getLayoutParams().height = screenRes.height;
ImageViewas.getLayoutParams().width = screenRes.width;
params.setMargins(0, 50, 0, 0);
Also when the device orientation will be changed, the width and height should be swapped. You can do that in onConfigurationChanged:
#Override
public void onConfigurationChanged(Configuration newConfiguration) {
super.onConfigurationChanged(newConfiguration);
// swap device width and height here and re-assign
}
using a dialog as a window I tried to set its layout as
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.x = 120;
lp.y = 120;
lp.width = 800;
lp.height = 450;
Dialog dialog = new Dialog(cxt);
dialog.getWindow().setAttributes(lp);
dialog.setContentView(vFirst);
dialog.show();
where
View vFirst = inflater.inflate(R.layout.popup, container, false);
which is inside a button click,but it is not setting it.
I think you should use this
dialog.getWindow().setLayout(800, 450);
This will set the dialog window size.
public class Myclass extends Dialog
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.FILL_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/dialogWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
// contents here
</LinearLayout>
According to documentation: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html,
X position for this window. With the default gravity it is ignored. When using LEFT or START or RIGHT or END it provides an offset from the given edge.
Y position for this window. With the default gravity it is ignored. When using TOP or BOTTOM it provides an offset from the given edge.
So to make x and y work you should set Gravity attribute before. For example:
lp.gravity = Gravity.TOP;
lp.gravity = Gravity.LEFT;
lp.y = 120;
lp.x = 120;
//dialog width and height are set according to screen, you can take you want
Dialog myDialog = new Dialog(context, R.style.CustomTheme);
myDialog.setContentView(R.layout.share_post_dialog);
int[] widthHeight = getDeviceWidthAndHeight(context);
int dialogHeight = widthHeight[1] - widthHeight[1] / 4;
int dialogWidth = widthHeight[0] - widthHeight[0] / 5;
WindowManager.LayoutParams params = myDialog.getWindow().getAttributes();
params.width= dialogWidth;
params.height = dialogHeight;
myDialog.getWindow().setAttributes((WindowManager.LayoutParams) params);
myDialog.show();
//This is used to get height width of screen
#SuppressWarnings("deprecation")
public static int[] getDeviceWidthAndHeight(Context context) {
int widthHeght[] = new int[2];
int measuredWidth = 0;
int measuredHeight = 0;
WindowManager w = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
widthHeght[0] = measuredWidth;
widthHeght[1] = measuredHeight;
} else {
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
widthHeght[0] = measuredWidth;
widthHeght[1] = measuredHeight;
}
return widthHeght;
}