Tuesday 9 October 2012

Class cast exception

This Exception mainly occur because when we create an action on click of a button or an action to be performed on start of an activity ,but we forget the write the code in the activity for which we wrote intent to go.
for example in  a main.xml file we have 7 buttons.

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
   
    >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="30dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TRIMMER RESORT"
             android:textColor="#0060aa"
        android:textSize="25dp"
        android:textStyle="bold"
            android:layout_gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/menu"
            android:textColor="#0060aa"
        android:textSize="15dp"
        android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="MENU" />

        <Button
            android:id="@+id/events"
             android:textColor="#0060aa"
        android:textSize="15dp"
        android:textStyle="bold"
            android:layout_marginTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="EVENTS" />

        <Button
            android:id="@+id/directions"
             android:textColor="#0060aa"
        android:textSize="15dp"
        android:textStyle="bold"
               android:layout_marginTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="DIRECTIONS" />

        <Button
            android:id="@+id/bktable"
             android:textColor="#0060aa"
        android:textSize="15dp"
        android:textStyle="bold"
               android:layout_marginTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="BOOK A TABLE" />

        <Button
            android:id="@+id/share"
             android:textColor="#0060aa"
        android:textSize="15dp"
        android:textStyle="bold"
               android:layout_marginTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="SHARE" />

        <Button
            android:id="@+id/aboutus"
             android:textColor="#0060aa"
        android:textSize="15dp"
        android:textStyle="bold"
               android:layout_marginTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="ABOUT US" />

        <Button
            android:id="@+id/contactus"
             android:textColor="#0060aa"
        android:textSize="15dp"
        android:textStyle="bold"
               android:layout_marginTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CONTACT US" />

    </LinearLayout>
</ScrollView>

we wrote the MainActivity
as:


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

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

public class MainActivity extends Activity implements View.OnClickListener {
Button menu,events,share,direction,bookTable,aboutUs,contactUs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        menu=(Button)findViewById(R.id.menu);
        events=(Button)findViewById(R.id.events);
        share=(Button)findViewById(R.id.share);
        direction=(Button)findViewById(R.id.directions);
        bookTable=(Button)findViewById(R.id.bktable);
        aboutUs=(Button)findViewById(R.id.aboutus);
        contactUs=(Button)findViewById(R.id.contactus);
        menu.setOnClickListener(this);
        events.setOnClickListener(this);
        events.setOnClickListener(this);
        share.setOnClickListener(this);
        direction.setOnClickListener(this);
        bookTable.setOnClickListener(this);
        aboutUs.setOnClickListener(this);
        contactUs.setOnClickListener(this);
       
    }

public void onClick(View v) {
if(v==menu)
{
Intent i=new Intent(MainActivity.this,menuDetailActivity.class);
startActivity(i);

}
else if(v==events)
{
Intent i=new Intent(MainActivity.this,eventDetailActivity.class);
startActivity(i);

}
else if(v==contactUs)
{
Intent i=new Intent(MainActivity.this,contacUsDetail.class);
startActivity(i);
}
else
{

}
// TODO Auto-generated method stub

}

 

}
Now if we jsut create the the three activities  cotactUsDetail,eventDetail and menudetail classesa and for get to write the implementation code there
we get
"CLASS CAST EXCEPTION"application force closes down




in order to resove it we need to write all the three java file ,register them in manifest file and set the content view to respective activity that is for every activity one xml file shoud be given.

like contact detail is given here
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class contacUsDetail extends Activity implements OnClickListener{
Button call,email;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.contactusdetail);
call=(Button)findViewById(R.id.call);
email=(Button)findViewById(R.id.email);
call.setOnClickListener(this);
email.setOnClickListener(this);
}

public void onClick(View v) {
if(v==call)
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
   callIntent.setData(Uri.parse("tel:123456789"));
   startActivity(callIntent);

}
else if(v==email)
{
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
   intent.setType("text/plain");
   

   startActivity(intent);
 }
}
// TODO Auto-generated method stub
}
contact detail.xml is given here


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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="CALL US"  

        android:textSize="15dp"
  android:textColor="#0060aa"
        

        android:textStyle="bold"


       


       
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/call"  android:textColor="#0060aa"
        android:textSize="15dp"
        android:textStyle="bold"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="9052531507" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EMAIL US" 

        android:textSize="15dp"
     android:textColor="#0060aa"


        android:textStyle="bold"

        


        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/email"  android:textColor="#0060aa"
        android:textSize="15dp"
        android:textStyle="bold"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="muza@cytrion.com" />

</LinearLayout>

Monday 8 October 2012

Android ListView with Searchbox Sort items


This article is next step of List Example. Here we have a search box which sorts the list view as when the content matches the list items.
Why do we need this?
Imagine that if we have 2K to 3K items in the listview, It will not be possible to  scrolldown till 2000th item.In this case this will be handy to cut short items.
To start with we add a edittext box and Listview added to LinearLayout.
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
<EditText android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="Search"></EditText><ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" ></ListView>
</LinearLayout>
[/sourcecode]
Have a sample String array that is to loaded as List items.
Create a temporary arraylist that hold the sorted items and Simply add it to the list view on addTextChangedListener() of edit text.
[sourcecode language="java"]
package com.androidpeople;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class searchsort extends Activity {
/** Called when the activity is first created. */
private ListView lv1;
private EditText ed;
private String lv_arr[]={"Android","Cupcake","Donut","Eclairs","AndroidPeople","Froyo",};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
ed=(EditText)findViewById(R.id.EditText01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++)
{
if(textlength<=lv_arr[i].length())
{
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength)))
{
arr_sort.add(lv_arr[i]);
}
}
}
lv1.setAdapter(new ArrayAdapter<String>(searchsort.this,android.R.layout.simple_list_item_1 , arr_sort));
}
});
}
}
[/sourcecode]
[gallery link="file"]












Android Listview Example


Today, we are going to see about a simple listview example. In Android, Listview is used to show a list of items in a vertically scrolling list.  Learn a listview of android array in this tutorial.
For instance, in a Registration form when we are selecting professions a list of items will be displayed. We can use Listview to display the list of items.
Your XML file should look like
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
[/sourcecode]
Your Java code looks like
[sourcecode language="java"]
public class ListviewExample extends Activity
{
private ListView lv1;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
}
}
[/sourcecode]



listviewexample

how create tabs in android application

use eclipse to create a project which contain tabs.
*select new project ,then select new android application project 
*chose the api level  i have taken 2.2
now in the layout folder go to mainpage.xml and paste this code 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  
 >


<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        
   

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>
</LinearLayout>

now in order  to create the tabs according to our requirement we can write java code given below




import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;



public class TabActivity extends  TabActivity 
{
public static TabActivity context;

 
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.mainpage);
       

       

          context=this;
       final TabHost tabHost = getTabHost();   
       Resources res = getResources();
   
  
     
     tabHost.addTab(tabHost.newTabSpec("tab1")
             .setIndicator("My Contact")
             .setContent(new Intent(this,TabGroup1Activity.class)));
     
     tabHost.addTab(tabHost.newTabSpec("tab2")
             .setIndicator("My Profile")
             .setContent(new Intent(this, TabGroup2Activity.class)));
     
     
     tabHost.addTab(tabHost.newTabSpec("tab2")
             .setIndicator("Settings")
             .setContent(new Intent(this, TabGroup3Activity.class)));
     
     
     tabHost.setCurrentTab(0); 
     for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
           tabHost.getTabWidget().getChildAt(i).getLayoutParams().height /=1.5;
          

       }
     
     
     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
int j=tabHost.getTabWidget().getChildCount();
Log.e("child countttttttttttttttttttttt ", Integer.toString(j));
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.BLACK);
tv.setTypeface(null,1);
tv.setTextSize(12);
 
//calling the method when the user change the tab
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
// int c=0;
// @Override
public void onTabChanged(String tabId) {
setTabColor(tabHost);
}
});
}
        setTabColor(tabHost);
        

}
public static void setTabColor(TabHost tabHost) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
try{
//  unselected tab will be displayed drawable background
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.header);
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
           //tv.setTextColor(Color.parseColor("#909598"));
           tv.setTextColor(Color.parseColor("#7c8286"));
}
catch (NullPointerException e) {
// TODO: handle exception
e.printStackTrace();
}

}
try{
//user selected tab will be displayed selected color
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor( "#1082ef"));
//tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(R.drawable.hover);
TextView tv = (TextView) tabHost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
       tv.setTextColor(Color.parseColor("#ffffff"));
}
catch (NullPointerException e) {
// TODO: handle exception
e.printStackTrace();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
       
}


   }


versions of android

  • army of android

    Android beta



    Android 1.1




    Android 1.5 Cupcake




    Android 1.6 Donut




    Android 2.0/2.1 Eclair




    Android 2.2.x Froyo




    Android 2.3.x Gingerbread




    Android 3.x Honeycomb




    Android 4.0.x Ice Cream Sandwich




    Android 4.1.x Jelly Bean