Thursday, 19 March 2015

Get Android Phone screen dimensions in pixels

You can get the Android Phone Screen dimensions  in the following ways :

1)
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
 
2)
Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

3)
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels; 
 

 
 

Close/hide the Android Soft Keyboard

 //Show soft-keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams 
                       .SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//hide keyboard :
 getWindow().setSoftInputMode(WindowManager.LayoutParams 
                         .SOFT_INPUT_STATE_ALWAYS_HIDDEN);

SharedPreferences in Android to store, fetch and edit values .

You can store,fetch and edit data for specific app in Shared Preferences in following way  :-

Way 1)

Initialize sharedPrefrences object: 
//declare it in onCreate() 
SharedPreferences preferences = this.getSharedPreferences(
      "com.example.android", Context.MODE_PRIVATE);
 
To edit and save preferences(same it used to store new values):
SharedPreferences.Editor editor =preferences.edit();   
editor.putString("Key","Value"); 
editor.commit();
 
To read preferences:
String name = preferences.getString("Key","");

 
 
Way 2)

Initialize sharedPrefrences object:  
 //declare it in onCreate()
SharedPreferences preferences = PreferenceManager. 
                                 getDefaultSharedPreferences(this);
 
To edit and save preferences(same it used to store new values):
 SharedPreferences.Editor editor = preferences.edit();
 editor.putString("key","value");
 editor.commit();
 
To read preferences:
String name = preferences.getString("Key","");

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

 

Wednesday, 9 April 2014

Change Action bar title Font Style android

CHANGE ACTION BAR TITLE FONT STYLE

Step 1)

get the resource ID of android title

int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");

Step 2)

Now
make object of text view and typecast the ID aquired above to textview object

TextView actionBarTitleView = (TextView) getWindow().findViewById(actionBarTitle);

Step 3)

make the folder  named fonts in assets and copy  any fontstyle.ttf file in that folder


Typeface forte = Typeface.createFromAsset(getAssets(), "fonts/FORTE.TTF");

Step 4)
 Give changes according to your need to the text view

if(actionBarTitleView != null){
            actionBarTitleView.setTypeface(forte);
            actionBarTitleView.setTextSize(25);

        }
 
Step 5)

Now set title for Action bar

getActionBar().setTitle("SAMPLE");

it will set the tile with the fontstyle u specified with textview object

COMPLETE CODE
copy and paste

    int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
        TextView actionBarTitleView = (TextView) getWindow().findViewById(actionBarTitle);
        Typeface forte = Typeface.createFromAsset(getAssets(), "fonts/FORTE.TTF");
        if(actionBarTitleView != null){
            actionBarTitleView.setTypeface(forte);
            actionBarTitleView.setTextSize(25);
        }
       
       
       getActionBar().setTitle("SAMPLE");


NOW run your app and see the changes