Wednesday, March 31, 2010

Using color in Android, by Java code

In last article, Using color in Android, by XML, described how to define color in XML file. Here, changing color using Java code will be described.



Keep using the main.xml and mycolor.xml

Modify Java code
package com.exercise.AndroidColor;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class AndroidColor extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final LinearLayout backGround = (LinearLayout)findViewById(R.id.background);
Button whiteButton = (Button)findViewById(R.id.whitebutton);
Button redButton = (Button)findViewById(R.id.redbutton);
Button greenButton = (Button)findViewById(R.id.greenbutton);
Button blueButton = (Button)findViewById(R.id.bluebutton);

whiteButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
backGround.setBackgroundResource(android.R.color.white);
}});

redButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
backGround.setBackgroundColor(0xff000000 + 0xff0000);
}});

greenButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
backGround.setBackgroundResource(R.color.green);
}});

blueButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
backGround.setBackgroundResource(R.color.blue);
}});
}
}


Download the files.

Tuesday, March 30, 2010

Using color in Android, by XML

In Android, colors can be defined directly, such as "#ff0000" for red. You can also create a resource file under /res/values folder (with any file name), with your own color id defined inside. Such that you can access them in Java code using id. Android also defined a base set in android.R.color.

example:



/res/values/mycolor.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
</resources>


/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/background"
>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

<!-- "white" defined in Android base set of colors -->
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="WHITE"
android:textColor="@android:color/white"
android:id="@+id/whitebutton"
/>

<!-- direct define textColor -->
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RED"
android:textColor="#ff0000"
android:id="@+id/redbutton"
/>

<!-- "green" defined in mycolor.xml -->
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GREEN"
android:textColor="@color/green"
android:id="@+id/greenbutton"
/>

<!-- "blue" defined in mycolor.xml -->
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BLUE"
android:textColor="@color/blue"
android:id="@+id/bluebutton"
/>
</LinearLayout>




Friday, March 12, 2010

SDK Tools, Revision 5 available

SDK Tools, Revision 5 (March 2010)
Dependencies:
  • If you are developing in Eclipse with ADT, note that SDK Tools r5 is designed for use with ADT 0.9.6 and later. After installing SDK Tools r5, we highly recommend updating your ADT Plugin to 0.9.6.
  • For Mac OS platforms, OS X 10.4.x (Tiger) is no longer officially supported.
SDK and AVD Manager:
  • Fixes SSL download for the standalone version of the SDK Updater.
  • Fixes issue with 64-bit JVM on Windows.
  • Adds support for platform samples components.
  • Improves support for dependency between components.
  • AVDs now sorted by API level.
  • The AVD creation dialog now enforces a minimum SD card size of 9MB.
  • Prevents deletion of running AVDs.
  • Settings are now automatically saved, no need to click "Apply".
Emulator:
  • Emulator now requires SD card to be 9MB or more.
Layoutopt:
  • Fixes layoutopt.bat to execute correctly on Windows.
You can refer to my article to know how to Install Android SDK on Eclipse 3.5 Galileo, in Ubuntu 9.10. Or, if you have install Android 1.6 SDK or later version, you can refer to another article to upgrade Android SDK using Android SDK and AVD Manager.