Get screen sizes & set them in carousel view android - android

Here you can see my carousel layout xml file.
<com.touchmenotapps.carousel.simple.HorizontalCarouselLayout
android:id="#+id/carousel_layout_event"
android:layout_width="600dp"
android:layout_height="500dp"
android:layout_above="#+id/relativeLayout1"
android:layout_alignTop="#+id/carousel_layout" >
</com.touchmenotapps.carousel.simple.HorizontalCarouselLayout>
I want to set the running android simulator screen width & height to the android:layout_width & android:layout_height...
can anyone help me. thanks

HorizontalCarouselLayout hcl=(HorizontalCarouselLayout)findViewById(R.id.carousel_layout_event);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
hcl.setLayoutParams(new LayoutParams(width,height));

Firs you get the divice size i.e.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int divWidth= size.x;
int divHeight = size.y;
Create a function to set the layout params. i.e.
public void setWidthHeight(View v, int width, int height){
LayoutParams lp;
lp = v.getLayoutParams();
lp.width = width;
lp.height = height;
v.setLayoutParams(lp);
}
then call this function as,
setWidthHeight(yourView,divWidth,divHeight);

Related

Changing text size according to the change in the button size

I have a toggle button that changes it's size as following :
protected int[] calculateCardWidth(int[] rowsCols) {
//Getting the screen size.
Display display = this.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
//cardWidHigh 0->width , 1 ->height.
cardWidHigh[0] = (int) (width / (rowsCols[1] + .25));
cardWidHigh[1] = (int) (height / (rowsCols[0] + 2));
return cardWidHigh;
}
I want to be able to change the text size according to the change in button size (it's width and height )dynamically, what is the best approach inorder to achieve this ?
You change the values of its LayoutParams, the parent will use the new values to rerender the screen at new positions:
LayoutParams p = cardView.getLayoutParams();
p.width = cardWidth[0];
p.height = cardWidth[1];
cardView.setLayoutParams(p);
cardView.getViewParent().invalidate();

How do i force an embedded android fragment to a fixed width

Set i have a fragment CustomSwitchFragment, that i want to use as followed:
<fragment android:name="m6world.test_toggle_yes_no.CustomSwitchFragment"
android:id="#+id/custom_switch_fragment"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="horizontal" />
i don't want to have to input 80dp, i would like to be able to put wrap_content and the fragment will be set whatever width i used when created it.
Basically i don`t want to have to tell people who are gonna use my fragment, to set it to 80dp specifically.
I guess you can use something like this
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width * 70 / 100;
params.height = height * 30 / 100;
getWindow().setAttributes(params);
getWindow().getAttributes().verticalMargin = 0.2F;

change the width & height of ImageButton at runtime in android

I have used this code to make a ImageButton & after that i want to change the size of it at run time. Both the codes the written below,
<ImageButton
android:id="#+id/btn_new"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/untitled3"
/>
public void scaler1() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
btn_new.setMinimumWidth(width/4);
btn_new.setMinimumHeight(height/6);
}
But it is not working please give any solution of it.
This is the actual code i'm using
public void scaler1() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
edit_screen.setWidth(width);
edit_screen.setHeight(height / 6);
lpsame = (LayoutParams) btn_0.getLayoutParams();
lpsame.width=width/4;
lpsame.height=height/6;
btn_0.setLayoutParams(lpsame);
LayoutParams lpedit = (LayoutParams) edit_screen.getLayoutParams();
lpedit.width=width;
lpedit.height=height/6;
LayoutParams lpequal = (LayoutParams) btn_0.getLayoutParams();
lpequal.width=width/2;
lpequal.height=height/6;
edit_screen.setLayoutParams(lpedit);
btn_2.setLayoutParams(lpsame);
btn_3.setLayoutParams(lpsame);
btn_4.setLayoutParams(lpsame);
btn_5.setLayoutParams(lpsame);
btn_6.setLayoutParams(lpsame);
btn_7.setLayoutParams(lpsame);
btn_8.setLayoutParams(lpsame);
btn_9.setLayoutParams(lpsame);
btn_add.setLayoutParams(lpsame);
btn_subtrac.setLayoutParams(lpsame);
btn_multiply.setLayoutParams(lpsame);
btn_divide.setLayoutParams(lpsame);
btn_dot.setLayoutParams(lpsame);
btn_percentage.setLayoutParams(lpsame);
btn_CA.setLayoutParams(lpsame);
btn_back.setLayoutParams(lpsame);
btn_equal.setLayoutParams(lpequal);
editParams = new RelativeLayout.LayoutParams(width, height/6);
sameparam= new RelativeLayout.LayoutParams(width/4,height/6);
equalparam= new RelativeLayout.LayoutParams(width/2,height/6);
edit_screen.setLayoutParams(editParams);
}
You can change properties of a View by using LayoutParams class
LayoutParams lp = (LayoutParams) btn_new.getLayoutParams();
lp.width = yourWidth;
lp.height = yourHeight;
btn_new.setLayoutParams(lp);
Update
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(yourWidth, yourHeight);
yourButtons.setLayoutParams(layoutParams);
You have to get an instance of your view, before changing its properties
ImageButton btn_new = (ImageButton) findViewById(R.id.btn_new);
try this:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
LayoutParams layoutParams = image_new.getLayoutParams();
layoutParams.width=width/4;
layoutParams.height=height/6;
image_new.setLayoutParams(layoutParams);

ImageView dynamic position

i'm using the following code to position ImageView
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
bg = (ImageView) rootView.findViewById(R.id.rectimage);
bg.getLayoutParams().width = width;
bg.getLayoutParams().height = 500;
MarginLayoutParams params = (MarginLayoutParams)bg.getLayoutParams();
params.setMargins(0, height - 510, 0, 0);
i want to place ImageView always at the bottom of screen but screenHeight - image_height is not working Any ideas?
Try to add
android:layout_alignParentBottom="true"
to your ImageView in your layout xml file

Android: Get the screen resolution / pixels as integer values

this is a really simple question on which I've found no answer :/
How can I quickly access the screen resolution (width, height) as integer values?
I've tried this one, but it always shows zero on my emulator:
DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;
In my case I want to dynamically create a table with tableRows, each containing two cols. This cols all shall fill half of the screen in width.
Can someone give me a hint?
The trashkalmar answer is correct.
However, the results will be specific to the activity context. If you want the whole device screen resolution:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
Note that the results also depend on the current device orientation.
You can get screen metrics in this way:
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
1.Simply use This inside activity to get screen width and height pixels.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
2.This can also be used But requires Api level inlined check
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
int width2 = size.x;
int height2 = size.y;
} else {
int width2 = display.getWidth();
int height2 = display.getHeight();
}
3.Use This when you are not in activity but having context
WindowManager wm = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
Display display1 = wm.getDefaultDisplay();
Point size1 = new Point();
int width1;
int height1;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display1.getSize(size1);
width1 = size1.x;
height1 = size1.y;
} else {
width2 = display.getWidth();
height2 = display.getHeight();
}
I use the following:
int scrWidth = getWindowManager().getDefaultDisplay().getWidth();
int scrHeight = getWindowManager().getDefaultDisplay().getHeight();
No muss, no fuss, just 2 class variables
The easiest way will be to implement onSizeChanged. You are probably getting a value of 0 because the view hasn't initialized yet. You can't call the above code in the View constructor for example.

Categories

Resources