Tuesday, 29 September 2015

Spinner in Android

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.
activity_main.xml
-----------------------
<LinearLayout
   
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"
   
android:orientation="vertical">

    <
Spinner
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/spinner" />
</
LinearLayout>
text.xml
---------------------
<?xml version="1.0" encoding="utf-8"?>
<TextView
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:textSize="25dp"
   
android:layout_height="match_parent"
>

</
TextView
MainActivity.Java
------------------------
package com.android.anil.spinner;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    Spinner sp;

    String[] citys={"Hyderabad","Secundrabad","Chennai","Bengaluru","Mumbai","Kolkata","Delhi","Pune","Anantapur","Tadipatri"};



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(MainActivity.this,R.layout.text,citys);

        sp.setAdapter(arrayAdapter);

        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, "Selected City is : "+citys[position], Toast.LENGTH_SHORT).show();

            }


            @Override

            public void onNothingSelected(AdapterView<?> parent) {


            }

        });

    }

}
Output is:
------------------------




SimpleListView in Android

In a Android, ListView is arranges components or items  in a vertical scrollable list. If you have a long list of items that you simply want to show to the user, the ListView view is the answer. This example demonstrates the way to use the ListView to show a listing of items contained within an array.

activity_main.xml
--------------------------------
<LinearLayout
   
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:orientation="vertical"
   
tools:context=".MainActivity">


    <
ListView
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/listView"
       
android:divider="#ff554b"
       
android:dividerHeight="4dp" />
</
LinearLayout>
MainActivity.Java
----------------------------------
package com.android.anil.simplelistview1;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

    ListView list;

    String[] names={"Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10"};



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,names);

        list.setAdapter(arrayAdapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, "Name is : "+names, Toast.LENGTH_SHORT).show();

            }

        });



    }



}
Output is:
----------------------------------







Shared Preferences in Android

Android provides several storage options to store persistent data application data. The solution you choose depends on on your specific needs, such as whether tha data should be private to your application or accessible to other applicaitons (and the user) and how much space your data requires.

Data storage options in Android:


1.                 SharedPreferences: Store private primitive data in key-value pairs.
2.                 Internal Storage: Store private data on the device memory.
3.                 External Storage: Store public data on shared external storage.
4.                 SQLiteDatabase: Store structured data in private database.
5.                 Network Connection: Store data on the web with your own network server.
SharedPreferences: The SharedPreferences class provides general framework that allows you to save  and retreive persistent key-value pairs of primitive datatypes .

you can use SharedPreferences to save any primitive data : booleans, floats, ints, longs, Strings. The data will persist across user sessions (even your application kills).

To get SharedPrferences object for your application use the following method
getSharedPrefeneces()

Ex:
 SharedPreferences sp = getSharedPrefereces(getApplicationContext(),0);


-->call edit() to get a SharedPreferences.Editor to get writing permissions to store the data in SharedPreferences.

-->Add values with methods such as putBoolean(), putString(), putInt(), putLong() and putFloat()

-->commit the new values with commit()

To read values: To read values , use SharedPreferences methods such as getBoolean(), getString(), getInt(), getFloat() and getLong()


activity_main.xml
---------------------

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


    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText"
       
android:hint="enter name"
       
android:layout_marginTop="7dp" />

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText2"
       
android:hint="enter location"
       
android:layout_marginTop="7dp" />

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText3"
       
android:hint="enter email"
       
android:layout_marginTop="7dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Save"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="15dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Show"
       
android:id="@+id/button2"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="15dp" />
</
LinearLayout>
MainActivity.Java
------------------------
package com.android.anil.sharedprefrences;



import android.content.Intent;

import android.content.SharedPreferences;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

    Button savebtn,showbtn;

    EditText uname,uloc,uemail;

    String  name1,loc1,email1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        uname=(EditText)findViewById(R.id.editText);

        uloc=(EditText)findViewById(R.id.editText2);

        uemail=(EditText)findViewById(R.id.editText3);

        savebtn=(Button)findViewById(R.id.button);

        showbtn=(Button)findViewById(R.id.button2);

        savebtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                name1=uname.getText().toString();

                loc1=uloc.getText().toString();

                email1=uemail.getText().toString();

                SharedPreferences sp=getSharedPreferences("pref",MODE_PRIVATE);

                SharedPreferences.Editor editor=sp.edit();

                //Storing the data from sharedpreferences

                editor.putString("name",name1);

                editor.putString("loc",loc1);

                editor.putString("email",email1);

                editor.commit();

                Toast.makeText(MainActivity.this, "Values are saved", Toast.LENGTH_SHORT).show();



            }

        });

        showbtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                startActivity(new Intent(getApplicationContext(),ReceivingActivity.class));

            }

        });



    }

}
activity_receiving.xml
-------------------------------
<LinearLayout     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="com.android.anil.sharedprefrences.ReceivingActivity"     android:orientation="vertical">     <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"         android:layout_marginTop="10dp" />     <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textAppearance="?android:attr/textAppearanceLarge"         android:text="Large Text"         android:id="@+id/textView2"         android:layout_gravity="center_horizontal"         android:layout_marginTop="10dp" />     <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textAppearance="?android:attr/textAppearanceLarge"         android:text="Large Text"         android:id="@+id/textView3"         android:layout_gravity="center_horizontal"         android:layout_marginTop="10dp" /> </LinearLayout>
ReceivingActivity.Java
-----------------------------
package com.android.anil.sharedprefrences;



import android.content.SharedPreferences;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;



public class ReceivingActivity extends AppCompatActivity {

    TextView t1,t2,t3;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_receiving);

        t1=(TextView)findViewById(R.id.textView);

        t2=(TextView)findViewById(R.id.textView2);

        t3=(TextView)findViewById(R.id.textView3);

        SharedPreferences sp=getSharedPreferences("pref",MODE_PRIVATE);

        //Retriving the data from sharedprefrences

        String s1=sp.getString("name",null);

        String s2=sp.getString("loc",null);

        String s3=sp.getString("email",null);

        t1.setText(s1);

        t2.setText(s2);

        t3.setText(s3);

    }



}
Output is:
-----------------------------











Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...