Thursday, 27 August 2015

Android Parallax effect in listview


   Here I am going to explain how to create Listview header parallax in android.

                                  


Step 1).Mainactivity.java

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

    private TextView stickyView;
    private ListView listView;
    private View imageView;
    private View stickyViewSpacer;
    private int ROWS = 20;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView= (ListView) findViewById(R.id.listView);
        imageView= findViewById(R.id.imageView);
        stickyView= (TextView) findViewById(R.id.stickyView);
        LayoutInflater inflater = (LayoutInflater)
                          getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View listHeader = inflater.inflate(R.layout.list_header, null);
        stickyViewSpacer = listHeader.findViewById(R.id.stickyViewPlaceholder);
        listView.addHeaderView(listHeader);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
       @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            }
        @Override
         public void onScroll(AbsListView view, int firstVisibleItem, int                                                                           visibleItemCount,int totalItemCount)
                  {
                    if (listView.getFirstVisiblePosition() == 0) {
                    View firstChild listView.getChildAt(0);
                    int topY = 0;
                    if (firstChild != null) {
                        topY firstChild.getTop();
                    }

                    int heroTopY stickyViewSpacer.getTop();
                    stickyView.setY(Math.max(0heroTopY topY));
                    imageView.setY(topY * 0.5f);
                }
            }
        });

        List<String> modelList = new ArrayList<>();
        for (int i = 0; i < ROWS; i++) {
            modelList.add("List item " + i);
        }

       ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_row,modelList);                                                                                 listView.setAdapter(adapter );
    }

Step 2).activty_main.xml


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@drawable/image1"
        android:scaleType="fitCenter" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#9E9E9E"
        android:dividerHeight="1dp"
        android:scrollbars="none"/>

    <TextView
        android:id="@+id/stickyView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#222"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:text="AndroidDhina"
        android:textColor="#fff"
        android:textSize="20sp"
        android:textStyle="bold" />
</FrameLayout>


Step 3).list_header.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">

    <Space
        android:layout_width="match_parent"
        android:layout_height="250dp" />

    <Space
        android:id="@+id/stickyViewPlaceholder"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

</LinearLayout>


Step 4).list_row.xml

 <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fafafa"
    android:gravity="center_vertical"
    android:minHeight="40dp"
    android:padding="8dp"
    android:textColor="#222" />


Step 5).gradle
 apply plugin: 'com.android.application'

 android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.androiddhina.parallaxlistview"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                                       'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}



Happy Coding:)



Tuesday, 25 August 2015

Android get list of Countries in Spinner



        Here I am going to explain how to get the list  of Countries in Spinner


                        



  Step 1).group.xml

    
 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#E6E6E6" />

    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />

</shape>


Step 2).activity.xml


<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#7faaff" >

<RelativeLayout 

  android:layout_width="match_parent"
  android:layout_height="124dp"
  android:layout_centerInParent="true"
  android:layout_margin="20dp"
  android:layout_marginTop="30dp"
  android:background="@drawable/group" >

<EditText
  android:id="@+id/editText1"
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:layout_alignParentLeft="true"
  android:layout_alignParentRight="true"
  android:background="@null"
  android:ems="10"
  android:hint="Name"
  android:imeOptions="actionNext"
  android:padding="10dp"
  android:singleLine="true" >
</EditText>

<View

  android:id="@+id/view1"
  android:layout_width="fill_parent"
  android:layout_height="1dp"
  android:layout_below="@+id/editText1"
  android:background="#c0c0c0" />

<EditText

  android:id="@+id/editText2"
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:layout_below="@+id/view1"
  android:background="@null"
  android:ems="10"
  android:hint="State"
  android:imeOptions="actionNext"
  android:padding="10dp"
  android:singleLine="true" >
</EditText>

<View
  android:id="@+id/view2"
  android:layout_width="fill_parent"
  android:layout_height="1dp"
  android:layout_below="@+id/editText2"
  android:background="#c0c0c0" />

<EditText

  android:id="@+id/editText3"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:layout_alignRight="@+id/view2"
  android:layout_below="@+id/view2"
  android:background="@null">
   
</EditText>
</RelativeLayout >

</RelativeLayout >

  Step 3).MainActivity.java

 import java.util.ArrayList;
 import java.util.Locale;
 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;
 import android.widget.Spinner;

public class MainActivity extends Activity {
Spinner country;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
country= (Spinner) findViewById(R.id.editText3);
ArrayList<String> list = new ArrayList<String>();

String[] locales = Locale.getISOCountries();

for (String countryCode : locales) {

Locale obj = new Locale("", countryCode);
                        list .add(obj.getDisplayCountry());

}

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
        dataAdapter .setDropDownViewResource
                                    (android.R.layout.simple_spinner_dropdown_item);
       country.setAdapter(dataAdapter );
}

}

Happy Coding:)

         

Saturday, 22 August 2015

Android example for custom grouped Edittext


  Here I am going to explain example for custom grouped edittext for android like an iphone


                            

     

Step 1).group.xml
    
 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#E6E6E6" />

    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />

</shape>


Step 2).activity.xml


<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#7faaff" >

<RelativeLayout 
  android:layout_width="match_parent"
  android:layout_height="124dp"
  android:layout_centerInParent="true"
  android:layout_margin="20dp"
  android:layout_marginTop="30dp"
  android:background="@drawable/group" >

<EditText
  android:id="@+id/editText1"
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:layout_alignParentLeft="true"
  android:layout_alignParentRight="true"
  android:background="@null"
  android:ems="10"
  android:hint="Name"
  android:imeOptions="actionNext"
  android:padding="10dp"
  android:singleLine="true" >
</EditText>

<View
  android:id="@+id/view1"
  android:layout_width="fill_parent"
  android:layout_height="1dp"
  android:layout_below="@+id/editText1"
  android:background="#c0c0c0" />

<EditText
  android:id="@+id/editText2"
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:layout_below="@+id/view1"
  android:background="@null"
  android:ems="10"
  android:hint="State"
  android:imeOptions="actionNext"
  android:padding="10dp"
  android:singleLine="true" >
</EditText>

<View
  android:id="@+id/view2"
  android:layout_width="fill_parent"
  android:layout_height="1dp"
  android:layout_below="@+id/editText2"
  android:background="#c0c0c0" />

<EditText
  android:id="@+id/editText3"
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:layout_alignRight="@+id/view2"
  android:layout_below="@+id/view2"
  android:background="@null"
  android:ems="10"
  android:hint="Country"
  android:imeOptions="actionDone"
  android:padding="10dp"
  android:singleLine="true" >
</EditText>
</RelativeLayout >


</RelativeLayout >

Happy Coding:)
         

Tuesday, 11 August 2015

Android Snackbar Example


Before going to the example, first we have to know about  CoordinatorLayout and SnackBars


SnackBars

          Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.
They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity. Snackbars can be swiped off screen.

CoordinatorLayout 
        The main function of a coordinatorlayout is to act as a container and perform specific interactions between child views. Like shifting up of floating action button when an Android snackbar appears.

Step 1)activity_main.xml

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:text="Mark as delete"/>
    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/abc_ic_menu_cut_mtrl_alpha"
        app:fabSize="normal"
        app:layout_anchor="@id/coordinatorLayout"
        app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>


Step 2)Mainactivity.java


package com.as0060.snackbar;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity 
    {
    View.OnClickListener mOnClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
       {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) 
                                                                        findViewById(R.id .coordinatorLayout);
        final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener
                                                 (new CompoundButton.OnCheckedChangeListener() 
         {
        @Override
        public void onCheckedChanged(CompoundButtonbuttonView,booleanisChecked)  {
                if (isChecked
                  {
                    Snackbar snackbar = Snackbar
                            .make(coordinatorLayout, "I AM DHINA", Snackbar.LENGTH_LONG)
                            .setAction("Undo", mOnClickListener);
                             snackbar.setActionTextColor(Color.RED);
                             View snackbarView = snackbar.getView();
                             snackbarView.setBackgroundColor(Color.DKGRAY);
                             TextView textView = (TextView)snackbarView.findViewById                                                                          (android.support.design.R.id.snackbar_text);
                             textView.setTextColor(Color.YELLOW);
                             snackbar.show();
                }
            }
        });

        mOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkBox.setChecked(false);
            }
        };
    }

}


Step 3)build.gradle(Module:app)


apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.as0060.snackbar"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:design:22.2.0'

}


Happy Coding:)