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>