Thursday, 28 July 2011

Android: how to use ColorStateList

(1) Create a folder in the 'res' folder and name it 'color'.

(2) Create an Android xml file in the new 'color' folder and name it 'mycolorlist.xml' or another name of your choosing. Place the following markup in your new xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
 android:color="@color/mycolor1"
 android:state_pressed="false" />
<item
 android:color="@color/mycolor2"
 android:state_pressed="true" />
</selector>

Be sure that 'mycolor1' and 'mycolor2' are colours specified in your 'res/values/colors.xml' file. If you're not sure how to specify colors in your 'colors.xml' file, see the end of this post.

(3) Add the following code to the TextView to which you wish to apply the new ColorStateList...
myTextView.setTextColor(getResources().getColorStateList(R.color.mycolorlist));
... where myTextView is a TextView instance.

Done! Now when myTextView is pressed, its colour will be 'mycolor2', and otherwise its colour will be 'mycolor1'.

Lastly, here is an example of what your 'colors.xml' file might look like...
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="mycolor1">#ff005eff</color>
 <color name="mycolor2">#ff86C4ed</color>
</resources>
... which specifies two different shades of blue.

Android: underline text programmatically

It's as easy as putting the following code in your Activity class...
TextView myTextView = new TextView(this);
SpannableString mySpannableString = new SpannableString("My String");
mySpannableString.setSpan(new UnderlineSpan(), 0, mySpannableString.length(), 0);
myTextView.setText(mySpannableString);

Android: make text bold programmatically

It's as easy as this...
myTextView.setTypeface(null, Typeface.BOLD);
... where myTextView is your TextView instance.

Tuesday, 26 July 2011

GWT: how to make and show a PopupPanel

Code sample for creating and showing a simple popup in gwt which has some styling, is centred in the browser and which contains a single string and a single button to close/hide the popup:

final PopupPanel popup = new PopupPanel(false);
popup.addStyleName(Resources.instance.styles().popup());
Button btnClose = new Button("Close");
btnClose.addClickHandler(new ClickHandler()
{
 @Override
 public void onClick(ClickEvent event) { popup.hide(); }
});
FlowPanel fpPopupContents = new FlowPanel();
fpPopupContents.add(new Label("My message"));
fpPopupContents.add(btnClose);
popup.setWidget(fpPopupContents);
popup.center();

Android: how to make and show an AlertDialog

Found a fair few tutorials on how to make and show an AlertDialog but this one seems to be the easiest...

AlertDialog.Builder builder = new AlertDialog.Builder(myActivityContext);
builder.setCancelable(true);
builder.setTitle("My title");
builder.setMessage("My message");
builder.create().show();

The AlertDialog.Builder has lots of other options such as for adding buttons to the AlertDialog. See the Android Developers 'Creating Dialogs' page at the following link for more info on this and other general information about creating dialogs:
http://developer.android.com/guide/topics/ui/dialogs.html

Note: if it's just a text message you need to display to the user, consider using Toast.makeText(...).show() instead... though, however, a Toast message is only visible for a few seconds at most before it disappears so not exactly ideal if user input or confirmation is required before closing the popup.

Monday, 25 July 2011

WinMerge file compare tool

Just downloaded and tried out WinMerge tool for comparing/contrasting textual changes in a file and really really impressed with how intuitive and useful it is to use.

Friday, 22 July 2011

Android: connecting to localhost

To connect the emulator to localhost, the urls...

"http://localhost:" + portNumber + "/" + subDirectoryName
"http://127.0.0.1:" + portNumber + "/" + subDirectoryName

... won't work. Use instead...

"http://10.0.2.2:" + portNumber + "/" + subDirectoryName


For more info on this, see the Android developer Using the Android Emulator page, Emulator Networking section:
http://developer.android.com/guide/developing/devices/emulator.html#networkaddresses

Android: aligning views in parent ViewGroup instance

It seems LinearLayout doesn't do anything with 'layout_alignParentRight' etc parameters though it does compile/build ok with these specified. So if you want to align a View within its parent ViewGroup instance, use RelativeLayout instead.


The downside of using RelativeLayout is that for each child View you have to remember to specify which other child View it is relative to and how (e.g. layout_toLeftOf, 'layout_below' etc ).

Android: how to make only part of a Textview string clickable

Say you have a string "Some text [clickable]" which you want to add to a TextView and you only want the "[clickable]" part of the string to be clickable. The way to go about it is to add the following code to your Activity class:

TextView myTextView = new TextView(this);
String myString = "Some text [clickable]";
int i1 = myString.indexOf("[");
int i2 = myString.indexOf("]");
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
myTextView.setText(myString, BufferType.SPANNABLE);
Spannable mySpannable = (Spannable)myTextView.getText();
ClickableSpan myClickableSpan = new ClickableSpan()
{
 @Override
 public void onClick(View widget) { /* do something */ }
};
mySpannable.setSpan(myClickableSpan, i1, i2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

If you wanted the clickable part of your TextView to open up a url by means of a WebViewActivity, for example, you would put the following code in the ClickableSpan.onClick method:


Intent i = new Intent(MyActivity.this, WebViewActivity.class);
i.putExtra("url", getResources().getString(R.string.myurl));
startActivity(i);

Thursday, 21 July 2011

Android app internet access

In order for an Android app to be allowed access to the internet, it must specify a request for this permission in its manifest, as follows:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Android: Referencing a library project

Just spent a frustrating hour(+) trying to figure out why my 'FlexStore' Android project couldn't find the classes of my 'KSD Android Utils' library project which was part of the same workspace. In the 'FlexStore' project Properties window, I had navigated to the 'Java Build Path' page and added the 'KSD Android Utils' project, and it was compiling fine. Turns out, what I had to do instead was navigate to the 'Android' page in the 'FlexStore' project Properties window and add the 'KSD Android Utils' library reference.


See the 'Managing Projects from Eclipse with ADT' android developers page for more info on this:
http://developer.android.com/guide/developing/projects/projects-eclipse.html

When Android debugger can't attach to the emulator...

If you select an app to debug from eclipse but instead of the app loading up on the emulator you get a message saying: "waiting for the debugger to attach"... best thing (I have found) to do is open up the windows command prompt, enter the command "adb kill-server", wait a second and then enter the command "adb devices". Your debugger at this point should attach to the emulator and your app should load up.

Tuesday, 19 July 2011

GWT: When the browser's scrollbars don't show...

... try adding the overflow:auto css property to your root view. For example,

(1) Define the .overflowAuto style in your styles.css file, as follows:
.overflowAuto { overflow:auto; }

(2) Declare it in your Styles.java file, as follows:
public String overflowAuto();

(3) Add it to your outermost HTMLPanel's styles in RootView.ui.xml, as follows:
<g:HTMLPanel addStyleNames="{res.styles.overflowAuto}">

That should work. The problem seems to arise when using LayoutPanels (and similar panels?) but haven't investigated it thoroughly so just an inkling.