Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Tuesday, December 6, 2011

Android Getting HTML from Webpages

Before you can access the internet you must enable internet access, add the line below to your AndroidManifest.xml file (I added mine under the minSdkVersion entry)
<uses-permission android:name="android.permission.INTERNET"/>
Now you can add this function to get the HTML of a website..
1:  public String getHTML(String urlToRead) {  
2:    URL url;  
3:    HttpURLConnection conn;  
4:    BufferedReader rd;  
5:    String line;  
6:    String result = "";  
7:    try {  
8:      url = new URL(urlToRead);  
9:      conn = (HttpURLConnection) url.openConnection();  
10:      conn.setRequestMethod("GET");  
11:      rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
12:      while ((line = rd.readLine()) != null) {  
13:       result += line;  
14:      }  
15:      rd.close();  
16:    } catch (Exception e) {  
17:      Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();  
18:    }  
19:    return result;  
20:   }  
And example usage..
String s = getHTML("http://lerietaylor.com/");       
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();

Tuesday, November 1, 2011

Using Android Checkboxes

In almost any settings window/screen you will probably need to use a check box, to "tick" something on or off. We are going to do some easy check boxes to help make a selection of what kind of fruit we would like to eat. Once the box is selected we will press a "save settings" button to show our selection.

I will assume you already know how to setup a project already. I will be (and have always been) using Eclipse as my IDE. We will have two screens, the first screen will have a button to allow us to move to our settings screen. The next screen is our settings screen, which will let us choose out fruit as mentioned earlier. Below is our first screen in graphical mode in our IDE.

Friday, July 15, 2011

Using String Resources

For quick string references, like a global string, you should try and use the string resource. You can set a string resource by opening the file in your project explorer located at values/strings.xml. In this file is where we will define our strings.

To manually enter a string (assuming your in Eclipse) double click the values/strings.xml file, when the new window opens at the bottom select "strings.xml". In here you can add your own strings in the following format: <string name="{a_name_here}">{you put the string's value in here}</string>

for example, I could store a URL for later use, it would look something like..

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_url_1">http://google.com/</string>
</resource>

We can reference that string in our code like this:

String s = getString(R.string.app_url_1);

Wednesday, April 27, 2011

Android Multiple Views

When I first started writing Android apps I never read much documentation as I am a hand on kind of guy. I always do this, I never read the documentation fully and always have to go back over my steps. It’s ok, I just learn slower eh? Well, the problem arose when I wanted to create multiple views. Creating multiple views doesn’t just consist or creating the XML and switching them via an OnClickListener under a button.

Creating Your First Android App

Today I am going to walk you through installing and setting up the Android SDK so you can create android applications. Android runs on Java, so if you don’t know Java, please step away now. If you want to try it anyway, keep following along. We first need to install JDK, the Java Development Kit. We will be installing JDK 5 which is the latest version as of writing this.