Pages

Thursday, June 30, 2016

Android ListView With Social Networking Icon Example

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context=".MainActivity">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>
MainActivity.java
package in.kantapp.androidlistviewwithsocialnetworkingiconexample;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    ListView list;
    String[] title;
    String[] dis;

    int[] img = {R.drawable.bing, R.drawable.blog, R.drawable.drive,
            R.drawable.email, R.drawable.flikr, R.drawable.
            flipboard, R.drawable.goodread, R.drawable.google,};

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


        Resources res = getResources();

        title = res.getStringArray(R.array.Title);
        dis = res.getStringArray(R.array.Dis);
        list = (ListView) findViewById(R.id.listView);

        Adapter adapter = new adopter(this, title, dis, img);
        list.setAdapter((ListAdapter) adapter);
    }


}

class adopter extends ArrayAdapter<String> {


    private final Context c;
    private final String[] Title;
    private final String[] dis;
    private final int[] img;

    public adopter(Context c, String[] Title, String[] dis, int[] img) {

        super(c, R.layout.image, R.id.title, Title);
        this.c = c;
        this.Title = Title;
        this.dis = dis;
        this.img = img;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.image, parent, false);
        ImageView imgs = (ImageView) row.findViewById(R.id.imageView);
        TextView text = (TextView) row.findViewById(R.id.title);
        TextView diss = (TextView) row.findViewById(R.id.dis);

        imgs.setImageResource(img[position]);
        text.setText(Title[position]);
        diss.setText(dis[position]);
        return row;

    }
}
string.xml
<resources>
    <string name="app_name">Android ListView With Social Networking Icon Example</string>
    <string-array name="Title">
        <item>Bing</item>
        <item>Blog</item>
        <item>Drive</item>
        <item>Mail</item>
        <item>Myspace</item>
        <item>flipboard</item>
        <item>goodread</item>
    </string-array>
    <string-array name="Dis">
        <item>bing.com</item>
        <item>blogger.com</item>
        <item>drive.google.com</item>
        <item>mail.google.com</item>
        <item>myspace.com</item>
        <item>flipboard.com</item>
        <item>goodread.com</item>
    </string-array>
</resources>

Android Simple WebView Example kantapp.in

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>
MainActivity.java
package in.kantapp.simplewebviewkantappinexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    WebView Myweb;

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

        Myweb = (WebView) findViewById(R.id.webView);

        WebSettings webSettings = Myweb.getSettings();

        webSettings.setJavaScriptEnabled(true);


        Myweb.loadUrl("http://www.kantapp.in/");

        Myweb.setWebViewClient(new WebViewClient());

    }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.simplewebviewkantappinexample">
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Android Custom ListView With Image and Image Click Example

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="MainActivity">

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/beach"
        android:layout_centerHorizontal="true"
        android:src="@drawable/simle"
        android:layout_alignParentTop="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Click Anyware"
        android:id="@+id/textView2"
        android:textColor="#ffffff"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="74dp"
        android:layout_marginEnd="74dp"
        android:layout_marginTop="68dp" />
</RelativeLayout>
image_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="62dp"
        android:layout_height="62dp"
        android:id="@+id/imageView"
        android:src="@drawable/superman" />
<LinearLayout
    android:layout_width="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title"
        android:id="@+id/title" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Dis"
        android:id="@+id/textView" />
</LinearLayout>

</LinearLayout>
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/weblist" />
</LinearLayout>
MainActivity.java
package in.kantapp.androidlistviewwithimageclick;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton img = (ImageButton) findViewById(R.id.beach);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), Listviews.class));
            }
        });
    }
}
ListView.java
package in.kantapp.androidlistviewwithimageclick;


        import android.content.Context;
        import android.content.res.Resources;
        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.Adapter;
        import android.widget.ArrayAdapter;
        import android.widget.ImageView;
        import android.widget.ListAdapter;
        import android.widget.ListView;
        import android.widget.TextView;


/**
 * Created by Kant on 3/2/2016.
 */
public class Listviews extends MainActivity
{
    ListView list;
    String[] title;
    String[] dis;

    int[] img={R.drawable.reading,R.drawable.redlite,R.drawable.shy,R.drawable.superman};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        Resources res = getResources();

        title=res.getStringArray(R.array.Title);
        dis=res.getStringArray(R.array.dis);

        list=(ListView)findViewById(R.id.weblist);

        Adapter adapter=new adopter(this,title,dis,img);

        list.setAdapter((ListAdapter)adapter);
    }
}


class adopter extends ArrayAdapter<String>
{


    private final Context c;
    private final String[] Title;
    private final String[] dis;
    private final int[] img;

    public adopter(Context c,String[]Title,String[] dis,int[]img)
    {

        super(c,R.layout.image_text,R.id.title,Title);
        this.c=c;
        this.Title=Title;
        this.dis=dis;
        this.img=img;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater= (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);

        View row=inflater.inflate(R.layout.image_text,parent,false);
        ImageView imgs= (ImageView) row.findViewById(R.id.imageView);

        TextView text=(TextView)row.findViewById(R.id.title);
        TextView diss=(TextView)row.findViewById(R.id.textView);

        imgs.setImageResource(img[position]);
        text.setText(Title[position]);
        diss.setText(dis[position]);
        return row;

    }
}

string.xml
<resources>
    <string name="app_name">Android ListView with ImageClick</string>

    <string-array name="Title">

        <item>minion 1</item>
        <item>minion 2</item>
        <item>minion 3</item>
        <item>minion 4</item>
    </string-array>
    <string-array name="dis">

        <item>Writing</item>
        <item>Hello Hello</item>
        <item>How Are you</item>
        <item>Give Me That</item>
    </string-array>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.androidlistviewwithimageclick">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Listviews"/>
    </application>

</manifest>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3d65aa</color>
    <color name="colorPrimaryDark">#3d65aa</color>
    <color name="colorAccent">#FF4081</color>
</resources>

Android Toggle Example

activity_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="wrap_content"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton"
        android:checked="false" />

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="@string/toggle_turn_on"
        android:textOff="@string/toggle_turn_off"
        android:checked="true" />

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="72dp">

        <ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Off"
            android:id="@+id/ton"
            android:checked="false" />

        <ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/On"
            android:id="@+id/toff"
            android:checked="true" />

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

</LinearLayout>
MainAcivity.java
package in.kantapp.androidtoggleexample;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    private ToggleButton t1, t2, tb1, tb2;
    private Button b1, b2;

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

    public void onDisplayeClick() {
        t1 = (ToggleButton) findViewById(R.id.toggleButton1);
        t2 = (ToggleButton) findViewById(R.id.toggleButton2);
        b1 = (Button) findViewById(R.id.btnDisplay);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer result = new StringBuffer();
                result.append("toggleButton1 : ").append(t1.getText());
                result.append("\ntoggleButton2 : ").append(t2.getText());

                Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void onClick() {
        tb1 = (ToggleButton) findViewById(R.id.ton);
        tb2 = (ToggleButton) findViewById(R.id.toff);
        b2 = (Button) findViewById(R.id.click);

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer onoff = new StringBuffer();

                onoff.append("Toggle 1 is  :  ").append(tb1.getText());
                onoff.append("\nToggle 2 is:  ").append(tb2.getText());

                Toast.makeText(getApplicationContext(), onoff.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
string.xml
<resources>
    <string name="app_name">Android Toggle Example</string>
    <string name="toggle_turn_on">Turn On</string>
    <string name="toggle_turn_off">Turn Off</string>
    <string name="btn_display">Display</string>
    <string name="On">Light ON</string>
    <string name="Off">Light Off</string>
</resources>

Android Simple Spinner Example With Country Name

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/country_arrays"
        android:prompt="@string/country_prompt"
        android:spinnerMode="dialog" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Activity"
        android:id="@+id/next" />

</LinearLayout>
new_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

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

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spin"

            android:spinnerMode="dropdown"
            android:background="@android:color/holo_blue_bright" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Select"
            android:id="@+id/select" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/country_arrays"
            android:prompt="@string/country_prompt"
            android:layout_gravity="center_horizontal"
            android:spinnerMode="dropdown" />
    </LinearLayout>
</ScrollView>
MainActivity.java
package in.kantapp.androidspinnerexample;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private Spinner spinner1, spinner2;
    private Button btnSubmit, next;

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

        addItemsOnSpinner2();
        addListenerOnButton();
        addListenerOnSpinnerItemSelection();

        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), New.class));
            }
        });
    }

    // add items into spinner dynamically
    public void addItemsOnSpinner2() {

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        List<String> list = new ArrayList<String>();
        list.add("list 1");
        list.add("list 2");
        list.add("list 3");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(dataAdapter);
    }

    public void addListenerOnSpinnerItemSelection() {
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    }

    // get the selected dropdown list value
    public void addListenerOnButton() {

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getApplicationContext(),
                        "OnClickListener : " +
                                "\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()) +
                                "\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
                        Toast.LENGTH_SHORT).show();
            }

        });
    }

}
New.java
package in.kantapp.androidspinnerexample;

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

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kant on 3/6/2016.
 */
public class New extends Activity
{
    private Spinner spiner,spiner1;
    private Button b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_activity);
        onSpinnerClick();

    }
    public  void onSpinnerClick()
    {
       spiner=(Spinner)findViewById(R.id.spin);
        List<String> list=new ArrayList<>();
        list.add("USA");
        list.add("UK");
        list.add("UAE");
        list.add("IND");
        list.add("JAP");
        list.add("CHINA");

        ArrayAdapter<String> data= new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,list);
        spiner.setAdapter(data);

        b1=(Button)findViewById(R.id.select);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), String.valueOf(spiner.getSelectedItem()), Toast.LENGTH_SHORT).show();
            }
        });

    }



}
CustomOnItemSelectedListener.java
package in.kantapp.androidspinnerexample;

/**
 * Created by Kant on 3/6/2016.
 */
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

    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<?> arg0) {
        // TODO Auto-generated method stub
    }

}
string.xml
<resources>
    <string name="app_name">Android Spinner Example</string>
    <string name="country_prompt">Choose a country</string>

    <string-array name="country_arrays">
        <item>Malaysia</item>
        <item>United States</item>
        <item>Indonesia</item>
        <item>France</item>
        <item>Italy</item>
        <item>Singapore</item>
        <item>New Zealand</item>
        <item>India</item>
    </string-array>

</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.androidspinnerexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".New"/>
    </application>

</manifest>

Sunday, June 19, 2016

Android Swipe Tab With Fragment Style 2

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@color/accent_material_light"
        app:tabMode="fixed"
        app:tabGravity="fill"/>

    <android.support.v4.view.ViewPager
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/viewPager"
        android:layout_below="@+id/tabLayout"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
frag1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />
</LinearLayout>
frag2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>
MainActivity.java
package in.blogspot.kantapp.androidswipetabwithfragmentstyle2;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;


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

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(),getApplicationContext()));

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
        });

    }


    private class CustomAdapter extends FragmentPagerAdapter {

        private String fragments [] = {"Fragment 1","Fragment 2"};
        
        public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
            super(supportFragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment2();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragments[position];
        }
    }
}
Fragment1.java
package in.blogspot.kantapp.androidswipetabwithfragmentstyle2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by filip on 8/21/2015.
 */
public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.frag1,container,false);
    }
}
Fragment2.java
package in.blogspot.kantapp.androidswipetabwithfragmentstyle2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by filip on 8/21/2015.
 */
public class Fragment2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.frag2,container,false);
    }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.blogspot.kantapp.androidswipetabwithfragmentstyle2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
dependencies
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
}

Android Swipe Tab With Fragment Style 1

activity_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="horizontal">


    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context=".MainActivity">

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/tab_strip"
            android:layout_width="wrap_content"
            android:layout_height="100dp"


            android:background="#e62117"
            android:focusableInTouchMode="false">


        </android.support.v4.view.PagerTabStrip>


    </android.support.v4.view.ViewPager>


</LinearLayout>
tab1.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:weightSum="1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Tab 1"
        android:id="@+id/textView"
        android:textAlignment="center"
        android:layout_gravity="center_vertical"
        android:textIsSelectable="false"
        android:textSize="30dp"
        android:paddingLeft="150dp"


        />
</LinearLayout>
tab2.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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Tab 2"
        android:id="@+id/textView2"
        android:textAlignment="center"
        android:layout_gravity="center_vertical"
        android:textIsSelectable="false"
        android:textSize="30dp"
        android:paddingLeft="150dp"/>

</LinearLayout>
tab3.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">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Tab 3"
        android:id="@+id/textView2"
        android:textAlignment="center"
        android:layout_gravity="center_vertical"
        android:textIsSelectable="false"
        android:textSize="30dp"
        android:paddingLeft="150dp"/>


</LinearLayout>
MainActivity.java
package in.blogspot.kantapp.androidtablayoutwithswipeableviews;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;


public class MainActivity extends FragmentActivity {

    ViewPager pager;
    PagerTabStrip tab_strp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tab);
        ma_pager_adapter mapager = new ma_pager_adapter(getSupportFragmentManager());
        pager = (ViewPager) findViewById(R.id.pager);

        pager.setAdapter(mapager);
        tab_strp = (PagerTabStrip) findViewById(R.id.tab_strip);
        tab_strp.setTextColor(Color.WHITE);

    }


}
ma_pager_adapter.java
package in.blogspot.kantapp.androidtablayoutwithswipeableviews;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.Locale;

public class ma_pager_adapter extends FragmentPagerAdapter
{
    public ma_pager_adapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public Fragment getItem(int i) {

        switch (i) {
            case 0:
                tab1 t1 = new tab1();
                return t1;
            case 1:
                tab2 t2 = new tab2();
                return t2;
            case 2:
                tab3 t3 = new tab3();
                return t3;

        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }//set the number of tabs

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return "Top stories";
            case 1:

                return "Members";
            case 2:

                return "Setting";
        }
        return null;
    }


}
tab1.java
package in.blogspot.kantapp.androidtablayoutwithswipeableviews;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class tab1 extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab1, container, false);


        return view;
    }
}
tab2.java
package in.blogspot.kantapp.androidtablayoutwithswipeableviews;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class tab1 extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab2, container, false);


        return view;
    }
}
tab3.java
package in.blogspot.kantapp.androidtablayoutwithswipeableviews;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class tab1 extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab3, container, false);


        return view;
    }
}
AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.blogspot.kantapp.androidtablayoutwithswipeableviews">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thursday, June 9, 2016

Android Swipe Refresh Layout ( Android Pull-Swipe Down to Refresh)


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeToRefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </ListView>

    </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>
MAinActivity.java
package in.blogspot.kantapp.androidswiperefreshlayoutandroidpull_swipedowntorefresh;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ArrayList arrayList = new ArrayList();
    SwipeRefreshLayout mSwipeRefreshLayout;
    ListView mListView;

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

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeToRefresh);
        mListView = (ListView) findViewById(R.id.listView);

        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);

        arrayList.add("First Element");
        arrayList.add("Second Element");
        arrayList.add("Third Element");
        arrayList.add("Fourth Element");
        arrayList.add("Fifth Element");

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
        mListView.setAdapter(adapter);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                shuffle();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    public void shuffle(){
        Collections.shuffle(arrayList, new Random(System.currentTimeMillis()));
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
        mListView.setAdapter(adapter);
    }
}

Android ListView Using Custom Adapter And SQLite

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>
MainActivity.java
package in.blogspot.kantapp.androidlistviewusingcustomadapterandsqlite;

import java.util.ArrayList;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    // Out custom adapter
    MySimpleArrayAdapter adapter;

    // contains our listview items
    ArrayList<MyNotes> listItems;

    // database
    DatabaseHelper DatabaseHelper;

    // list of todo titles
    ArrayList<String> newData;

    // contains the id of the item we are about to delete
    public int deleteItem;

    // EditText field for adding new items to the list
    EditText editText2;

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

        // We're getting our listView by the id
        ListView listView = (ListView) findViewById(R.id.list);

        // Creating a new instance of our DatabaseHelper, which we've created
        // earlier
        DatabaseHelper = new DatabaseHelper(this);

        // This returns a list of all our current available notes
        listItems = DatabaseHelper.getAllNotes();

        newData = new ArrayList<String>();

        // Assigning the title to our global property so we can access it
        // later after certain actions (deleting/adding)
        for (MyNotes note : listItems) {
            newData.add(note.Title);
        }

        // We're initialising our custom adapter with all our data from the
        // database
        adapter = new MySimpleArrayAdapter(this, newData);

        // Assigning the adapter to ListView
        listView.setAdapter(adapter);

        // Assigning an event to the listview
        // This event will be used to delete records
        listView.setOnItemLongClickListener(myClickListener);
    }

    /**
     * This adapter will create your list view row by row
     */
    public class MySimpleArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final ArrayList<String> values;

        public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
            super(context, R.layout.rowlayout, values);

            this.context = context;
            this.values = values;
        }

        /**
         * Here we go and get our rowlayout.xml file and set the textview text.
         * This happens for every row in your listview.
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.rowlayout, parent, false);

            TextView textView = (TextView) rowView.findViewById(R.id.label);

            // Setting the text to display
            textView.setText(values.get(position));

            return rowView;
        }
    }

    /**
     * On a long click delete the selected item
     */
    public OnItemLongClickListener myClickListener = new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
            // Assigning the item position to our global variable
            // So we can access it within our AlertDialog below
            deleteItem = arg2;

            // Creating a new alert dialog to confirm the delete
            AlertDialog alert = new AlertDialog.Builder(arg1.getContext())
                    .setTitle("Delete " + listItems.get(deleteItem).Title)
                    .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                    // Retrieving the note from our listItems
                                    // property, which contains all notes from
                                    // our database
                                    MyNotes note = listItems.get(deleteItem);

                                    // Deleting it from the ArrayList<string>
                                    // property which is linked to our adapter
                                    newData.remove(deleteItem);

                                    // Deleting the note from our database
                                    DatabaseHelper.deleteNote(note.Id);

                                    // Tell the adapter to update the list view
                                    // with the latest changes
                                    adapter.notifyDataSetChanged();

                                    dialog.dismiss();
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                    // When you press cancel, just close the
                                    // dialog
                                    dialog.cancel();
                                }
                            }).show();

            return false;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    /**
     * This method is called when you press any button in your menu We've named
     * our add button action_add in our menu.xml file
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_add:
                showCreateNote();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * This simply shows a alert dialog asking for the todo text
     */
    public void showCreateNote() {
        // Creating a dynamical edittext for our alert dialog
        editText2 = new EditText(this);
        editText2.setId(9999);
        editText2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        editText2.setHint("Enter your note");

        AlertDialog alert = new AlertDialog.Builder(this)
                .setTitle("Create Note")
                .setView(editText2)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        if (editText2.getText().toString().length() > 0) {
                            // Adding the new todo text to our database
                            long Id = DatabaseHelper.addRecord(editText2
                                    .getText().toString());

                            // Create a new MyNotes object to add it to our
                            // global property listItems
                            MyNotes note = new MyNotes();
                            note.Id = (int) Id;
                            note.Title = editText2.getText().toString();

                            listItems.add(note);

                            newData.add(note.Title);

                            adapter.notifyDataSetChanged();
                        }

                        dialog.dismiss();
                        // This hides the android keyboard
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.toggleSoftInput(
                                InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                dialog.cancel();

                                // This hides the android keyboard
                                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                imm.toggleSoftInput(
                                        InputMethodManager.HIDE_IMPLICIT_ONLY,
                                        0);

                            }
                        }).show();

        // We are automatically focusing on the editText field
        if (editText2.requestFocus())
            ;
        {
            // This brings out the android keyboard as soon as we focus on the
            // editText area
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }
    }
}
DatabaseHelper.java
package in.blogspot.kantapp.androidlistviewusingcustomadapterandsqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;

public class DatabaseHelper extends SQLiteOpenHelper {

   private static final int DATABASE_VERSION = 1;
   private static final String DATABASE_NAME = "MyNotes";
   private static final String DATABASE_TABLE_NAME = "todo";
   public static final String COLID = "MyNotesID";
   public static final String COLTITLE = "Title";

   DatabaseHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
   }

   /**
    * This runs once after the installation and creates a database
    */
   @Override
   public void onCreate(SQLiteDatabase db) {
      // Here we are creating two columns in our database.
      // MyNotesID, which is the primary key and Title which will hold the
      // todo text
      db.execSQL("CREATE TABLE " + DATABASE_TABLE_NAME + " (" + COLID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLTITLE + " TEXT)");

   }

   /**
    * This would run after the user updates the app. This is in case you want
    * to modify the database
    */
   @Override
   public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
      // TODO Auto-generated method stub
   }

   /**
    * This method adds a record to the database. All we pass in is the todo
    * text
    */
   public long addRecord(String title) {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues cv = new ContentValues();
      cv.put(COLTITLE, title);

      return db.insert(DATABASE_TABLE_NAME, null, cv);
   }

   /**
    * //This method returns all notes from the database
    */
   public ArrayList<MyNotes> getAllNotes() {
      SQLiteDatabase db = this.getReadableDatabase();
      ArrayList<MyNotes> listItems = new ArrayList<MyNotes>();

      Cursor cursor = db.rawQuery("SELECT * from " + DATABASE_TABLE_NAME,
            new String[] {});

      if (cursor.moveToFirst()) {
         do {
            MyNotes note = new MyNotes();

            note.Id = cursor.getInt(cursor.getColumnIndex(COLID));
            
            note.Title = cursor.getString(cursor.getColumnIndex(COLTITLE));

            listItems.add(note);
         } while (cursor.moveToNext());
      }

      cursor.close();

      return listItems;
   }

   /*
    * //This method deletes a record from the database.
    */
   public void deleteNote(long id) {
      SQLiteDatabase db = this.getReadableDatabase();

      String string = String.valueOf(id);
      db.execSQL("DELETE FROM " + DATABASE_TABLE_NAME + " WHERE " + COLID
            + "=" + id + "");
   }
}
MyNotes.java
package in.blogspot.kantapp.androidlistviewusingcustomadapterandsqlite;

public class MyNotes {
   public int Id;
   public String Title;
}
manu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.ahotbrew.listviewsqlite.MainActivity" >

    <item android:id="@+id/action_add"
        android:showAsAction="always"
        android:title="Add"/>
</menu>
rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="35dp"
    android:layout_weight="1"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textSize="22sp" />

</LinearLayout>

Wednesday, June 8, 2016

Android Drop Down list Example

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/static_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp" />

    <Spinner
        android:id="@+id/dynamic_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
MainActivity.java
package in.blogspot.kantapp.androiddropdownlistexample;

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

public class MainActivity extends Activity {

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

        Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);

        // Create an ArrayAdapter using the string array and a default spinner
        ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
                .createFromResource(this, R.array.topMovies,
                        android.R.layout.simple_spinner_item);

        // Specify the layout to use when the list of choices appears
        staticAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // Apply the adapter to the spinner
        staticSpinner.setAdapter(staticAdapter);

        Spinner dynamicSpinner = (Spinner) findViewById(R.id.dynamic_spinner);

        String[] items = new String[]{"Milk Tea", "Green Tea", "Black Tea"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, items);

        dynamicSpinner.setAdapter(adapter);

        dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                Log.v("item", (String) parent.getItemAtPosition(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });
    }
}
string.xml
<resources>
    <string name="app_name">Android Drop Down list Example</string>
    <string-array name="topMovies">
        <item>The Dark Knight</item>
        <item>Batman Begins</item>
        <item>Superman </item>
        <item>Deadpool</item>
        <item>Guardians of the Galaxy</item>
        <item>Captain America: The Winter Soldier</item>
        <item>Hellboy</item>
        <item>Blade</item>
        <item>Ant-Man</item>
        <item>The Amazing Spider-Man </item>
    </string-array>
</resources>

Admission Form Android Example With PHP and MySQL

student_personal Table
DROP TABLE IF EXISTS `student_personal`;
CREATE TABLE IF NOT EXISTS `student_personal` (
 `reg_id` int(11) NOT NULL AUTO_INCREMENT,
 `first_name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `last_name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `DOB` date NOT NULL,
 `gender` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
 `blood_group` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `birth_place` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `pin` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
 `city` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `state` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `country` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `mother_tongue` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `Religion` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `DOJ` date NOT NULL,
 `Standard` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `Batch` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`reg_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;
student_personal.php
<?php

include "conn.php";

$fname=$_POST["fname"];
$lname=$_POST["lname"];
$dob=$_POST["dob"];
$sex=$_POST["sex"];
$blood=$_POST["blood"];
$bithplace=$_POST["bithplace"];
$pin=$_POST["pin"];
$city=$_POST["city"];
$state=$_POST["state"];
$country=$_POST["country"];
$mother_ton=$_POST["mother_ton"];
$religions=$_POST["religions"];
$doj=$_POST["doj"];
$course=$_POST["course"];
$batch=$_POST["batch"];


$sql="INSERT INTO `u179022344_db`.`student_personal` (`reg_id`, `first_name`, `last_name`, `DOB`, `gender`, `blood_group`, `birth_place`, `pin`, `city`, `state`, `country`, `mother_tongue`, `Religion`, `DOJ`, `Standard`, `Batch`) VALUES (NULL, '$fname', '$lname', '$dob', '$sex', '$blood', '$bithplace', '$pin', '$city', '$state', '$country', '$mother_ton', '$religions', '$doj', '$course', '$batch');";



if(!mysqli_query($conn,$sql))
{
 echo "fail";
}
else
{
 echo "success";
}
?>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context="kantapp.admission.MainActivity">

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

        <EditText
            android:id="@+id/fname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="First Name"
            android:inputType="textPersonName" />


        <EditText
            android:id="@+id/lname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Last Name"
            android:inputType="textPersonName" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/txt_dob"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingTop="10dp"
                android:text="YYYY-MM-DD (Date of Birth)"
                android:textColor="#000000"
                android:textSize="15dp" />

            <ImageView
                android:id="@+id/img_dob"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginLeft="10dp"
                android:onClick="onImageDOBClick"
                android:src="@drawable/calendar" />
        </LinearLayout>

        <RadioGroup
            android:id="@+id/radioSex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radioMale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male" />

            <RadioButton
                android:id="@+id/radioFemale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female" />

        </RadioGroup>


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"

                android:text="Blood Type :"
                android:textColor="#000000" />

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <EditText
            android:id="@+id/birthplace"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Birth Place"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/pin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Pin Code"
            android:inputType="number" />

        <EditText
            android:id="@+id/city"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="City"
            android:inputType="textPersonName" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="State :"

                android:textColor="#000000"
                android:textSize="15dp" />

            <Spinner
                android:id="@+id/state"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <EditText
            android:id="@+id/india"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="India" />

        <EditText
            android:id="@+id/mother_ton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Mother Tongue"
            android:inputType="textPersonName" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Religion :"

                android:textColor="#000000"
                android:textSize="15dp" />

            <Spinner
                android:id="@+id/religion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/txt_doj"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingTop="10dp"
                android:text="YYYY-MM-DD (Date of Join)"
                android:textColor="#000000"
                android:textSize="15dp" />

            <ImageView
                android:id="@+id/img_doj"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginLeft="10dp"
                android:onClick="onImageDOJClick"
                android:src="@drawable/calendar" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Standard :"

                android:textColor="#000000"
                android:textSize="15dp" />

            <Spinner
                android:id="@+id/standard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Batch :"

                android:textColor="#000000"
                android:textSize="15dp" />

            <Spinner
                android:id="@+id/batch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onSaveClick"
            android:text="Save" />
    </LinearLayout>


</ScrollView>
MainActivity.java
package kantapp.admission;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends Activity {

    RadioGroup radioSex;
    RadioButton gen;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onEditText();
        onBloodType();
        onState();
        onReligion();
        onBatch();
        onStandard();

    }

    EditText fname, lname,birth_place,pin,city,country,mother_ton;

    public void onEditText() {
        fname = (EditText) findViewById(R.id.fname);
        lname = (EditText) findViewById(R.id.lname);
        birth_place=(EditText)findViewById(R.id.birthplace);
        pin=(EditText)findViewById(R.id.pin);
        city=(EditText)findViewById(R.id.city);
        country=(EditText)findViewById(R.id.india);
        mother_ton=(EditText)findViewById(R.id.mother_ton);
    }

    TextView dob;

    public void onImageDOBClick(View view) {
        dob = (TextView) findViewById(R.id.txt_dob);
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(MainActivity.this, onDateSetListener, calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            int month = monthOfYear + 1;
            dob.setText(year + "-" + month + "-" + dayOfMonth);
        }
    };




    String[] bloodtype = {"O Positive", "A Positive", "B Positive", "AB Positive",
            "O Negative", "A Negative", "B Negative", "AB Negative"};
    Spinner blood;

    public void onBloodType() {

        blood = (Spinner) findViewById(R.id.spinner);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, bloodtype);
        blood.setAdapter(adapter);

        Toast.makeText(MainActivity.this, blood.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
    }

    //String[] abc={"A","B"};
    Spinner state;
    public void onState()
    {
        state=(Spinner)findViewById(R.id.state);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>
                (MainActivity.this,android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.india_states));

        state.setAdapter(adapter);
    }

    String[] religions={"Hinduism","Islam","Christianity","Sikhism","Buddhism","Jainism","Zoroastrianism","Others"};
    Spinner religion;
    public void onReligion()
    {
        religion=(Spinner)findViewById(R.id.religion);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>
                (MainActivity.this,android.R.layout.simple_spinner_item,religions);

        religion.setAdapter(adapter);
    }

    TextView doj;

    public void onImageDOJClick(View view) {
        doj = (TextView) findViewById(R.id.txt_doj);
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(MainActivity.this, d, calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            int month = monthOfYear + 1;
            doj.setText(year + "-" + month + "-" + dayOfMonth);
        }
    };

    String[] standards={"Class I","Class II","Class III","Class IV","Class V","Class VI","Class VII","Class VIII","Class IX","Class X","Class XI","Class XII"};
    Spinner standard;
    public void onStandard()
    {
        standard=(Spinner)findViewById(R.id.standard);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>
                (MainActivity.this,android.R.layout.simple_spinner_item,standards);

        standard.setAdapter(adapter);
    }

    String[] batchs={"A","B","C","D","E"};
    Spinner batch;
    public void onBatch()
    {
        batch=(Spinner)findViewById(R.id.batch);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>
                (MainActivity.this,android.R.layout.simple_spinner_item,batchs);

        batch.setAdapter(adapter);
    }

    public void onSaveClick(View view)
    {

        radioSex = (RadioGroup) findViewById(R.id.radioSex);
        int selectID = radioSex.getCheckedRadioButtonId();
        gen = (RadioButton) findViewById(selectID);

        String first_name=fname.getText().toString();
        String last_name=lname.getText().toString();
        String birthplace=birth_place.getText().toString();
        String pins=pin.getText().toString();
        String citys=city.getText().toString();
        String countrys=country.getText().toString();
        String mother_tons=mother_ton.getText().toString();

        String bloodz=blood.getSelectedItem().toString();
        String statez=state.getSelectedItem().toString();
        String religionz=religion.getSelectedItem().toString();
        String standardz=standard.getSelectedItem().toString();
        String batchz=batch.getSelectedItem().toString();

        String dobz=dob.getText().toString();
        String dojz=doj.getText().toString();
      String sex=gen.getText().toString();

        String method="personal";
        BackGroundTask backGroundTask=new BackGroundTask(this);
        backGroundTask.execute(method,first_name,last_name,dobz,sex,bloodz,birthplace,pins,citys,statez,countrys,mother_tons,dojz,standardz,batchz,religionz);


    }
}
BackGroundTask.java
package kantapp.admission;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

public class BackGroundTask extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;

    public BackGroundTask(Context context) {
        this.context = context;
    }

    @Override
    protected String doInBackground(String... params) {
        String method = params[0];

        if (method.equals("personal")) {
            try {
                String fname = params[1];
                String lname = params[2];
                String dob = params[3];
                String sex = params[4];
                String blood = params[5];
                String bithplace = params[6];
                String pin = params[7];
                String city = params[8];
                String state = params[9];
                String country = params[10];
                String mother_ton = params[11];

                String doj = params[12];
                String course = params[13];
                String batch = params[14];
                String religions = params[15];


                URL url = new URL("http://varahii.16mb.com/student/student_personal.php");

                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

                String Data = URLEncoder.encode("fname", "UTF-8") + "=" + URLEncoder.encode(fname, "UTF-8") + "&" +
                        URLEncoder.encode("lname", "UTF-8") + "=" + URLEncoder.encode(lname, "UTF-8") + "&" +
                        URLEncoder.encode("dob", "UTF-8") + "=" + URLEncoder.encode(dob, "UTF-8") + "&" +
                        URLEncoder.encode("sex", "UTF-8") + "=" + URLEncoder.encode(sex, "UTF-8") + "&" +
                        URLEncoder.encode("blood", "UTF-8") + "=" + URLEncoder.encode(blood, "UTF-8") + "&" +
                        URLEncoder.encode("bithplace", "UTF-8") + "=" + URLEncoder.encode(bithplace, "UTF-8") + "&" +
                        URLEncoder.encode("pin", "UTF-8") + "=" + URLEncoder.encode(pin, "UTF-8") + "&" +
                        URLEncoder.encode("city", "UTF-8") + "=" + URLEncoder.encode(city, "UTF-8") + "&" +
                        URLEncoder.encode("state", "UTF-8") + "=" + URLEncoder.encode(state, "UTF-8") + "&" +
                        URLEncoder.encode("country", "UTF-8") + "=" + URLEncoder.encode(country, "UTF-8") + "&" +
                        URLEncoder.encode("mother_ton", "UTF-8") + "=" + URLEncoder.encode(mother_ton, "UTF-8") + "&" +
                        URLEncoder.encode("religions", "UTF-8") + "=" + URLEncoder.encode(religions, "UTF-8") + "&" +
                        URLEncoder.encode("doj", "UTF-8") + "=" + URLEncoder.encode(doj, "UTF-8") + "&" +
                        URLEncoder.encode("course", "UTF-8") + "=" + URLEncoder.encode(course, "UTF-8") + "&" +
                        URLEncoder.encode("batch", "UTF-8") + "=" + URLEncoder.encode(batch, "UTF-8");

                bufferedWriter.write(Data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                InputStream inputStream = httpURLConnection.getInputStream();

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

                String response = "";
                String line = "";

                while ((line = bufferedReader.readLine()) != null) {
                    response += line;
                }

                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();

                return response;

            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Status");
    }

    @Override
    protected void onPostExecute(String s) {
        alertDialog.setMessage(s);
        if (s.equals("success")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    context.startActivity(new Intent(context.getApplicationContext(), Student_contact.class));
                }
            });

            alertDialog = builder.create();

        }
        alertDialog.show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

}
Student_contact.java
package kantapp.admission;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Student_contact extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student_contact);
    }
}
student_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context="kantapp.admission.Student_contact">


</ScrollView>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kantapp.admission">
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Student_contact"></activity>
    </application>

</manifest>
string.xml
<resources>
    <string name="app_name">Admission</string>
    <string-array name="india_states">
        <item>Select State</item>
        <item>Andaman and Nicobar Islands</item>
        <item>Andhra Pradesh</item>
        <item>Arunachal Pradesh</item>
        <item>Assam</item>
        <item>Bihar</item>
        <item>Chandigarh</item>
        <item>Chhattisgarh</item>
        <item>Dadra and Nagar Haveli</item>
        <item>Daman and Diu</item>
        <item>Delhi</item>
        <item>Goa</item>
        <item>Gujarat</item>
        <item>Haryana</item>
        <item>Himachal Pradesh</item>
        <item>Jammu and Kashmir</item>
        <item>Jharkhand</item>
        <item>Karnataka</item>
        <item>Kerala</item>
        <item>Lakshadweep</item>
        <item>Madhya Pradesh</item>
        <item>Maharashtra</item>
        <item>Manipur</item>
        <item>Meghalaya</item>
        <item>Mizoram</item>
        <item>Nagaland</item>
        <item>Orissa</item>
        <item>Pondicherry</item>
        <item>Punjab</item>
        <item>Rajasthan</item>
        <item>Sikkim</item>
        <item>Tamil Nadu</item>
        <item>Tripura</item>
        <item>Uttaranchal</item>
        <item>Uttar Pradesh</item>
        <item>West Bengal</item>
    </string-array>
</resources>