Aug 28, 2011

Load local HTML from /assets

We can load local HTML (under /assets folder) in WebView.

Load local HTML from /assets

Modify from the former post Handle the Back button in WebView, to back in history.

Create our local HTML, /assets/myweb.html.
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>Android Coding</title>
</head>
<body>

<h1>Hello!</h1>
It's a local html in /assets.<br/>
Visit <a href="http://android-coding.blogspot.com/?m=1">Android Coding</a>

</body>
</html>





Load the local HTML in Java code:

final String DEFAULT_URL = "file:///android_asset/myweb.html";

webView.loadUrl(DEFAULT_URL);





Aug 26, 2011

Load ListView contents from XML resources

In the last posts A simple ListView using android.R.layout.simple_list_item_1 layout and ListView with your own layout; contents of ListView is defined in Java code. It can be defined in XML resources, and fill in ArrayAdapter by calling ArrayAdapter.createFromResource() method.

Create /res/values/month.xml file
<?xml version="1.0" encoding="utf-8"?>

<resources>
<string-array name="month">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
<item>October</item>
<item>November</item>
<item>December</item>
</string-array>
</resources>


Modify onCreate() to create ArrayAdapter from resource.
    @Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView)findViewById(R.id.list);

ArrayAdapter<CharSequence> adapter
= ArrayAdapter.createFromResource(this,
R.array.month,
R.layout.mylistlayout);

myList.setAdapter(adapter);

}





Related:

- ListView with multiple choice






Aug 25, 2011

ListView with your own layout

As stated in last post, A simple ListView using android.R.layout.simple_list_item_1 layout, we can copy and modify the built-in layout to implement our own layout in ListView.

ListView with your own layout

Copy the file android-sdk-linux_x86/platforms/android-7/data/res/layout/layout/simple_list_item_1.xml, rename mylistlayout.xml, and paste into /res/layout/ folder of your project. Modify it:
/res/layout/mylistlayout.xml
<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold|italic"
android:gravity="center_vertical|right"
android:paddingRight="30dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:drawableLeft="@drawable/icon"
/>


Modify the onCreate() method to use R.layout.mylistlayout in our ListView
    @Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView)findViewById(R.id.list);

ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
R.layout.mylistlayout,
listContent);
myList.setAdapter(adapter);

}


next:
- Load ListView contents from XML resources

A simple ListView using android.R.layout.simple_list_item_1 layout

It's a very simple and standard example of ListView, using the built-in layout android.R.layout.simple_list_item_1 layout.

A simple ListView using android.R.layout.simple_list_item_1 layout

package com.AndroidListView;


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

public class AndroidListViewActivity extends Activity {

ListView myList;

String[] listContent = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView)findViewById(R.id.list);

ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listContent);
myList.setAdapter(adapter);

}
}


<?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"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


But...what's android.R.layout.simple_list_item_1. You can check it in <android-sdk>/platforms/<android-version>/data/res/layout/simple_list_item_1.xml. Such that you can copy and modify it for your own.





For example, the content of android-sdk-linux_x86/platforms/android-7/data/res/layout/layout/simple_list_item_1.xml is:
<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright (C) 2006 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>






Related:

- Load ListView contents from XML resources

- ListView with multiple choice

- Simple GridView example




Aug 18, 2011

Handle the Back button in WebView, to back in history.

Normally, we will have a Back button on browser, for user to go back in history. The default behaviour of the BACK button on Android system is Exiting the app. In order to change it's behaviour, we can override the onKeyDown() method: if it's BACK button pressed AND there has a back history item, than go back; otherwise pass to super method to handle as normal.

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK){
if(webView.canGoBack()){
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}





Related Post:

- Display Progress Bar on WebView when loading




next:

- Load local HTML from /assets



related:
- Create your own London 2012 App



Infolinks In Text Ads