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. A 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>