Wednesday 14 January 2015

Difference between px, dp, dip and sp in Android?

  • px is one pixel.
  • sp is scale-independent pixels.
  • dip is Density-independent pixels. enter image description here
You would use
  • sp for font sizes
  • dip for everything else.
    dip==dp
From Android Developers center:
px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
1 Inch = 2.54 centimeters
mm
Millimeters - based on the physical size of the screen.
pt (it depends upon physical size of the screen) Points - 1/72 of an inch based on the physical size of the screen.
This will help you for more understanding.
enter image description here
dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

Show icons in Action Bar

Try this code

1) Using android default action bar
 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="always"       
  />
</menu>
 
2) If using supported action bar
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
  <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      yourapp:showAsAction="always"    
  />
 </menu>
 
 
for more info u can refer to this link
http://developer.android.com/guide/topics/ui/actionbar.html

Display response from http in next activity

Show response in next activity can be done in following ways

Solution 1 response comes in string so pass whole response to next activity by using

         intent.putExtras("Key",response);
 
Solution 2 make a singleton class of getter setter , by using this class u can set and get values throughout our application .
like this

 step 1

public class SingleObject {

 //create an object of SingleObject
 private static SingleObject instance = new SingleObject();

 private String name;
 private String age;

 //make the constructor private so that this class cannot be
 //instantiated
   private SingleObject(){}

 //Get the only object available
 public static SingleObject getInstance(){
  return instance;
 }

  // here u can declare getter setter


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAge() {
    return age;
}

public void setAge(String age) {
    this.age = age;
}

}
 
step 2  use it in first activity like this
    
     Singleton tmp = Singleton.getInstance( ); tmp.setName("");

step 3 and in next activity like this

    Singleton tmp = Singleton.getInstance( ); String name = tmp.getName;

Solution 3 : Make a static Arraylist of hashmap and use it in any activity

or there may be more solutions .....

enjoy coding