Friday, December 27, 2013

android:spinnerMode="dialog"

Example of Android Spinner without and with android:spinnerMode="dialog":

Normal Spinner without android:spinnerMode="dialog"
Normal Spinner without android:spinnerMode="dialog"

Spinner with android:spinnerMode="dialog"
Spinner with android:spinnerMode="dialog"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com"
android:textColor="#A00000"
android:textStyle="bold|italic" />

<Spinner
android:id="@+id/myspinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/myspinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />

</LinearLayout>

package com.example.androidspinner;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.app.Activity;

public class MainActivity extends Activity {

String[] dayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Spinner MySpinner1 = (Spinner)findViewById(R.id.myspinner1);
ArrayAdapter<String> myAdapter1 = new
ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dayOfWeek);
myAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MySpinner1.setAdapter(myAdapter1);

Spinner MySpinner2 = (Spinner)findViewById(R.id.myspinner2);
ArrayAdapter<String> myAdapter2 = new
ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dayOfWeek);
myAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MySpinner2.setAdapter(myAdapter2);
}

}

Monday, December 23, 2013

Google Maps Android API Utility Library

Google Maps Android API Utility Library is an open-source library of classes that are useful for a range of applications, to add advanced features to your maps.

The source of the Google Maps Android API Utility Library is available on GitHub. The GitHub repository includes the utility classes and a demonstration application that illustrates the use of each class. To get started, follow the setup guide for Eclipse. Alternatively, the project's website includes a getting-started guide for Android Studio/Gradle and Maven. The reference documentation is also available on GitHub. Below is an overview of the utilities in the library.

This video discusses the utility library, with a focus on polyline decoding, spherical geometry, and bubble icons:

Monday, December 16, 2013

Example of using TextWatcher

Example of using TextWatcher
Example of using TextWatcher


package com.example.androidtextwatcher;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

EditText input;
TextView info, cont;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

input = (EditText)findViewById(R.id.input);
info = (TextView)findViewById(R.id.info);
cont = (TextView)findViewById(R.id.cont);
input.addTextChangedListener(myTextWatcher);
}

TextWatcher myTextWatcher =
new TextWatcher(){

@Override
public void afterTextChanged(Editable s) {
/*
* This method is called to notify you that,
* somewhere within s, the text has been changed.
*/
//info.setText("");
//cont.setText(s.toString());
}

@Override
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {
/*
* This method is called to notify you that,
* within s, the count characters beginning at
* start are about to be replaced by new text
* with length after.
*/
info.setText("beforeTextChanged - " + start + " " + count + " " + after);
cont.setText(s.toString());
}

@Override
public void onTextChanged(CharSequence s,
int start, int before, int count) {
/*
* This method is called to notify you that,
* within s, the count characters beginning
* at start have just replaced old text that
* had length before.
*/
info.setText("onTextChanged - " + start + " " + before + " " + count);
cont.setText(s.toString());
}};

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com"
android:textColor="#A00000"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/cont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

Sunday, December 15, 2013

Set layout programmatically

This example show how to set paddings and margins programmatically with Java code.

set paddings and margins programmatically
set paddings and margins programmatically

package com.example.androidadjlayout;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

LinearLayout mainLayout;
SeekBar settingPadding, settingMargin2;
ImageView image1, image2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout)findViewById(R.id.mainlayout);
settingPadding = (SeekBar)findViewById(R.id.setmainpadding);
settingMargin2 = (SeekBar)findViewById(R.id.setimage2margin);
image1 = (ImageView)findViewById(R.id.image1);
image2 = (ImageView)findViewById(R.id.image2);

settingPadding.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mainLayout.setPadding(progress, progress, progress, progress);
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
}

@Override
public void onStopTrackingTouch(SeekBar arg0) {
}});

settingMargin2.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
LinearLayout.LayoutParams layoutParams = (LayoutParams) image2.getLayoutParams();
layoutParams.setMargins(progress, progress, progress, progress);
image2.setLayoutParams(layoutParams);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}});
}

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#303030" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#A00000"
android:textStyle="bold|italic"
android:text="android-coding.blogspot.com" />
<SeekBar
android:id="@+id/setmainpadding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="5"/>
<SeekBar
android:id="@+id/setimage2margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="5"/>
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:background="#505050"/>
<ImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:src="@drawable/ic_launcher"
android:background="#808080" />

</LinearLayout>

Thursday, December 12, 2013

Game Physics with Liquid Fun

Game On!: Game Physics with Liquid Fun

Todd Kerpelman and Wolff Dobson share some of the exciting new details on the open-source release of LiquidFun, a new C++ 2D physics library that makes it easier for developers to add realistic physics to their games.

Based on Box2D, LiquidFun features particle-based fluid simulation. Game developers can use it for new game mechanics and add realistic physics to game play. Designers can use the library to create beautiful fluid interactive experiences.

For more information:
http://android-developers.blogspot.com/

To get the library:
http://google.github.io/liquidfun/

Build mobile apps with Chrome WebView

Build mobile apps with Chrome WebView - Chrome Dev Summit 2013 (Matt Gaunt)

Learn how to create mobile UIs with a mix of the latest HTML5 features and native UIs and features.

Slides: http://gauntface.co.uk/presentations/chrome-dev-summit-2013/chrome-webview/

Tuesday, December 10, 2013

setImageDrawable vs setImageResource vs setBackground

Example show usage of setImageDrawable(), setImageResource(), setBackgroundDrawable(), setBackgroundResource and setBackground().

package com.example.androidsetimage;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ImageView image1 = (ImageView)findViewById(R.id.image1);
ImageView image2 = (ImageView)findViewById(R.id.image2);
ImageView image3 = (ImageView)findViewById(R.id.image3);
TextView text4 = (TextView)findViewById(R.id.textview4);
Button button5 = (Button)findViewById(R.id.button5);
EditText edittext6 = (EditText)findViewById(R.id.edittext6);

image1.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
image2.setImageResource(R.drawable.ic_launcher);

//Requires API 16
image3.setBackground(getResources().getDrawable(R.drawable.ic_launcher));

//deprecated
text4.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));

button5.setBackgroundResource(R.drawable.ic_launcher);

//Requires API 16
edittext6.setBackground(getResources().getDrawable(R.drawable.ic_launcher));
}

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303030"
android:textStyle="bold|italic"
android:text="android-coding.blogspot.com" />
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 4"/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 5"/>
<EditText
android:id="@+id/edittext6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hello android-coding"/>

</LinearLayout>