Author Archives: wpadmin

Set spinner style

This is simple to do in xml. This example makes the spinner look like an edit text. In styles xml file add <style parent=”@android:style/Widget.Spinner” name=”SpinnerAsEditText”><item name=”android:background”>@android:drawable/edit_text</item> </style> In layout file add spinner. <Spinner android:id=”@+id/spinner1″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” style=”@style/SpinnerAsEditText” />  

Posted in Android | Leave a comment

Create spinner from array

Create a sinner and an adapter, link the two then add to the layout. Use array from strings xml file or created programmatically. LinearLayout myLayout = (LinearLayout) findViewById(R.id.layout1); ArrayList spinnerArray = new ArrayList(); spinnerArray.add(“a”); spinnerArray.add(“b”); spinnerArray.add(“c”); Spinner spinner = new … Continue reading

Posted in Android | Leave a comment

Get API level programmatically

The SDK level (integer) running is available in android.os.Build.VERSION.SDK_INT. int currentapiVersion = android.os.Build.VERSION.SDK_INT; You can then use the int value or the enum corresponding to this int in the android.os.Build.VERSION_CODES class. if (currentapiVersion >= 18) or if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO)  

Posted in Android | Leave a comment

Assign R.id to dynamically created editText

With thanks to stackoverflow, 2 methods, 1 simple and one less simple. 1) You can assign an id after creating the edit text and before adding it to the layout. e.setId(int) This doesn’t put it in the R.java file (so … Continue reading

Posted in Android | Leave a comment

Textwatcher example

A simple example to watch or text changed. ((EditText)view.findViewById(R.id.manualCodeEntry)).addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub … Continue reading

Posted in Android | Leave a comment

Simple listview example

1. Start with a simple layout for each line. <?xml version=”1.0″ encoding=”utf-8″?> <TextView xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:padding=”10dp” android:textSize=”20sp” > </TextView> Create activity extending ListActivity. public class WordListActivity extends ListActivity { static final String[] words = new String[] { “Apple”, “Bread”, … Continue reading

Posted in Android | Leave a comment

Eclipse oddities

1. Unbound prefix I was getting an error in a layout file: error parsing xml, unbound prefix. It turned out that a line of xml pasted in from another (working) layout file contained the prefix ‘android1:’, whereas all other lines … Continue reading

Posted in Android | Leave a comment

Examples of setting colours dynamically

Buttons and textviews. button.setBackgroundColor(Color.parseColor(“#bdbdbd”)); yourTextView.setTextColor(0xffbdbdbd); textView.setTextColor(getResources().getColor(R.color.sf_yellow)); Where colors.xml (in values folder) contains <resources> <color name=”sf_yellow”>#f5e214</color> </resources> On canvas simply: canvas.drawColor(Color.BLUE); or for greater control: float[] color=new float[3]; // HSV (0..360,0..1,0..1) //color[0]=(float)(Math.random()*360); color[0]=197; color[1]=1; color[2]=1; canvas.drawColor(Color.HSVToColor(color));  

Posted in Android | Leave a comment

Android screenshots

To take a screen shot on an android phone with 4.0 or above. Standard methods: press the power button and the volume down button together for 1-2 seconds or press the power button for 1-2 seconds and select the screen … Continue reading

Posted in Android | Leave a comment

Crash passing args in TextWatcher when orientation has changed

I have a fragment with a TextWatcher on an edittext and the onTextChanged sends the current edittext string to a method in a DatabaseHelper class (extends SQLiteOpenHelper) to check it. If I change the orientation (I do it twice to … Continue reading

Posted in Android | Leave a comment