Feb 26, 2011

Final Android 3.0 Platform and Updated SDK Tools


We are pleased to announce that the full SDK for Android 3.0 is now available to developers. The APIs are final, and you can now develop apps targeting this new platform and publish them to Android Market. The new API level is 11.

For an overview of the new user and developer features, see the Android 3.0 Platform Highlights.

Together with the new platform, we are releasing updates to our SDK Tools (r10) and ADT Plugin for Eclipse (10.0.0). Key features include:

  • UI Builder improvements in the ADT Plugin:
    • New Palette with categories and rendering previews. (details)
    • More accurate rendering of layouts to more faithfully reflect how the layout will look on devices, including rendering status and title bars to more accurately reflect screen space actually available to applications.
    • Selection-sensitive action bars to manipulate View properties.
    • Zoom improvements (fit to view, persistent scale, keyboard access) (details).
    • Improved support for layouts, as well as layouts with gesture overlays.
  • Traceview integration for easier profiling from ADT. (details)
  • Tools for using the Renderscript graphics engine: the SDK tools now compiles .rs files into Java Programming Language files and native bytecode.

To get started developing or testing applications on Android 3.0, visit the Android Developers site for information about the Android 3.0 platform, the SDK Tools, and the ADT Plugin.



Source: http://android-developers.blogspot.com/2011/02/final-android-30-platform-and-updated.html

Feb 25, 2011

Detect Android Wifi State

int WifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE ,
WifiManager.WIFI_STATE_UNKNOWN);


The returned value can be:

Feb 24, 2011

Turn Android Wifi On/Off

To turn Wifi ON:
    WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);


To turn Wifi OFF:
    WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);

Feb 22, 2011

SimpleDateFormat

Usage of SimpleDateFormat:

 SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy  hh:mm:ss a");  
String now = formatter.format(new Date());


Feb 21, 2011

Change Screen Brightness

To change screen brightness:

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.buttonBrightness = value;
getWindow().setAttributes(layoutParams);


where value is float between 0 to 1, value of less than 0 to use the preferred default screen brightness.

Feb 20, 2011

Change the desired orientation of activity: setRequestedOrientation()

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


public void setRequestedOrientation(int requestedOrientation) is used to change the desired orientation of this activity. If the activity is currently in the foreground or otherwise impacting the screen orientation, the screen will immediately be changed (possibly causing the activity to be restarted). Otherwise, this will be used the next time the activity is visible.

Where requestedOrientation is an orientation constant: ActivityInfo.screenOrientation.

Related: How to set orientation of activity/application in AndroidManifest.xml

Feb 19, 2011

InputFilter

InputFilters can be attached to Editables to constrain the changes that can be made to them.

ex.
        EditText edit = (EditText)findViewById(R.id.edit);
InputFilter[] FilterArray = new InputFilter[2];
FilterArray[0] = new InputFilter.LengthFilter(4); //Limit length of the text
FilterArray[1] = new InputFilter.AllCaps(); //capitalize all the lower case letters
edit.setFilters(FilterArray);


Feb 18, 2011

Toast with image

It's a sample code to display Toast with image.

Toast with image

package com.AndroidImageToast;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

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

Toast ImageToast = new Toast(getBaseContext());
LinearLayout toastLayout = new LinearLayout(getBaseContext());
toastLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView image = new ImageView(getBaseContext());
TextView text = new TextView(getBaseContext());
image.setImageResource(R.drawable.icon);
text.setText("Hello!");
toastLayout.addView(image);
toastLayout.addView(text);
ImageToast.setView(toastLayout);
ImageToast.setDuration(Toast.LENGTH_LONG);
ImageToast.show();
}
}


Feb 17, 2011

Using inputType="textPassword" to format EditText as password



<EditText  
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
/>


Feb 16, 2011

ImageButton

ImageButton displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the XML element or by the setImageResource(int) method.

To remove the standard button background image, define your own background image or set the background color to be transparent.

ImageButton

To implement a ImageButton, create three images (for normal, on focus, ad clicked) and place in /drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/imgbutton_3" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/imgbutton_2" /> <!-- focused -->
<item android:drawable="@drawable/imgbutton_1" /> <!-- default -->
</selector>


Create a xml in /drawable folder, named imgbutton.xml here.

Add ImageButton in XML, refer to imgbutton.xml as android:background.
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageButton
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/myimagebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/imgbutton"
/>
</LinearLayout>


Feb 8, 2011

Get Android OS info from android.os.Build

Get Android OS info from android.os.Build

package com.GetOsBuildInfo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

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

TextView device = (TextView)findViewById(R.id.device);
TextView model = (TextView)findViewById(R.id.model);
TextView product = (TextView)findViewById(R.id.product);
TextView codename = (TextView)findViewById(R.id.codename);
TextView incremental = (TextView)findViewById(R.id.incremental);
TextView release = (TextView)findViewById(R.id.release);
TextView sdk = (TextView)findViewById(R.id.sdk);
TextView sdkInt = (TextView)findViewById(R.id.sdk_int);

device.setText("android.os.Build.DEVICE: " + android.os.Build.DEVICE);
model.setText("android.os.Build.MODEL: " + android.os.Build.MODEL);
product.setText("android.os.Build.PRODUCT: " + android.os.Build.PRODUCT);
codename.setText("android.os.Build.VERSION.CODENAME: " + android.os.Build.VERSION.CODENAME);
incremental.setText("android.os.Build.VERSION.INCREMENTAL: " + android.os.Build.VERSION.INCREMENTAL);
release.setText("android.os.Build.VERSION.RELEASE: " + android.os.Build.VERSION.RELEASE);
sdk.setText("android.os.Build.VERSION.SDK: " + android.os.Build.VERSION.SDK);
sdkInt.setText("android.os.Build.VERSION.SDK_INT: " + String.valueOf(android.os.Build.VERSION.SDK_INT));
}
}


<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/device"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/model"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/product"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/codename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/incremental"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/release"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/sdk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/sdk_int"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Feb 6, 2011

Get Android OS property using System.getProperty(String)

System.getProperty(String propertyName) returns the value of a particular system property or null if no such property exists.

Android OS property

package com.GetOsInfo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class GetOsInfo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView arch = (TextView)findViewById(R.id.arch);
TextView name = (TextView)findViewById(R.id.name);
TextView version = (TextView)findViewById(R.id.version);
arch.setText("os.arch: " + System.getProperty("os.arch"));
name.setText("os.name: " + System.getProperty("os.name"));
version.setText("os.version: " + System.getProperty("os.version"));
}
}


<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/arch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/version"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Feb 4, 2011

Get screen resolution of Android device

DisplayMetrics ia a structure describing general information about a display, such as its size, density, and font scaling.

Get screen resolution of Android device

package com.GetScreenResolution;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class GetScreenResolution extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView screenWidth = (TextView)findViewById(R.id.screenwidth);
TextView screenHeight = (TextView)findViewById(R.id.screenheight);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

screenWidth.setText("Screen Width = " + dm.widthPixels);
screenHeight.setText("Screen Height = " + dm.heightPixels);
}
}


<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/screenwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/screenheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Feb 3, 2011

SeekBar

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.

Clients of the SeekBar can attach a SeekBar.OnSeekBarChangeListener to be notified of the user's actions.

SeekBar

<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
/>
<TextView
android:id="@+id/seekbarvalue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


package com.AndroidSeekBar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class AndroidSeekBar extends Activity {

TextView seekBarValue;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
seekBarValue = (TextView)findViewById(R.id.seekbarvalue);
seekBar.setOnSeekBarChangeListener(seekBarChangeListener);

}

SeekBar.OnSeekBarChangeListener seekBarChangeListener
= new SeekBar.OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(arg1));
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}};
}


Infolinks In Text Ads