Determining the Size of an Android View or Screen at Run Time
For efficient bitmap handling or dynamic View creation in an App the area that a widget or layout is using needs to be known. If no fixed sizes are allocated at design time the size of a View may not be known until an App is executed. This is because of the wide range of display sizes that Android supports. The example code snippets in this articles shows how to read the screen size and the size of Views as the App runs. To run the example code you will need to create a new Android project (those new to Android programming can view the article Your First Android Hello World Java Program to see how), we called our App View Size.
In the layout designer for activity_main.xml (or whatever you called your layout) add another TextView, called textXY, next to the existing Hello world! widget. Change the Text on the first TextView to X,Y. Add this code to the oncreate method in MainActivity.java (or whatever class you are using), you will need an imports for TextView and DisplayMetrics. :
//object to store display information
DisplayMetrics metrics = new DisplayMetrics();
//get display information
//show display width and height
This is the code running on an AVD with a 320×480 screen:
He is the layout used for this screen: Continue reading
In Android Get View Defined in XML in Activity Code
The UI screens for an App can be designed outside of the code. They are stored in XML files. This eases support for multiple screen sizes and types and helps with software maintenance. Classes in the Android SDK have methods used to access the UI components. Screens are composed of various implementations of the Android View class. The major sub-classes for Views are widgets and ViewGroups. The widget class is not to be confused with Widgets that can be added to the Android home screen. Instead View widgets are the normal components with which the Android device users interact, including the Button, Checkbox, EditText (a text box), ImageView, RadioButton, ProgressBar, TextView (label) and many more. Several widgets sit in a ViewGroup which provides a container for laying out components. Different ViewGroups provide different types of layouts, including RelativeLayout, LinearLayout, ScrollView, WebView and others. ViewGroups can contain other ViewGroups as well as widgets therefore building complex displays by nesting different Views is possible.
Create a Basic Android Screen
For this tutorial create a new, simple Android App project and call it Button Demo (if you don’t know how see Your First Android Hello World Java Program). A simple screen that just has a button on it was created using the starting layout, we kept the default layout name of activity_main.xml. In Eclipse use the Package Explorer to open the activity_main.xml file (in the res/layout folder). Using the Graphical Layout view (selected via the tabs at the bottom of the editor window) delete the TextView, showing Hello world!, and drag and drop a Button from the Form Widgets folder onto the screen. This screen’s code is stored in the activity_main.xml file:
To show how this view is accessed from the App’s code the text on the button will be changed and it displays a message when it is clicked. Continue reading
How to Display A Smaller Window on Top of an Activity
The Activity screen is not the only way to interact with the user of an App. A brief message or dialog can be displayed in appropriate circumstances to ask a specific question or get specific input. Android has build in support for such small focused interactions that require immediate attention.
- The Toast class – to display brief informational only message.
- The Dialog class, managed by a DialogFragment – to support a flexible input that can display information and can ask for inputs or choices. There are several built in sub-classes including:
- AlertDialog – supports buttons (from zero to three), list selections, check boxes and radio buttons.
- ProgressDialog – supports a progress bar or wheel and supports buttons because it extends AlertDialog.
- DatePickerDialog – supports selecting a date.
- TimePickerDialog – supports a time selection.
- The PopupWindow class – allows a View to float on top of an existing activity. Suitable for custom informational messages.
These classes build their user interfaces through class methods with support from custom layouts when required. This article looks at using the PopupWindow class. What is the PopupWindow? The Android developer documention has the following class overview:
“A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.”
It can be used to display extra information without needing to code another Activity. An example would be to show results at the end of a game (or end of a level), which we will be doing in the following tutorial. The steps for this example code are:
- Create a new project.
- Design the pop-up window.
- Add the code to load and populate the pop-up window.
- Call the code.
Open a Project to Add the Pop-Up Window
In this tutorial we are assuming that you have a working project in Eclipse to use as a base, either open an existing project or create a new one (if you need help see Your First Android Hello World Java Program).
Design the Layout for the Pop-Up
For this example we are going to have a pop-up to display a game’s result as gold, silver and bronze medal positions. The pop-up will have a graphic (ImageView) for each of the medals, a TextView for the winner’s name and one for the score. The images for the medals came from Open Clip Art Library user momoko. They have been resized for an Android project and you can download medals_png.zip ready for importing into a project.
Use an ImageView to Show a Raster Graphic
Showing a bitmap image, such as a Portable Network Graphics (PNG) or JPEG file, in an Android application (App) is easy. In this tutorial we show you how. It is assumed you are programming in Java using the Android SDK from within the Eclipse Integrated Development Environment (IDE). For PC users starting out in Android Java development see Set Up Windows for Android Development to prepare your development environment.
The ImageView class can be used and pointed at a bitmap stored under one of the drawable folders in the res project folders. Let us start by creating a very basic App project that will contain the ImageView and bitmap to display. If you do not know how to create a basic Android project in Eclipse see the article Your First Android Program. With Eclipse open select the File menu then New and Android Application Project. In the New Android App wizard the fields are set as follows:
- Application Name – Bitmap
- Project Name – Bitmap
- Package Name – biz.tekeye.bitmap
- Build SDK – Android 4.1 (API 16)
- Minimum Required SDK – API 3: Android 1.5 (Cupcake) – or the lowest one installed
- Create custom launcher icon – unchecked
- Mark this project as a library – unchecked
- Create project in Workspace – checked
Press the Next button. Create Activity should be checked and BlankActivity selected. Press the Next button again. On the New Blank Activity screen leave the fields as default and press Finish. The Bitmap project will be created and the App’s initial screen will load in the graphical designer. Continue reading