vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Friday, August 31, 2012

java.text.ParseException- Convert string to Date type in android


HI. I got this above error , when I try to convert string to date type format.
In my scenario   I get  the data in string variable then I need to convert the string to certain Date format.
For that I tried many ways to accomplish my needs but each try I got this below error..

java.text.ParseException: Unparseable date: "01-08-2012"

Finally my friend refer the code ,then I convert the string to my date format.
Requirement Format I need:
“02 Feb “I need to this type of Format
Before the start the process you need to import the below namespace….

import java.util.*;
import java.text.*;

This the code finally Work For me…………..
String strformat="MMM-dd";
String ddate="01-08-2012";//Date in string format
try{
                     SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
                     java.util.Date date=sdf.parse(ddate);
                     sdf=new SimpleDateFormat("dd-MMM");//Format i need
                          
                     Toast.makeText(this, sdf.format(date), Toast.LENGTH_LONG).show();
                           }
              catch(Exception ex){
                     Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
              }
Result:
01-Aug

Any Doubts please share with comments..
Happy Coding……….

How to set the selected item by data or position on spinner in android


When using spinner in android we set the data to spinner using adapter.

Normally we get the data in array string and we store the value in spinner using adapter.
In some situation we need select the data through dynamic at the time we need to follow the below process to set the data in Spinner.
But spinner has to allow set the data only by positions. But we need to choose by data like dropdown list.
In that situation we have to follow the below process….

String[] name;
name =new String[]{"name1","name2","name3","name4","name5"};
ArrayAdapter adapter = new ArrayAdapter(this,
                                                                                android.R.layout.simple_spinner_item, name);
spinccy.setAdapter(adapter);

IF we want to set spinner selected data as “name3”
First we find the position of the data in adapter
int Position = adapter.getPosition(“name3”);
spinccy.setSelection(Position);

Thast’it this process you need to follow to set the data by value in spinner.
Happy Coding………….

Thursday, August 23, 2012

How to check internet connectivity in android?


Here I would like share bit of useful information used in android. In Some scenario user accessing date through network access, for an example consuming web service uses android application.
In such cases before call any service we must make sure mobile have an internet access.For checking that process you can use the below code to check the option

Step 1:
Use the below code wherever you want to check the connection
private boolean checkConnection()
       {
         
           ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);       
           NetworkInfo netInfo = cm.getActiveNetworkInfo();
           if (netInfo != null && netInfo.isConnectedOrConnecting()) {
           return true;
           }

           return false;
       }

This one return Boolean value with respect the process checking.
Step 2:

Also you need to add the below code get the access permission to check the internet connection in android manifest file.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

That’s it no you have finished the process, now you can run the code check the function for checking internet connection.
Happy coding……..Post comments…

Wednesday, August 22, 2012

How to show alternating row color on ListView in android


Here I would share the code to apply alternative rows colors on listview.In a previous article I wrote to bind or add the data listview, for your information I mention link as below..

As we know very well we can add the data two listview using two methods..First one is create separate class for adapter and another using hash map string we can add the data in the same activity.
If you are using simple adapter in the same activity then you need to add the alternative row color then follow the below method process.
Here I add the data using simple adapter in the same activity now I want to apply the alternative row color.
If you use any adapter either simple or array you can follow the same process..
Previous code:
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                           R.layout.rowlayout, R.id.label, str);
              lst.setAdapter(adapter);

First step:
Create one separate class file and then extends the adapter that you used. Here I am using arrayadpater
And create class name BEadapter.java
Code:
public class BEadapter extends ArrayAdapter{
       private int[] colors = new int[] { 0x30B0E0E5, 0x30FFFFFF };

       public BEadapter(Context context, int resource, int textViewResourceId,
                     String[] objects) {
              super(context, resource, textViewResourceId, objects);
              // TODO Auto-generated constructor stub
       }
      
       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
         View view = super.getView(position, convertView, parent);
         int colorPos = position % colors.length;
         view.setBackgroundColor(colors[colorPos]);
         return view;
       }

}

Now you can change the code in main activity:
New Code:
BEadapter adapter = new BEadapter(this,
                           R.layout.rowlayout, R.id.label, str);
              lst.setAdapter(adapter);
               registerForContextMenu(lst);

Instead of using array adapter I used the class BEadapter, now its shows the color on the alternative row..
I hope it been useful..
Post your comments …..

Wednesday, August 15, 2012

Download Visual studio express 2012 for windows 8 and web


Here you can find the link to download the new visual studio express 2012 for windows and web. From the below link they provide the software for free.
Even you can download the windows 8 RTM to develop windows 8 application

Download link

Visual Studio express2012 For web:

Visual Studio express2012 For windows 8:

Visual Studio express2010 For windows phone:
For getting more details about the windows application development..


Tuesday, August 14, 2012

Apply different border style for layout activity in android


Here I share my ideas to apply some different border style for activity that we used in android applications.
I hope that you got some basic idea about adding background style for layout activity. For your reference here I mention my previous article related to background style in android.


Let come to this article, by making border to a whole activity application get more stylish and when it run on mobile the edges will look goods.
First create one xml file in the drawable folder Here I create onel xml file bggrad.xml
Add the below code into the xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <gradient
        android:endColor="#A0D3F8"
        android:gradientRadius="200"
        android:startColor="#FFF9FF"
        android:type="linear" />
    <stroke
        android:width="4dp"
        android:color="#1582BC" >
    </stroke>
</shape>

Once you created the file add the file as background for your all layout 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="vertical"
    android:background="@drawable/bggrad">  
</LinearLayout>

That’s it your layout will look like below sample screen ,even if you want to apply the corner radius ,add the corner radius tag into bggrad.xml file
<corners android:topLeftRadius="40dp" android:topRightRadius="40dp"
              android:bottomLeftRadius="40dp" android:bottomRightRadius="40dp"></corners>



















Post your comments here........Happy coding

Thursday, August 9, 2012

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application


android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application


I got this error when I had been working one of my android project..This error occurs due to I declare the dialog box should like below..

Dialog dialog =new Dialog(getApplicationContext());

Here I am using getApplicationContext() its denotes the context of whole application,due to mention this I got the error.
To solve this issues you need to mention like below..

Dialog dialog =new Dialog(st.this);

Here st denotes my activity, we have to declare the activity context to dialog.Now you can run the project it will work superbly.

How to install android APK file in android mobile phone


Here I would like to share my experience to start my first android application in my android phone.
I was very curious to see my application on my android phone, so I start to install my application on my phone. In this process I try number of methods to install my android application on my phone, but my works are helpless. Then finally I tried one method this time my application has successfully installed on my phone. Here I share the steps I followed.

Steps 1:
Create your android virtual device exactly same as your android mobile version.

Steps 2:
Run your project or press Ctrl + f11

Step 3:
In your project bin folder file has been created with your application name .apk
Get the .apk file from the folder

Step 4:
Just copy the file to your mobile phone by sending the .apk file to your phone any method you can follow to get the apk file to your phone.
Then the application will be installed on your phone.. That’s it.

Any doubts post your comments here…….

Monday, August 6, 2012

Share date using shared preferences in android application


Here I would like to share the usage of shared preference in android application. Basically in all application we need option to store a common data throughout the application. In android if you such a option then you can use shared preference method.
By using shared preference you can save the data in the three different types of ways.
1. Share data only on single activity
2. Share data on all activities within the application.
3. Share data between the different applications on the device.

First we see the different types of mode used shared preference.

MODE_PRIVATE-This Mode creates the file and it is used within the application.
MODE_WORLD_READABLE- This mode creates the file and it’s readable from other application in the device.
MODE_WORLD_WRITEABLE - This mode creates the file and it’s writable from other application in the device.



Storing Data in Activity Level:
SharedPreferences mypref=getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= mypref.edit();
        editor.putString("user", "pass");
        editor.commit();

Retrieve the data in same activity:
SharedPreferences mypref=getPreferences(Context.MODE_PRIVATE);
           String val=mypref.getString("user", null);

Storing Data across different activity Level:
Here we can get storage  data from any activity within the application
SharedPreferences mypref= getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=mypref.edit();
        editor.putString("user1", "test1");
        editor.putString("user2", "test2");
        editor.putString("user3", "test3");
        editor.commit();


Retrieve Data across different activity Level::
By using the below code we can get the data from the shared preference.
SharedPreferences mypref= getSharedPreferences("user", Context.MODE_PRIVATE);
           String val=mypref.getString("user1", null);



Store Sharing data across application level :
Here by using the below code if we store the data then we can get the data from the different or another application. This can be achieved by using the package name.
Here I have mention the two application of package name
1. com.sample.demoapp1
2. .com.sample.demoapp2
SharedPreferences mypref= getSharedPreferences("user", Context.MODE_WORLD_READABLE);
        SharedPreferences.Editor editor=mypref.edit();
        editor.putString("user1", "test1");
        editor.commit();

Retrieve Data across application level
Context cont;
                            cont = createPackageContext("com.sample.demoapp1", 0);
                        SharedPreferences mypref=cont.getSharedPreferences("user", Context.MODE_PRIVATE);
                        String x= mypref.getString("user1", null);
                        txt.setText(x);

Here I get the data from the application dempapp1 from demoapp2
Note:For sharing to application level data must store with the mode of Context.MODE_WORLD_READABLE then only it can access from the other application.

If you have any doubts regard using shared preference post your comments here……..

Wednesday, August 1, 2012

Enable the Button click event on ListView in android.


Hi.
Recently I had work with activity with listview. Activity I have a case to add button on listview, so I design custom layout with button and textview.my requirement has when on listview or button I need to get the id from the row and move to next activity.
In code I used only activity and arrayadapter to setadapter on listview

Already I wrote some articles about listview please refe it..
http://www.dotnetcode.in/2012/07/showing-context-menu-in-single-click-on.html

Requirement:
I have a requirement if the user clicks on button I need to get the id from the row and move on to another activity.

Problem:
Normally In listview without using button you can call the method OnItemClickListener
By using this one we can easily get the position and move to the next activity. But if you use button on listview this event will not fire on listview.

Solution:
By made big search on Google I found a simple and best solution to solve my requirement.
Intially you need add the two things in xml in button,then only OnItemClickListener
will work on listview.
android:focusable="false"
android:focusableInTouchMode="false"
Next one we want to make enable the button click event on list view
For that another line on xml button layout
android:onClick="myClick"

The final code will be looki like below one..
<Button
        android:id="@+id/btnaccno"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View"
        android:focusable="false"
android:focusableInTouchMode="false"
 android:onClick="myClick"
        />

Then write the myclick function on the activity. If you want to get the postion of the row from the listview use lst.getPositionForView(v)
Here lst denotes the ListView
public void myClick (View v)
           {
               int position1 = lst.getPositionForView(v);           
                     Intent i=new Intent(viewacc.this,viewtran.class);
                     i.putExtra("no", position1);
                     startActivity(i);         
           }

Thatsit…Now I fulfill my requirement I need ,Incase if you have met same kind of problem I hope this article would help you,the reason I share the code is I spent more time search to find the solutions.i hope I will reduce someone spare time to search this one..
In case if you have any problem to follow this method post your comments here..i try to give my solution best…