Wednesday, February 22, 2012

Invalid use of SingleClientConnManager: connection still allocated

Android Invalid use of SingleClientConnManager

Problem:
When i tried to call place same object Httpclient.
ex: like
HttpClient httpClient = new DefaultHttpClient();
httpClient.execute(request);
httpClient.execute(request);
httpClient.execute(request);
httpClient.execute(request);
like multiple place if call same  object . you will get Invalid use of SingleClientConnManager: connection still allocated
below i mention code for solve this problem.
Solution

Tuesday, February 21, 2012

Send the Array of data to PHP server in JSONArray format

Send the Array of  data to PHP  server in JSONArray format

Problem:
I want to send the Array of  data to PHP  server in JSONArray format.
Solution:
-->

OUTPUT
-->
 
 

Saturday, February 11, 2012

Android JSONParsing Tutorial

Android JSONParsing Tutorial:

JSON is the best alternative to XML for storing data in files. It is easy to parse and access data stored in JSON format.
JSON Structure:

i am using example for json google route direction. below structure of json.
{
   "routes" : [----- first JsonArray
      {--- First json object
         "bounds" : {--- Second JsonObject
            "northeast" : {--- Third JsonObject
               "lat" : 11.664450,
               "lng" : 78.14620000000001
            },
            "southwest" : {
               "lat" : 11.664450,
               "lng" : 78.14620000000001
            }
         },
         "legs" : [--- Second JsonArray
            {
               "distance" : {
                  "text" : "1 m",
                  "value" : 0
               },
               "duration" : {
                  "text" : "1 min",
                  "value" : 0
               },
               "end_address" : "Salem, Tamil Nadu, India",
               "end_location" : {
                  "lat" : 11.664450,
                  "lng" : 78.14620000000001
               },
               "start_address" : "Salem, Tamil Nadu, India",
               "start_location" : {
                  "lat" : 11.664450,
                  "lng" : 78.14620000000001
               },
               "steps" : [---- Third JSON Array
                  {
                     "distance" : {
                        "text" : "1 m",
                        "value" : 0
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 0
                     },
                     "end_location" : {
                        "lat" : 11.664450,
                        "lng" : 78.14620000000001
                     },
   "html_instructions" : "Head \u003cb\u003eeast\u003c/b
\u003e on \u003cb
\u003eOmalur Main Road\u003c/b\u003e",
                     "polyline" : {
                        "points" : "yeefAw|}{M"
                     },
                     "start_location" : {
                        "lat" : 11.664450,
                        "lng" : 78.14620000000001
                     },
                     "travel_mode" : "DRIVING"
                  }
               ],
               "via_waypoint" : []
            }
         ],
         "overview_polyline" : {
            "points" : "yeefAw|}{M"
         },
         "summary" : "Omalur Main Road",
         "warnings" : [],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}
 
  This JSON Structure for Multiple array of values

PROBLEM:

How to read multible JsonArray .

SOLUTIONS:
 Before see the below code. please check above the JSON Structure.

String loginUrl = "http://maps.googleapis.com/maps/api/directions/json?
origin=Tamil%20ndau,Salem&destination=Tamil%20nadu,%20
salem&region=in&sensor=false";
 try{
  HttpGet request = new HttpGet(loginUrl);
  HttpResponse response = httpClient.execute(request);
  entityResponse = response.getEntity();
  result = EntityUtils.toString(entityResponse);  

  JSONArray array = object.getJSONArray("routes"); 
---Parsing first jsonArray     
      JSONObject routes = array.getJSONObject(0);
---(parsing first jsonarray object)
      String bounds= routes.getString("bounds"); 
-- parsing second josn Array  
      JSONArray legs = routes.getJSONArray("legs"); 
--- (parsing second jsonarray object)   
    JSONObject steps = legs.getJSONObject(0); 
    String distance= routes.getString("distance"); 
  --- parsing third json Array
      JSONArray legs1 = steps.getJSONArray("steps"); 
      for(int i = 0; i < legs1.length(); i++){
      JSONObject steps1 = legs1.getJSONObject(i);
 --parsing third jsonarray objecct      
      String htMlVale = steps1.getString("html_instructions").toString(); 
   -- parsing inside third jsonarray of jsonarray   
      JSONObject distance             =  steps1.getJSONObject("distance");
              String sDistance                =  distance.getString("text");()
         

            }
} 
more question ask me..... or any help about JSON
         
  

Android Merging Layouts

Android Merging Layouts

The articles showed you how to use the <include /> tag in XML layouts, to reuse and share your layout code. This article explains the <merge /> tag and how it complements the <include /> tag.

The <merge /> tag was created for the purpose of optimizing Android layouts by reducing the number of levels in view trees. It's easier to understand the problem this tag solves by looking at an example. The following XML layout declares a layout that shows an image with its title on top of it. The structure is fairly simple; a FrameLayout is used to stack a TextView on top of an ImageView:


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
    
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    
    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="20dip"
        android:layout_gravity="center_horizontal|bottom"

        android:padding="12dip"
        
        android:background="#AA000000"
        android:textColor="#ffffffff"
        
        android:text="Golden Gate" />
</FrameLayout>
 
Since our FrameLayout has the same dimension as its parent, by the virtue of using the fill_parent constraints, and does not define any background, extra padding or a gravity, it is totally useless. We only made the UI more complex for no good reason. But how could we get rid of this FrameLayout? After all, XML documents require a root tag and tags in XML layouts always represent view instances.
That's where the <merge /> tag comes in handy. When the LayoutInflater encounters this tag, it skips it and adds the <merge /> children to the <merge /> parent. Confused? Let's rewrite our previous XML layout by replacing the FrameLayout with <merge />:

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
    
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    
    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="20dip"
        android:layout_gravity="center_horizontal|bottom"

        android:padding="12dip"
        
        android:background="#AA000000"
        android:textColor="#ffffffff"
        
        android:text="Golden Gate" />
</merge> 
Optimized view hierarchy using the merge tag
Obviously, using <merge /> works in this case because the parent of an activity's content view is always a FrameLayout. You could not apply this trick if your layout was using a LinearLayout as its root tag for instance. The <merge /> can be useful in other situations though. For instance, it works perfectly when combined with the <include /> tag. You can also use <merge /> when you create a custom composite view. Let's see how we can use this tag to create a new view called OkCancelBar which simply shows two buttons with customizable labels. You can also download the complete source code of this example. Here is the XML used to display this custo

 <merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">

    <ImageView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
    
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    
    <com.example.android.merge.OkCancelBar
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"

        android:paddingTop="8dip"
        android:gravity="center_horizontal"
        
        android:background="#AA000000"
        
        okCancelBar:okLabel="Save"
        okCancelBar:cancelLabel="Don't save" />
</merge>

 The source code of OkCancelBar is very simple because the two
buttons are defined in an external XML file, loaded using a
LayoutInflate. As you can see in the following snippet, the XML
layout R.layout.okcancelbar is inflated with the
OkCancelBar as the parent:
public class OkCancelBar extends LinearLayout {
    public OkCancelBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        setWeightSum(1.0f);
        
        LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);
        
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);
        
        String text = array.getString(R.styleable.OkCancelBar_okLabel);
        if (text == null) text = "Ok";
        ((Button) findViewById(R.id.okcancelbar_ok)).setText(text);
        
        text = array.getString(R.styleable.OkCancelBar_cancelLabel);
        if (text == null) text = "Cancel";
        ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);
        
        array.recycle();
    }
} 

The two buttons are defined in the following XML layout. As you can see, we use the <merge /> tag to add the two buttons directly to the OkCancelBar. Each button is included from the same external XML layout file to make them easier to maintain; we simply override their id:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
   
<include
       
layout="@layout/okcancelbar_button"
       
android:id="@+id/okcancelbar_ok" />
       
   
<include
       
layout="@layout/okcancelbar_button"
       
android:id="@+id/okcancelbar_cancel" />
</merge>
We have created a flexible and easy to maintain custom view that generates an efficient view hierarchy:
The resulting hierarchy is simple and efficient
The <merge /> tag is extremely useful and can do wonders in your code. However, it suffers from a couple of limitations:
  • <merge /> can only be used as the root tag of an XML layout
  • When inflating a layout starting with a <merge />, you must specify a parent ViewGroup and you must set attachToRoot to true (see the documentation for inflate(int, android.view.ViewGroup, boolean) method)

Android Optimizing Battery Life

Android Optimizing Battery Life

Problem:
For your app to be a good citizen, it should seek to limit its impact on the battery life of its host device. After this class you will be able to build apps that monitor modify their functionality and behavior based on the state of the host device.
By taking steps such as disabling background service updates when you lose connectivity, or reducing the rate of such updates when the battery level is low, you can ensure that the impact of your app on battery life is minimized, without compromising the user experience.

SOLUTIONS:
Monitoring the Battery Level and Charging State
Learn how to alter your app's update rate by determining, and monitoring, the current battery level and changes in charging state.
Determining and Monitoring the Docking State and Type
Optimal refresh rates can vary based on how the host device is being used. Learn how to determine, and monitor, the docking state and type of dock being used to affect your app's behavior.
Determining and Monitoring the Connectivity Status
Without Internet connectivity you can't update your app from an online source. Learn how to check the connectivity status to alter your background update rate. You'll also learn to check for Wi-Fi or mobile connectivity before beginning high-bandwidth operations.
Manipulating Broadcast Receivers On Demand
Broadcast receivers that you've declared in the manifest can be toggled at runtime to disable those that aren't necessary due to the current device state. Learn to improve efficiency by toggling and cascading state change receivers and delay actions until the device is in a specific state.
 

Android Improving Layout Performance

Android Improving Layout Performance

Problem:
 Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint.


Solutions:     
Optimizing Layout Hierarchies
In the same way a complex web page can slow down load time, your layout hierarchy if too complex can also cause performance problems. This lesson shows how you can use SDK tools to inspect your layout and discover performance bottlenecks.
Re-using Layouts with <include/>
If your application UI repeats certain layout constructs in multiple places, this lesson shows you how to create efficient, re-usable layout constructs, then include them in the appropriate UI layouts.
Loading Views On Demand
Beyond simply including one layout component within another layout, you might want to make the included layout visible only when it's needed, sometime after the activity is running. This lesson shows how you can improve your layout's initialization performance by loading portions of your layout on demand.
Making ListView Scrolling Smooth
If you've built an instance of ListView that contains complex or data-heavy content in each list item, the scroll performance of the list might suffer. This lesson provides some tips about how you can make your scrolling performance more smooth.

Android Tracking Memory Allocations

Android Tracking Memory Allocations

Writing efficient mobile applications is not always straightforward. In particular, Android applications rely on automatic memory management handled by Dalvik's garbage collector, which can sometimes cause performance issues if you are not careful with memory allocations.
In a performance-sensitive code path, such as the layout or drawing method of a view or the logic code of a game, any allocation comes at a price. After too many allocations, the garbage collector will kick in and stop your application to let it free some memory. Most of the time, garbage collections happen fast enough for you not to notice. However, if a collection happens while you are scrolling through a list of items or while you are trying to defeat a foe in a game, you may suddenly see a drop in performance/responsiveness of the application. It's not unusual for a garbage collection to take 100 to 200 ms. For comparison, a smooth animation needs to draw each frame in 16 to 33 ms. If the animation is suddenly interrupted for 10 frames, you can be certain that your users will notice.
Most of the time, garbage collection occurs because of tons of small, short-lived objects and some garbage collectors, like generational garbage collectors, can optimize the collection of these objects so that the application does not get interrupted too often. The Android garbage collector is unfortunately not able to perform such optimizations and the creation of short-lived objects in performance critical code paths is thus very costly for your application.
To help you avoid frequent garbage collections, the Android SDK ships with a very useful tool called allocation tracker. This tool is part of DDMS, which you must have already used for debugging purposes. To start using the allocation tracker, you must first launch the standalone version of DDMS, which can be found in the tools/ directory of the SDK. The version of DDMS included in the Eclipse plugin does not offer you ability to use the allocation tracker yet.
Once DDMS is running, simply select your application process and then click the Allocation Tracker tab. In the new view, click Start Tracking and then use your application to make it execute the code paths you want to analyze. When you are ready, click Get Allocations. A list of allocated objects will be shown in the first table. By clicking on a line you can see, in the second table, the stack trace that led to the allocation. Not only you will know what type of object was allocated, but also in which thread, in which class, in which file and at which line. The following screenshot shows the allocations performed by Shelves while scrolling a ListView.

Tuesday, February 7, 2012

Android Set animation for dialog

Android :Set animation for dialog

Problem:

*How to set animation for Dialog.

Solution:

Below i added code for how to set animation for Dialog.
it will use full for increase attractive our apps.

Dialog dlg = new Dialog(this);
dlg.getWindow().getAttributes().windowAnimations = R.style.Animations_SmileWindow;


XML style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
          <style name="Animations" />
         <style name="Animations.SmileWindow">
                <item name="@android:windowEnterAnimation">@anim/grow_from_bottom</item>
                <item name="@android:windowExitAnimation">@anim/grow_from_top</item>
        </style>
       
</resources>


Xml anim code

grow_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <scale
                android:fromXScale="0.3" android:toXScale="1.0"
                android:fromYScale="0.3" android:toYScale="1.0"
                android:pivotX="50%" android:pivotY="100%"
                android:duration="@android:integer/config_shortAnimTime"
        />
        <alpha
                android:interpolator="@android:anim/decelerate_interpolator"
                android:fromAlpha="0.0" android:toAlpha="1.0"
                android:duration="@android:integer/config_shortAnimTime"
        />
</set>

grow_from_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <scale
                android:fromXScale="0.3" android:toXScale="1.0"
                android:fromYScale="0.3" android:toYScale="1.0"
                android:pivotX="50%" android:pivotY="0%"
                android:duration="@android:integer/config_shortAnimTime"
        />
        <alpha
                android:interpolator="@android:anim/decelerate_interpolator"
                android:fromAlpha="0.0" android:toAlpha="1.0"
                android:duration="@android:integer/config_shortAnimTime"
        />
</set>


How do I make Android tabs in a tabview transparent

Android - How do I make tabs in a tabview transparent.

  Problem: 

  *How to make tabwidget tabview  transparent.

  Solution:  

 just make it android:visibility="gone"

  <TabWidget 

android:id="@android:id/tabs" 

android:visibility="gone"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />    

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...