Friday, 19 December 2014

Android AutoCompleteEditText - j4android

I will show how to create AutoCompleteTextView  and MultiAutoCompleteTextView in android programmatically.

Please try it your self.

package com.j4android;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity{
private AutoCompleteTextView autoComplete;
private MultiAutoCompleteTextView multiAutoComplete;
private ArrayAdapter<String> adapter;
String[] srt_array = { "arjun", "anbu", "anandh", "babu", "balu", "balaji",
"cinna", "dinesh", "darvin", "edward", "edvin", "faishal", "guru",
"ganesh", "harish", "hariharan", "sasi", "varun", "yamini" };
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);

      // get the defined string-array
String[] colors = getResources().getStringArray(R.array.colorList);

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

autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete);

// set adapter for the auto complete fields
autoComplete.setAdapter(adapter);
multiAutoComplete.setAdapter(adapter);

// specify the minimum type of characters before drop-down list is shown
autoComplete.setThreshold(1);
multiAutoComplete.setThreshold(2);
// comma to separate the different colors
multiAutoComplete
.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

// when the user clicks an item of the drop-down list
multiAutoComplete.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),
"" + arg0.getItemAtPosition(arg2), Toast.LENGTH_LONG)
.show();
}
});
}

}

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <AutoCompleteTextView
        android:id="@+id/autoComplete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_marginTop="20dp"
        android:ems="8" />
<MultiAutoCompleteTextView
        android:id="@+id/multiAutoComplete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text2"
        android:layout_marginTop="20dp"
        android:ems="13" />


</LinearLayout>

Thursday, 18 December 2014

Android WebView example - j4android

I will show simple code how to load a url or website inside to webview in android.


Please try it your self.
package com.j4android;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
WebView webview ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webView1);
webViewCallingMethod();
}

private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

public void webViewCallingMethod() {
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
}
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>