Thursday, May 3, 2012

Layout technique: Center a TextView with a Button left and right


Layout technique: Center a TextView with a Button left and right
While working on an application which should look similar to an iPhone application, I have to create a bar at the top where I have a back and a forward button (each on one side) and a dynamic text centered in the middle between them.
To get this done, I searched quite a lot and got it finally to work.
The surrounding layout should be the RelativeLayout with two Buttons (in my case I used ImageViews) and a TextView.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:background="#666666"
    android:paddingTop="3dip">
    <ImageView android:id="@+id/buttonLeft"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentLeft="true"
        android:paddingLeft="3dip"
        android:scaleType="centerInside"
        android:clickable="true"
        android:src="@drawable/buttonLeft"/>
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#000000"
        android:textSize="20dip"
        android:textStyle="bold"
        android:text="@string/titleText"/>
    <ImageView android:id="@+id/buttonRight"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentRight="true"
        android:paddingRight="3dip"
        android:scaleType="centerInside"
        android:clickable="true"
        android:src="@drawable/buttonRight"/>
</RelativeLayout>
As you can see, there are some layout params the auto completion didn’t list at all.
The params layout_centerVertical / layout_centerHorizontal do what they are named for and layout_alignParentRight / layout_alignParentLeft are like the CSS float: left / right.
Last tipp: If you have an application with different activitys/layouts but with some repeating layout parts, like the bar at the top, try to use the Tag.

Android Tutorial – Overview and contents.


Android Tutorial – Overview and contents



Because it is a very efficient and attractive platform:
  • open source platform based on Linux;
  • portable across a wide range of mobile platforms;
  • optimized for low-power and low-memory devices;
  • supports Java applications based on the Dalvik virtual machine;
  • has multimedia support for 2D vector graphics, OpenGL, MP3, AVC and AAC;
  • increased application security;
  • component-based architecture for applications;
  • data storage solution based on SQLite;
Android has become one of the major mobile platforms along Windows Mobile, Symbian, iPhone and J2ME (Java Mobile Edition)
In order to learn and develop Android applications you need to (for Windows) :
  1. download and install the Java Development Kit (JDK); the latest is 7 but any version starting with JDK 6.0 is recommended; you can download the JDK from java.oracle.comDO NOT install the JRE (Java Runtime Environment) because Eclipse and the Android SDK need the tools from the JDK (Java Development Kit); even if you have a 64bit system it is recommended to install the 32bit version because there are still some compatibility problems regarding the JDK, the Eclipse IDE and the Android SDK;
  2. install the Eclipse IDE; using an IDE is not required but it is recommended as it is going to save you a lot of time and it will let you concentrate on the solution rather than on how to compile, build and execute it; Eclipse is available at http://www.eclipse.org/ and you should download the Eclipse IDE for Java Developers or the Eclipse Classic version; same as the JDK install the 32bit version;
  3. download and install the Android SDK Starter Package; the are two distributions for the Starter Package, both available at developer.android.com/sdk/index.html; if you have Windows and you chose the installer, which is the recommended version, on Windows 7 you can get an error (bug) because the installer does not detect the Java JDK; the solution (in some cases) is to hit Back and after that Next to return to the JDK detection step; install the Android SDK to a permanent directory such as C:\Android;
  4. using the Android SDK Manager (installed at the previous step) download the Android SDK Components which include SDK Tools, documentation, platforms, libraries, USB Driver for Windows and samples; at the SDK Manager start it will check available downloads and updates but you can manage what components to download from the Available packages panel;
    Using Android SDK Manager to download the Android SDK Components
    Using Android SDK Manager to download the Android SDK Components
    Using Android SDK Manager to download the Android SDK Components
    Using Android SDK Manager to download the Android SDK Components
  5. install the ADT (Android Development Toolkit) Plugin for Eclipse:
    • in Eclipse menu select Help > Install New Software…;
    • click Add button, in the top-right corner;
      Install the ADT (Android Development Toolkit) Plugin for Eclipse
      Install the ADT (Android Development Toolkit) Plugin for Eclipse
    • in the Add Repository form, enter “Android ADT Plugin” (or whatever name you like) for the Name and the following URL for the Location:
    https://dl-ssl.google.com/android/eclipse/
    • click OK (If you have trouble acquiring the plugin, try using “http” in the Location URL, instead of “https“)
    • in the Available Software dialog, select the checkbox next to Developer Tools and clickNext;
      Install the ADT (Android Development Toolkit) Plugin for Eclipse
      Install the ADT (Android Development Toolkit) Plugin for Eclipse
    • in the next window (install details and items review) click Next;
    • read and accept the license agreements (check the radio button) and click Finish;
  6. restart Eclipse;
  7. configure the Eclipse ADT plugin:
    • in Eclipse menu select Window > Preferences…to open the Preferencespanel;
      Configure the Eclipse ADT plugin
      Configure the Eclipse ADT plugin
    • in the Preferences window select Android category from the left panel;
    • in the main panel, click Browse… and locate the Android SDK directory (in this tutorial, the Android SDK was installed at step 3 in C:\Android);
    • click Apply, then OK.
  8. test the Android SDK and the Android Platform by running the emulator; to do this you must create an Android Virtual Device (AVD)using the Android SDK and AVD Manager:
    • open the Android SDK and AVD Manager from Start > Programs > Android SDK Toolsor from Eclipse using the Window > Android SDK and AVD Manager menu option;
    • from the left panel, select the Virtual devices category;
    • in the main panel click the New… button;
      Create an Android Virtual Device (AVD)
      Create an Android Virtual Device (AVD)
    • in the Create new Android Virtual Device window set the name of the virtual emulator (A), the Android Platform (B), the size of the memory card (C), the emulator skin (D) and other hardware settings (E);
    • select the newly created virtual device and start the emulator using the Start… button;
Android Virtual Device

Simple Expandable List Example


Simple Expandable List Example


Description:
This example shows how to create Expandable list using simple default adapter.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it SimpleExpandableListExample.
2.) Run for output.
Steps:
1.) Create a project named SimpleExpandableListExample and set the information as stated in the image.
Build Target: Android 2.3.3
Application Name: SimpleExpandableListExample
Package Name: com.example
Activity Name: SimpleExpandableListExampleActivity
Min SDK Version: 10
2.) Open SimpleExpandableListExampleActivity.java file and write following code there:
package com.example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
public class SimpleExpandableListExampleActivity extends ExpandableListActivity {
    private static final String NAME = "NAME";
    private static final String IS_EVEN = "IS_EVEN";
   
    private ExpandableListAdapter mAdapter;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = newArrayList<List<Map<String, String>>>();
        for (int i = 0; i < 20; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, "Item " + i);
            curGroupMap.put(IS_EVEN, (% 2 == 0) ? "This group is even" : "This group is odd");
           
            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < 5; j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
               // curChildMap.put(NAME, "Child " + j);
                curChildMap.put(IS_EVEN, (% 2 == 0) ? "Hello " + j: "Good Morning "+ j);
            }
            childData.add(children);
        }
       
        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(
                this,
                groupData,
                android.R.layout.simple_expandable_list_item_1,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 },
                childData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );
        setListAdapter(mAdapter);
    }
}
3.) Compile and build the project.
Share and Enjoy