• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 4 comments

Android – Simple AutoCompleteTextView Example

Source main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<AutoCompleteTextView
android:id="@+id/autocompletetextview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</AutoCompleteTextView>

</LinearLayout>


Source AutoCompleteTextViewExampleActivity.java

package com.actvex;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class AutoCompleteTextViewExampleActivity extends Activity {

    AutoCompleteTextView autocompletetextview;

    String[] array = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
            "Eight", "Nine", "Ten" };


    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        autocompletetextview = (AutoCompleteTextView)     findViewById(R.id.autocompletetextview);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.select_dialog_item,
array);

        autocompletetextview.setThreshold(1);

        autocompletetextview.setAdapter(adapter);


    }
}


The Output will be







Posted by Jack - - 1 comments


Android – Selector on Button Example

Source button.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />

<item android:drawable="@drawable/button_normal"
android:state_focused="true" />

<item android:drawable="@drawable/button_normal" />

</selector>


Source main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="40dp"
android:background="@drawable/button"
android:textColor="#ffffff"
android:text="Button" />

</LinearLayout>

The output will be

 


Posted by Jack - - 0 comments

SET CUSTOM FONT IN TEXTVIEW

TextView textView1 = (TextView) findViewById(R.id.textView1);

Typeface font = Typeface.createFromAsset(getAssets(),
                "Doonga Slash.ttf");
       
textView1.setTypeface(font);

Posted by Jack - - 2 comments


Searching the elements of a String array, starts with SEARCHTEXT (String)

public static ArrayList<String> searchFromStart(String[] inputArray, String searchText) {

            ArrayList<String> outputArray = new ArrayList<String>();

            for (int i = 0; i < inputArray.length; i++) {

                        if (searchText.compareToIgnoreCase(inputArray[i].substring(0,
                                                searchText.length())) == 0) {

                                    outputArray.add(inputArray[i]);

                        }

            }

            return outputArray;
}

Posted by Jack - - 19 comments


Android – Accessing Webservices using SOAP Example

Source main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Celsius to Farenheit" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width=" fill_parent"
        android:layout_height="wrap_content"
        android:numeric="integer"
        android:singleLine="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Convert to Farenheit" />


    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

Source WebserviceActivity.java

package com.webserviceexample;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WebserviceActivity extends Activity {

            private final String NAMESPACE = "http://tempuri.org/";
            private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
            private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
            private final String METHOD_NAME = "CelsiusToFahrenheit";

            Button b;

            TextView tv;

            EditText et;

            public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);

                        et = (EditText) findViewById(R.id.editText1);

                        tv = (TextView) findViewById(R.id.tv_result);

                        b = (Button) findViewById(R.id.button1);

                        b.setOnClickListener(new OnClickListener() {
                                    public void onClick(View v) {

                                                String result = getFahrenheit(et.getText().toString());

                                                tv.setText(result+"° F");
                                    }
                        });

            }

            public String getFahrenheit(String celsius) {

                        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                        PropertyInfo celsiusPI = new PropertyInfo();
                        celsiusPI.setName("Celsius");
                        celsiusPI.setValue(celsius);
                        celsiusPI.setType(double.class);
                        request.addProperty(celsiusPI);

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                                                SoapEnvelope.VER11);
                        envelope.dotNet = true;
                        envelope.setOutputSoapObject(request);
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                        try {
                                    androidHttpTransport.call(SOAP_ACTION, envelope);
                                    SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                                    Log.i("Webservice Output", response.toString());

                                    return response.toString();

                        } catch (Exception e) {
                                    e.printStackTrace();
                        }

                        return null;
            }
}

Note:
·         Add Internet Permission in your AndroidManifest.xml file.
·         Download ksoap2.jar and add to buildpath.

The output will be