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.