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>



Sunday, 23 November 2014

Android - Spinner Dropdown Example

I will show simple code how to create  Spinner (drop down list) in android programmatically.
Please try it your self.

package com.j4android;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner spinner1;
ArrayAdapter spinnerArrayAdapter1;
ArrayList<String> results = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
results.add("India");
results.add("Singapore");
results.add("US");
results.add("UK");

spinner1 = (Spinner) findViewById(R.id.spinner1);
spinnerArrayAdapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, results);
spinnerArrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner1.setAdapter(spinnerArrayAdapter1);
                spinner1.setOnItemSelectedListener(this);
}
        public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
   Toast.makeText(parent.getContext(), 
       "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
       Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub


}

}


main.xml:

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

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />


</LinearLayout>

Thursday, 20 November 2014

Android - Custom Toast

I will show sample code how to create  Custom Toast in android programmatically.

Please try it your self.

package com.j4android;


import android.app.Activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
Button toastButton;

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
toastButton = (Button) findViewById(R.id.toast_btn);
toastButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Custom Toast");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});

}
}

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:background="@drawable/button_bg_gradient"
    android:orientation="vertical" >

    <Button
        android:id="@+id/toast_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click" />

</LinearLayout>


custom_toast.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff0000"
    android:padding="8dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF" />


</LinearLayout>

Wednesday, 12 November 2014

Android - Draw Horizontal line in xml

I will show you how to draw horizontal line in android layout design in xml. Its very simple to make a line using xml.

Please try it your self.

Just write this in your xml layout, this will create horizontal line, 
and also change desire color in it.

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#e2e2e2"
    android:paddingTop="17dp" />

Thanks.

Friday, 7 November 2014

Android - How to change the fontFamily on the TextView

I will show you how to change fontfamily and include external fontfamily  for textview, edittext and all the string show in the screen.

Please try it your self.

package com.j4android;


import android.app.Activity;                     
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity{
TextView text1, text2, text3; @Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);

                text1 = (TextView) findViewById(R.id.textView1);
                text2 = (TextView) findViewById(R.id.textView2);
        text3 = (TextView) findViewById(R.id.textView3);
     

        Typeface tf1 = Typeface.createFromAsset(getAssets(),
"fonts/albr65w.ttf");
        Typeface tf2 = Typeface.createFromAsset(getAssets(),
"fonts/Roboto-Medium.ttf");
        Typeface tf3 = Typeface.createFromAsset(getAssets(),
"fonts/Roboto-Italic.ttf");


                 text1.setTypeface(tf1);
         text2.setTypeface(tf2);

         text3.setTypeface(tf3);
           
       }


}


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" >

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

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

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


</LinearLayout>

Note:
Sample font :
    - albr65w.ttf
    - Roboto-Italic.ttf 
    - Roboto-Medium.ttf
Create folder "fonts" inside assets and put the above font into fonts. 

Thursday, 16 October 2014

Android - Repeat a thread with a time delay

I will show sample code how to create repeated a task with a time delay in android programmatically.

Please try it yourself

package com.j4android;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
PopupWindow popupWindow;
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
repeatedCallMethod();
}

public void repeatedCallMethod() {
TimerTask repeatedProcess;
final Handler handler = new Handler();
Timer timer = new Timer();
repeatedProcess = new TimerTask() {
int count = 0;
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
System.out.println("Process" + count);
count++;
} catch (Exception e) {

}
}
});
}
};
timer.schedule(repeatedProcess, 0, 5000);
}

}

Saturday, 11 October 2014

Android - How to show pop up window in android programmatically

I will show sample code to create a popup window in Android programmatically. popup window can be used to display an arbitrary view

Please try it your self.
package com.j4android;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;

public class PopupWindowActivity extends Activity {
PopupWindow popupWindow;
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);

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

popupBttn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
popupSettings();
}
});

}
private void popupSettings() {
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popup = inflater.inflate(R.layout.popup_xml, null);
popupWindow = new PopupWindow(popup,android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(popup, Gravity.BOTTOM, 0, 0);// Gravity.TOP etc.,
               

}
}

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >  
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click to show PopupWindow" />

</LinearLayout>

note:
don't forget to create desire layout in popup.xml.



Friday, 10 October 2014

Android - how to check internet connection programmatically

I will show simple code to check internet connection status in android device programmatically.

Please try it your self.

package com.j4android;

import android.app.Activity;                     

import android.os.Bundle;

public class MainActivity extends Activity{
Boolean isInternetPresent = false;
ConnectionDetector cd;
    @Override
    public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
cd = new ConnectionDetector(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
Toast.makeText(getApplicationContext(), isInternetPresent+"",
Toast.LENGTH_SHORT).show();
//Do something here
} else {
Toast.makeText(getApplicationContext(), isInternetPresent+"",
Toast.LENGTH_SHORT).show();
}              
    }

}

ConnectionDetector.java


package com.j4android;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context) {
this._context = context;
}

public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}

}

Don't forget to add these two permissions in the manifest file.


<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

Wednesday, 8 October 2014

Splash screen - Android Example

I will show How to make a simple splash screen, it's an activity that will show for set time when your application is starting and after set time period return to application main screen.

Please try it your self.

package com.j4android;


import android.app.Activity;                     

import android.os.Bundle;

public class SplashActivity extends Activity{

@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
SplashActivity.this.finish();
Intent i=new Intent(this,MainActivity.class);
           }
}, 4000);
           
       }
}



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:background="#fff000"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="SplashScreen"
            android:textColor="#000"
            android:textSize="40dp"
            android:textStyle="bold" />
    </LinearLayout>


</LinearLayout>