Saturday, 11 October 2014

ListView OnItemClickListner

Yes, in this article we did two things,

1. Adding Items to ListView
2. Register ListView to event(OnItemClickListner) to find which item is clicked.



Adding Items to ListView

In Add(onclick) adding items to Listview at runtime using ArrayList, ArrayAdapter of string type.

ArrayAdapter will take String[] as a parameter as a source. so i taken arraylist(al). arraylist can take string item.

String item=edt.getText().toString().trim();
al.add(item); then adapter will be updated automatically using predefined event

adapter.notifyDataSetChanged();   at onCreate method it taken al as input/source.

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

Register OnItemClickListener

import android.widget.AdapterView.OnItemClickListener;

MainActivity implement OnItemClickListerner also. which fires whenever an item is clicked on ListView.

this event has 4 parameters.

onItemClick(AdapterView<?> parent, View view, int position,long id);

parent  :  is Listview,Gridview,spinner,...ect who currently binding Adapterview data.

as well we can check (Listview1, Listview2..also)

view :   view with in the AdapterView.  

position :  item position in the adapterview.

args3   : the Row id of the item was clicked.

ListView Databinding, add remove items

yes, we know Listview a component to bind list of items, if items are more it will show items in scrollview behaviour.

in this demo: we added 2 string-array files(awords,bwords). awords added at design time, and bwords are added at runtime(button click--Bind items).

blistwords items are,we can't get at class declaration time because bwords are not available resources are loaded. so added that onclick event.

public class MainActivity extends Activity implements OnClickListener,OnItemClickListener
{
    Button addbtn,delbtn;
    ListView listview;
    EditText edt;
    ArrayList<String> al;
    ArrayAdapter<String> arrayadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt=(EditText)findViewById(R.id.editText1);
listview=(ListView)findViewById(R.id.listView1);
addbtn=(Button)findViewById(R.id.button1);
delbtn=(Button)findViewById(R.id.button2);
al=new ArrayList<String>();
arrayadapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,al);
listview.setAdapter(arrayadapter);
addbtn.setOnClickListener(this);
delbtn.setOnClickListener(this);
listview.setOnItemClickListener(this);
}
Add and remove items

public void onClick(View v)
{
if(v==addbtn)
{
String item=edt.getText().toString().trim();
if(item.length()>0)
{
al.add(item);
arrayadapter.notifyDataSetChanged();
edt.setText("");
edt.requestFocus();
}
}
if(v==delbtn)
{
String item=edt.getText().toString().trim();
if(item.length()>0)
{
al.remove(item);
arrayadapter.notifyDataSetChanged();
edt.setText("");
edt.requestFocus();
}
}
}
AdapterView event : this event supports of listview, spinner...ect

public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
TextView tv=(TextView)view;
String item=tv.getText().toString();
edt.setText(item);
}

Sunday, 28 September 2014

AutoCompleteTextView in android

yes, AutocompleteTextview is a view component which is not available in our Palette controls. directly we have to write in our layout .xml file. inside Relatielayout of activity_main.xml

 <AutoCompleteTextView
        android:id="@+id/AutoCompleteView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
         android:completionThreshold="3"
 </AutoCompleteTextView>

here we given threshold at design time itself,  or runtime also set this threshold value.






 AutoCompleteTextview : is an EditText with autocomplete funcationality.  this is achived using Adapter.adapter acts a bridge between edittext and underlaying data/values. adapter is responsible for view each item in that dataset.

1st way i utilized predefined   android.R.layout.simple_list_item_1  Style for showing items in Autocompletetextview.

2nd way i designed one layout file as Root element is TextView---mystyle.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:textColor="#f00"--------->red color so that values(india,indonisa) showing in red color.
    android:textSize="16sp"
    android:padding="12dp" >
</TextView>

Externalizing values :    it defines a file with resources as an string-array named as countries.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
      <string-array name="country">
        <item>India</item>
        <item>Indonesia</item>
        <item>Iran</item>
        <item>Iraq</item>
        <item>Ireland</item>
        <item>Poland</item>
        <item>Pakistan</item>
        <item>Peru</item>      
    </string-array>
</resources>

Thursday, 25 September 2014

Resources in Android application

yes, in andriod application we have nice project structure to organize the project with the help of speration/extranalization as resources in the project in res folder.

based on resource type : strings, images,color, menu,styles ..has their respective folders like Values,Drawable,Colors,Menu...are predefined structures should maintain. buf finally we called all of them as resources.



as well extranalizating resoures and alternative resources are supports specific
device configurations,
different languages,
based screen sizes...ect.

ColorStateList is a list  resouce :

    clolorstatelist is an object we can define as an mycolor.xml that can apply a color to a viewobject, so that depending on the state/behaviour of the view object the color gets changed on the view componet.

we can  apply color to the view component in 2 ways

1. design time                 2.runtime

any of the above we need to create an xml which is type Color List.

select Android Project(ColorsProject)--New --Other--New android xml file----

Type of Resouce  : color List
File                       : mycolors
Root Element       : selector

then as mycolors.xml is created with selector as root element. now we added two colors with two behaviours(pressed,checked).

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#0f0"/>
    <item android:state_checked="true" android:color="#00f" />
    <item android:color="#f00"/>

</selector>

at design time:

 <Button
        android:id="@+id/button1"
        android:textColor="@color/mycolors"></Button>



at runtime/programatic time:

public class MainActivity extends Activity implements OnClickListener {

Button b2,b3;
@Override
protected void onCreate(Bundle savedInstanceState)
       {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(this);

               b3=(Button)findViewById(R.id.button3);
b3.setOnClickListener(this);

}
@Override
public void onClick(View v)
    {
      if(v==b2)
b2.setTextColor(getResources().getColor(R.color.mycolors));
else
b3.setTextColor(getResources().getColorStateList(R.color.mycolors));

}



we have 2 more options based on class, and configuration also. for apply resouce to view object.


Monday, 15 September 2014

Linking Activities using Intents,Passing data between 2 Activities

Yes, In andriod we have Intents to pass data from one activity to another activity. intent is available package  android.content.Intent.

design First Activity/Main_activity.which bydefault specified in andriod manifest file.




& Second Activity and Specify this in androidManifest.xml
then speciy in manifest file

    <activity android:name="com.exampleintents.intentsproject.SecondActivity">



How to use TableLayout and simple demo with conversions

yes, we will demonstract here for conversion, Integer.Parse
package com.andriodproject.helloandriod;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity  implements OnClickListener
{
   EditText edt1;
   EditText edt2;
   int result;
   TextView tvresult;
   Button btnadd,btnsubstract,btnexit;
int total;

   @Override
         protected void onCreate(Bundle savedInstanceState)
   {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
       
         edt1=(EditText)findViewById(R.id.editText1);
         edt2=(EditText)findViewById(R.id.editText2);
       
         tvresult=(TextView)findViewById(R.id.textView3);
       
         btnadd=(Button)findViewById(R.id.btn_add);
         btnsubstract=(Button)findViewById(R.id.btn_sub);
         btnexit=(Button)findViewById(R.id.btn_exit);
         btnadd.setOnClickListener(this);
         btnsubstract.setOnClickListener(this);
         btnexit.setOnClickListener(this);
   }
@Override
public void onClick(View v)
{
if(v==btnexit)
{ finish();
}
else
{
String first=edt1.getText().toString().trim();
String second=edt2.getText().toString().trim();
if(first.length()<=0)
{
tvresult.setText("Enter First No");
       edt1.requestFocus();
return;
}
if(second.length()<=0)
{
tvresult.setText("Enter second No");
       edt2.requestFocus();
         return;
}
try
{
  int a=Integer.parseInt(first);
  int b=Integer.parseInt(second);                                                                   if(v==btnadd)
                                    total=a+b;
else if(v==btnsubstract)
total=a-b;
  tvresult.setText(String.valueOf(total));
}
catch(Exception ex)
{
tvresult.setText("error occured");
}
}

}
}

Sunday, 14 September 2014

Layouts : Relative,Linear,Grid,Table,TableRow


Android Provides few layouts for structure  user interface such a Linear,Relative,Grid,Table,Frame layouts

by default main activity contains Relative layout, Relative layout provides advantage is 

 we can place our components/views like one another other, one below another. however we need, but we need to specify with respect another view or parent. 

 Relative layout is good customized layout for all our components/views. 

1. Place view center of the relative layout.

andriod:layout_centerInParent, Vertical,Horizontal---are true/false /

 In Java[ Center_IN_Parent,     Center_Vertical,   Center_Horizontal].

2. Alignment between view with its parent edge

android:layout_alignParentTop                     // ALIGN_PARENT_TOP
android:layout_alignParentButtom               // ALIGN_PARENT_BUTTOM
android:layout_alignParentLeft                    // ALIGN_PARENT_LEFT
android:layout_alignParentRight                 //  ALIGN_PARENT_RIGHT
















3. Alignment between the views using 

android:layout_above
android:layout_below
android:layout_toLeftOf
android:layout_toRightOf

android:layout_below="@id/textView1"   ----then button is place below the textview control.

4. Define alignment between 2 views
 android:layout_alignTop/Button,Left,Right,Baseline.


Relative layout doesn't provides weight and gravity properties to align them like linear layout.

Linear layout : by placing views and specifing weight to same value, all views has same size or width. linear layout can be horizontal or vertical.

Gridlayout: which has rowscount and columcount values to specify the size of Gird. 

Grid lines are reference with indices, based on our need we will place them in respective cells/rows in the grid layout.

Framelayout is place holder for view or viewgroup.in this also all views are bydefault place to top left. using gravity enum we can specify : center,right,left.

Tablelayout to represent the views in table format.



Wednesday, 10 September 2014

Andriod register events and implementation


Android provides controls as components to design our application layout. in eclipse palette provides various categories of components : TextFileds,Layouts,Composite, Images & Media , Date and time.Tansitions,,ect

Layouts: these are main controls/components to design screen/activity UI. using respective layout contorl(Grid,Linear,Relative,Frame,Table,TableRow,Fragement,Space).


in this we created/default design layout is RelativeLayout, in that we place button and editText controls/components in Relativelayout. andriod.layout_below: order of control placed in layout/relativelayout.

in Code of  MainActivity.we need refer these components, so that we need find these control. then handling/registering the events.


here we casted 2 ways --1 is view casting && 2nd is direct casting to repective control(EditText) type. next article is types of layouts.

2nd implementation:

       Button b1;
       TextView tv;

        tv=(TextView)findViewById(R.id.textView1);
        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener()
        {
       public void onClick(View v)
       {
        tv.setText("welcome to android");
       }});
     
         }
3rd implementation

        Button b1;
        TextView tv;
   
            protected void onCreate(Bundle savedInstanceState)
   {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_main);
                     tv=(TextView)findViewById(R.id.textView1);
                     b1=(Button)findViewById(R.id.button1);
                     b1.setOnClickListener(myhandler1);  ****
   }    
   OnClickListener myhandler1 = new View.OnClickListener()
   {
       public void onClick(View v) {        
        tv.setText("welcome to androids");
       }
     };
     
4th implementation : declare funcation in Code & at activity_main.xml

  <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_first" android:onClick="ButtonOnClick" />


     


type castings: View is base type

View v=findViewById(R.id.button1);
Button b=(Button)v;
or 
Button b=(Button)findViewById(R.id.button1);

public void onClick(View v)
{
   if(v==b1)
       tv.setText("Button-1 Clicked");
        else if(v.getId()==R.id.button2)
       tv.setText("Button-2 Clicked");
else
     finish();
 }


Sunday, 7 September 2014

How to Create Andriod Project : Hello Andriod

In this article we will discuss about "How to Create Andriod Project using Eclipse"

we have no'f tools to develop andriod applications like eclipse,visual stuido, andriod studio, presently i am using eclipse.--open eclipse--File--New --Andriod Application Project


in this we specified

Application name :  name of application display in tablet or mobile.

Project name :  which creates a folder at andriod workspace.

package name : which our package/library for Currently implementation code is packaged into to it.

Min SDK :  lowest version of our application supports.
Max SDK : highest version of our application supports.
Compile with : Andriod runtime for execution code.
Theme :  theme is to support for good design.


Activity name and custom/predefined logo && location of project ---giving option/control to developer as optionals.



then, we can create our own custom text and images/logos or utilize andriod supported images also.
and select one of templet of andriod predefined templets as per our requirements. example our project requires any actionbar support ---choose--Actionbar templet this below image does has that templet.



these templets are available based our ---project creation environment(Min,Max SDK's, compile...ect).--our first above image project settings.

once andriod project is created :
in this project structure : we have

src:-  implementation as a package // com.andriodproject.helloandriod.

gen:- our source code written in java language
      com.andriodproject.helloandriod
         -R.java. // deleted by our self--andriod runtime will create automatically every time.

Andriod 4.2.2
       andriod .jar file. which has predefined packages, which placed under

G:\Softwares\adt-bundle-windows-x86_64-20140702\sdk\platforms\android-19

this platfrom is selected by it self eclipse editor while choosing 

Complie with : API level--below .jar file will be andriod runtime will take the platfrom of apporiarite envirnoment for andriod project compliation.

G:\Softwares\adt-bundle-windows-x86_64-20140702\sdk\platforms\android-19
G:\Softwares\adt-bundle-windows-x86_64-20140702\sdk\platforms\android-20
G:\Softwares\adt-bundle-windows-x86_64-20140702\sdk\platforms\android-L

so that once the project is created with Min and Max SDK, then we simple changes in AndriodManifest.xml.--> also project will not run because the the project platfrom/compile platfrom for execution excepting respective .jar file. otherwise "unable to resolve target andriod-8"

bin : andriod apk & manfiest.xml

res : which has various sub folders as per our application resources we need to take respective folders and design the application.

layouts, values, colors,,...ect  base on resources.

Activity Name : MainActivity
Layout Name   : activity_main      //  graphical style and activity_main.xml.





this is best pratice to declare variables/string values in Values folder. for accessing styles, string values,images,colors...base on type of resource define respective resource files in respective folders.

How these values are accessed through xml to R.java file.

activity_main.xml
strings.xml
styles.xml



strings.xml

<resources>
    <string name="app_name">HelloAndriod</string>
    <string name="hello_world">Hello world!</string>
    <string name="hello_demo">Welcome to android!</string>
</resources>

MainActivity.java



then these variables / resources accessble in java program as like

R.string.hello_world
R.string.hello_demo
R.string.app_name
R.drawable.andriod

setContentView(R.layout.activity_main);    as well

R.java

        public static final int activity_main=0x7f030018;
        public static final int app_name=0x7f0a000d;
        public static final int hello_demo=0x7f0a000f;
        public static final int hello_world=0x7f0a000e;


Sunday, 24 August 2014

Introduction to Mobile applications

Yes, an application which is installed in mobile operating system is called mobile application,simply called as mobile app's. now we have various mobile operating systems available(Andriod,IOS,Windows) are widely used operating systems.
                         All these operating systems/various platforms has their own runtimes, specifications to run the mobile applications.

Ex : Mobile operating systems : 1andriod platfrom ,
                                                2. ios platform,
                                                3.windows platform

every operating system has their native run time to run the mobile application. so that if we need an application for these operating systems. we have 2 options

1. Individual applications to develop using

   Andriod platform----------using Java language ---using eclipse/andriod studio editors
   IOS platform--------------using Objective-c -----using XCode editor
   Windows platfrom---------using C# or xaml-----using Visual studio.

2. Hybrid/Cross mobile applications(write once, deploy any device)

   1. Phonegap--------Html5,Javascript,CSS, & Phonegap API or Cordova

   Phone gap is open source API to develop an application which run in all mobile platfoms(andriod,windows,ios).

   2.  Xamarin---------using C# language-----xamarin studio or visual studio extension.
 
  Xamarin is not open source which has various editions to develop mobile hybrid apps. which generates respective native app code to other platfroms. xamarin is excepts C# language to develop the mobile applications.

these are currently available mobile platroms,languages,editors to develop mobile applications(mobile app's).

now we learn Andriod,IOS,Windows applications then Cross platform/Hybrid apps.

Andriod:

Andriod is a mobile plafrom/os run the applications, save the informations. Andriod has it's architecture.



Android Architecture



Andriod is Stack based architecture of operating system. it has 4 parts

1. Linux Kernal
2. Libraries & Runtime      
3. Application Framework   // for developing user defined apps
4. Applications.                   // built in apps.

Linux Kernal
               andriod operating system is modified linux operating system. which provides abstraction service to run andriod applications. this is abstraction layer to run the applications on andriod envirnoment/platrom/os.

Libraries
             libraries are built in services to run in-built and custom applications to in andriod envirnoment.
\
Application framework
            which provides built in framework to develop custom applications using java.
Applications
            Andriod operting system provides few built in applications like contacts,galary,calcualtor...ect

this is basic overview of andriod operating system. Andriod API(Name,Version,Code)provides few services which helps to develop the applicaions as early.more productively.