After tracking down this problem as well, I found this note in the Android documentation:
http://source.android.com/source/using-eclipse.html
*Note: Eclipse sometimes likes to add an "import android.R" statement at the
top of your files that use resources,
especially when you ask Eclipse to
sort or otherwise manage imports. This
will cause your make to break. Look
out for these erroneous import
statements and delete them.*
While going through the Android sample tutorials, I would often use the Ctrl + Shift + O command to "Organize Imports" and generate any missing import statements. Sometimes this would generate the incorrect import statement which would hide the R.java
class that is automatically generated when you build.
If you want the display dimensions in pixels you can use getSize
:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If you're not in an Activity
you can get the default Display
via WINDOW_SERVICE
:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
If you are in a fragment and want to acomplish this just use Activity.WindowManager (in Xamarin.Android) or getActivity().getWindowManager() (in java).
Before getSize
was introduced (in API level 13), you could use the getWidth
and getHeight
methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
For the use case, you're describing, however, a margin/padding in the layout seems more appropriate.
Another way is: DisplayMetrics
A structure describing general information about a display, such as its size, density, and font scaling. To access the DisplayMetrics members, initialize an object like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
We can use widthPixels
to get information for:
"The absolute width of the display in pixels."
Example:
Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);
API level 30 update
final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
// Gets all excluding insets
final WindowInsets windowInsets = metrics.getWindowInsets();
Insets insets = windowInsets.getInsetsIgnoreVisibility(WindowInsets.Type.navigationBars()
| WindowInsets.Type.displayCutout());
int insetsWidth = insets.right + insets.left;
int insetsHeight = insets.top + insets.bottom;
// Legacy size that Display#getSize reports
final Rect bounds = metrics.getBounds();
final Size legacySize = new Size(bounds.width() - insetsWidth,
bounds.height() - insetsHeight);
Best Answer
I usually do it by copying the file. You will find the JARs inside
$ANDROID_SDK/extras/android/support
, where$ANDROID_SDK
is wherever your Android SDK is installed.If you use Eclipse, if you copy this at the filesystem level, and Eclipse is open, press
<F5>
with the project highlighted in Package Manager so Eclipse picks up the changes. Easier is to drag it out of the SDK and drop the JAR intolibs/
right in Eclipse, which will both update the filesystem and let Eclipse know about the change.It's possible that the Eclipse option to add the JARs (right-click over the project, then choose Android Tools > Add Support Library... from the context menu) will also do this. That particular approach doesn't work for me due to some peculiarities with my development environment.