Showing posts with label Add Widgets. Show all posts
Showing posts with label Add Widgets. Show all posts

Add a ListView using ListActivity

How to Add a ListView using ListActivity

1. Add a ListView to XML file.

2. Set the id to android:id="@android:id/list" in the ListView.

3. Set android:drawSelectorOnTop = "fasle" in the ListView.

4. Now open the MainActivity.java file and add the below import to the imports section.

import android.widget.ListView;
import android.app.ListActivity;

5. You will need to extend the ListActivity in your MainActivity.java file. To do this
change this:

public class MainActivity extends Activity{

to this:

public class MainActivity extends ListActivity{

6. Add the line: EditText editText between the public class and the onCreate method.
Defining the editText variable here will make this available throughout the whole class.

public class MainActivity extends Activity {

EditText editText;

    public void onCreate(Bundle savedInstanceState) {

7. Add the bolded code below in the OnCreate method after setContentView and before the end of the method }. This code will link your editText in your main.xml file to the editText in code.

          editText = (EditText) findViewById(R.id.editText);

8. Run your app to test it. It should display an EditText.

9. Set the default text for the EditText by adding to the main.xml file EditText section, anywhere between the <EditText and the /> tags.

<EditText
...
    android:text="your text here"
...
/>

10. Compile and run!

How to Add a Static ListView to your Android Project

A "Static ListView" meaning that the items in the ListView will not be changed. For a ListView that you'd like to add items to, see "Dynamic ListView".

1. Add a Basic ListView.

2. Add an array of items and name it arrayTasks.
For testing, make the list "Task 1", Task 2", Task 3", "Task 4", "Task 5"

3. Create an ArrayAdapter named myAdapter.

4. Link your ArrayAdapter to your Array.

5. Link your ArrayAdapter to the ListView named myListView.

6. Compile and run!

How to Add a Toast

1. If you don't already have a project, create an Android Project.

2. Open MainActivity.java file and add this import:

    import android.widget.Toast;

3. There are several ways to make a toast, but this simple toast will get us started making Toasts. Add this to the MainActivity.java file in the onCreate method. This will display the message "This is a toast." The messsage MUST be a string. For example, it cannot be an integer such as 1.

Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show(); 

4. Compile and run!

Related Articles:
Solution for: error: no suitable method found for makeText

Add an EditText

How to add an EditText

1. Open your layout/main.xml file. Add this section of code between the <LinearLayout> tags.

<LinearLayout ..... >

        <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
                android:inputType = "text"
/>

</LinearLayout>

2. Compile and run!

Related Articles
Resources:
http://developer.android.com/training/keyboard-input/style.html

Add a WebView to your Android Project

How to Add a WebView

1. Open the res/layout/main.xml file or the file you would like to add the WebView to.

2. Add the following default WebView code to the file below the beginning LinearLayout and before the ending </LinearLayout>.

Example:
<LinearLayout ......>

   <WebView
            android:id="@+id/webView" 
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_gravity="left|center_vertical"/>

</LinearLayout>

3. Open the MainActivity.java file under the src/com directory.

4 Declare a class-level, WebView named webView.

5. Add Internet Permission to your Android Project

6. Add the bolded code below to the OnCreate. This code will link your webView in your main.xml file to the webView in code.
The  webView.loadUrl will load the google web site on startup.

          webView = (WebView) findViewById(R.id.webView);
          webView.loadUrl("http://www.google.com");


7. Run your app to test it. It should display a WebView with the Google page.



8. However, you will notice when you do a search for something one of two things will happen when you do     a search for something. Let's say you search for 'birds'.
          1. Your default browser on your device will open with the search contents.
          2. A pop-up box appears and asks which browser you would like to use.


 If you choose a browser, that browser will open and browsing will be through that one instead of your WebView. To keep the browsing experience in your WebView, you will need to add this line of code below.
Add the webView.getSettings line right before the webView.loadUrl line.

          webView.getSettings().setJavaScriptEnabled(true);
          webView.loadUrl("http://www.google.com");

9. Run and test your app again.
You should be able to search for 'birds' and the results show up in your own WebView.



Resources:
http://developer.android.com/guide/webapps/webview.html