I am writing an application in which I want to make one activity small when it goes to the background and resize it when it is clicked.
Basically I want a floating window like Skype video call floating window.
I have tried to add an child activity for the main activity but I cannot afford to create another activity again, so my idea is to make this current activity small when go to background and resize it when it is clicked.
To make is small I do this
//Get the window
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,WindowManager.LayoutParams.FLAG_DIM_BEHIND);
// Params for the window.
// You can easily set the alpha and the dim behind the window from here
WindowManager.LayoutParams params = window.getAttributes();
params.alpha = 1.0f; // lower than one makes it more transparent
params.dimAmount = 0f; // set it higher if you want to dim behind the window
window.setAttributes(params);
// Gets the display size so that you can set the window to a percent of that
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
// You could also easily used an integer value from the shared preferences to set the percent
if (height > width) {
window.setLayout((int) (width * .35), (int) (height * .30));
} else {
window.setLayout((int) (width * .20), (int) (height * .35));
}
Now I want to set the onclicklistener on the window to make is bigger when clicked.
Thanks for your help in advance.
Related
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;
Referring to below image, I want that view with yellow background and right border to slide in from right with some text. So this requirements include:
creating a custom view (which will need canvas I guess)
giving it width, height, position, color, border, border-color, etc.
animation to slide from right
textview within this custom view to display text
I referred this tutorial to understand how to include view with a canvas in layout. But instead of hard coded point positions, I tried following:
public MyView(Context context) {
super(context);
WindowManager wm = (WindowManager) DashBoardActivity.mContext
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
width -= 10;
height -= 50;
myPath = new Pt[6];
myPath[0] = new Pt(width, height);
myPath[1] = new Pt(width - 400, height);
myPath[2] = new Pt(width - 500, height - 100);
myPath[3] = new Pt(width - 400, height - 200);
myPath[4] = new Pt(width, height - 200);
myPath[5] = new Pt(width, height);
}
But view is not getting positioned as per the expectation. I tried with different hard-coded positions, but either it's getting displayed at wrong position or not getting displayed at all.
Also, to show the text, I added textview in the layout itself but it is getting displayed at top left of the screen instead of the position shown in below image.
How I can achieve this ? Any suggestions appreciated.
You dont need any custom view for this, you can set shape or 9patch image as background to the textview and animate that textview using object animator.
Border in shape xml
In an Android Application, I want to know the height and the width of the screen. I'm using the following code:
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
The problem is than when the soft navigation bar exists, the returned height = the actual height - navigation bar height.
I want to get the total value.
Note: I'm using Android SDK 13 (v 3.2).
Check this, it may help:
FrameLayout root = (FrameLayout) findViewById(R.id.root);
root.post(new Runnable() {
public void run() {
Rect rect = new Rect();
Window win = getWindow(); // Get the Window
win.getDecorView().getWindowVisibleDisplayFrame(rect);
// Get the height of Status Bar
int statusBarHeight = rect.top;
// Get the height occupied by the decoration contents
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
// Calculate titleBarHeight by deducting statusBarHeight from contentViewTop
int titleBarHeight = contentViewTop - statusBarHeight;
// By now we got the height of titleBar & statusBar , Now lets get the screen size
int screenHeight = dm.heightPixels;
// Now calculate the height that our layout can be set If you know that your application doesn't have statusBar added, then don't add here also.
//Same applies to application bar also
int layoutHeight = screenHeight - (titleBarHeight + statusBarHeight);
}
});
You can use
display.getRealSize(size); instead of display.getSize(size);
source:
http://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)
you can use DisplayMetrics. This class holds information about the screen such its size, density, font scaling. For instance
getResources().getDisplayMetrics().widthPixels
returns
The absolute width of the display in pixels.
to retrieve the height
getResources().getDisplayMetrics().heightPixels
In onCreate method of your class write down this code, and you will get the actual width and height of screen:
Display mDisplay = this.getWindowManager().getDefaultDisplay();
final int width = mDisplay.getWidth();
final int height = mDisplay.getHeight();
System.out.println("width " + width + " height " + height);
Please get me correctly over here :
I want to get the height/width of the space available to the Activity/Layout in onCreate() method to calculate the height that can be given to child layouts. I can get the screen size using :
root = (LinearLayout) findViewById(R.id.mainroot); // Main layout of LinearLayout
android.view.Display display = getWindowManager().getDefaultDisplay();
int height = Display.getHeight(); // I know this is deprecated have hence used
int width = Display.getWidth(); // DisplayMetrics
int childWidth, childHeight;
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
//int density = metrics.densityDpi;
height = metrics.heightPixels; //480
width = metrics.widthPixels; //320
This both the methods/ways gives me same height and width i.e. size of full screen. What I am looking for is to get actual height that is avaialbe for the layout after deduction of Application Title, Status Bar, etc.
Any idea how to get this. OR to get the sizes of titles, etc - what all should be counted over here. On emulator I see 2 bars on top - 1 must be application titile what an be the other one. I can get heights of them all and deduct from screen height.
ONE more point : In this case I will be setting the height programamtically so it will be pixel based (as I can setheight in pixels only I guess) will that affect in density factor with differnet screen sizes. What can be a way to calculate height (lets say 50%) for child layout that will be same for any density o so.
SOLUTION :
In my onCreate(), I added the following lines :
setContentView(R.layout.mainpage);
root = (LinearLayout) findViewById(R.id.mainroot);
root.post(new Runnable() {
public void run() {
Rect rect = new Rect();
Window win = getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(rect);
int statusHeight = rect.top;
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
titleHeight = contentViewTop - statusHeight;
Log.i(Utility.TAG, "titleHeight = " + titleHeight + " statusHeight = " + statusHeight + " contentViewTop = " + contentViewTop);
// CALCULATE THE SIZE OF INNER LAYOUTS
calculateChildSize();
}
});
With the above code, I get the values of titleBar & statusBar. On deducting it from metrics.heightPixels; I get the required height of the screen.
Good Point is this code works for all density's.
Hope this helps others too.
FOR IMPROVEMENT : I have to do similar calculations for all Activities in my application, so was thinking about writing this code only once. I can save teh titleHeight to a static variable so can use in all activities.
BUT
Can the user change the phone's density at runtime.
If so, then the Activity's onCreate will be called again or not ?
If not, then can I trap the density change event where I can add this code and make the current activity to refresh.
Any idea suggestions for improving is appreciated.
//Where rootView is the object for the root view of your application
final int viewWidt = rootView.getMeasuredWidth();
There is very easy method for that. doOnLayout() method is called as soon as layout is measured and ready:
rootView.doOnLayout {
rootView.getMeasuredWidth()
rootView.getMeasuredHeight()
}
I have a layout with an imageview. This imageview should have a 4/3 ratio. In portrait mode i get the width of the screen then compute the height and set the imageView dimensions.
int[] tailles = new int[2];
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int widthScreen = dm.widthPixels;
tailles[0] = widthScreen;
tailles[1] = (tailles[0]*3)/4;
This part works great.
In landscape mode i need to set the imageView width according to the available height. So i need to get the height of the screen minus the status/notification bar.
I saw the value of 48px for the bar in this question. Is it a standard size ?
What the best solution to get the available height regardless of the used device ?
Display d = ((Activity) mContext).getWindowManager()
.getDefaultDisplay();
int h = d.getHeight();
int w = d.getWidth();
in portrait h > w and in landscape w > h
If you're creating an Activity, than you have to have some sort of layout. Get it's root component, and retrieve it's size (getWidth, getHeight). That'll be the available width and height without counting the status bar.