Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Thursday, May 21, 2015

Set text size for Toolbar

Since Toolbar doesnt provide a textSize attribute to set the text size of the Title you have to add a TextView and pass the title text to it.
Ofcourse you can instead use setTitleTextAppearance where you can set color, size, style etc.

For the first solution the steps you got to follow are :

1) remove default title :

2) Add TextView in Toolbar :




3) Set toolbar_textSize on dimens.xml :

<dimen name="toolbar_textSize">16sp</dimen>

Now toolbar's title size is the one you specify on the dimens.xml  file. That's it 16sp in our code.
We prefer sp units (instead of dp) so the size can scale when user changes the font size on her/his device.

Sunday, July 14, 2013

Android Parsing JSON data

When we are dealing with Web Services often the response are in JSON format. That means we have to get the data and with the JSONArray and JSONObject classes parse through the response and extract the data we want.

The whole JSON schema uses the convention of name:value pair. The simplest example we can have here is the JSON Object { "foo":"bar" } where bar is value of foo.
We can easily imagine making a request to the server for a basketball player name and getting the answer in the above format.
Even better we can ask the server for the Personal Information of the player named "lebron james" and get data like position, nationality, weight, height, age, etc...
Then a valid response from the server could be like that :
Values can be String, number, true, false, null, or even Object and Array.

You can read more about json here.

Now in our application we are going to use the schema ( and the values ) you can see in the image below.
click the image to enlarge

What we see is that we have an Object described by the String "users" and that Object's value is an Array.
Now we just need to think procedural and a) add the data to a JSONObject and from that data b) get the JSONArray described by the name "users".

So our code should look like :
       JSONObject jObj = new JSONObject(data);
       JSONArray sArr = new JSONArray();
       sArr = jObj.getJSONArray("users"); //getting the "users"

Now all we need to do is to iterate through the items of the Array and get the values specified by each tag ( id, username and password in our case ).

Since we don't get any response from a web server I stored the answer into a String.

The code is below :

And the XML layout file : 

The result in our Android emulator :
display JSON response from server

Monday, April 8, 2013

Detect user click on Map

To detect click on GoogleMap we implement OnMapClickListener Interface.
We add to our GoogleMap setOnMapClickListener . This sets a callback to the Interface when the map is clicked

Then we need to override the onMapClick(LatLng point) 

public void onMapClick(LatLng point) { mMap.animateCamera(CameraUpdateFactory.newLatLng(point)); }

We are using CameraUpdateFactory to get a new CameraUpdate Object ( an object to modify a map's camera ) and animate to the new point with animateCamera.

Sunday, November 25, 2012

ListView with Objects

Let's say you have just parsed some JSON data and you want to display them to a ListView.
For Simplicity sake I will assign all the properties of each item to only one TextView so we won't need to create a custom ArrayAdapter.

Assume that's our JSON data :

[  {"id":"1","title":"Homeland"},{"id":"2","title":"Breaking Bad"}{"id":"3","title":"Dexter"},{"id":"4","title":"Californication"}  ]

There's a JSON Array and that's everything in between [, ] and there are four  JSON Objects all of them with id and title properties.

First of all we are gonna create a class for our Objects :

public class TvShow() {
int id;
String title;
     public TvShow( int id, String title) { = id;
    this.title = title;
     public int getID() {
    return id;
        public String getTitle() {
    return title;
    public String toString(){
String toReturn = "ID = "+id + " Title= " +title ;
    return  toReturn;
Note that we need to Override the toString method for the Object TvShow so that every TextView in the list will display strings.

Initialize an ArrayList of  <TvShow> Obejcts :

     ArrayList<TvShow> alist = new ArrayList<TvShow>();

 So finally we store our data to a JSONArray object :

   JSONArray jArray = new JSONArray(result); //result is the String response we got from our web service

and get the length of the array. int len = jArray.length();

All we need to do now is a for statement to run the array for each item.

   for (int i = 0; i < len; i++) {
JSONObject jObj = jArray.getJSONObject(i);
            int _id = jObj.getInt("id");
String _title = jObj.getString("title");
                            JSONData jData = new JSONData(_id, _title);
   } catch (Exception e) {
// TODO: handle exception
return alist;

Now we have our ArrayList alist with TvShow Object's data.

            ArrayList<TvShow> results = GetResults(); //assuming the GetResults returns the alist from above

In our XML file we create a ListView and grab its id to the ListView item :

        ListView lv= (ListView)findViewById( );

Next we need to define our Adapter :
            ArrayAdapter<TvShow> arrayAdapter = new ArrayAdapter<TvShow>(this, android.R.layout.simple_list_item_1 , results );
The constructor is : 

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

The last thing we need to do is to assign the adapter to the ListView :

Now our ListView assings to an Object for every TextView item in the list.

Sunday, March 25, 2012

java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation

Well that's a common error if u are messing around with google maps api.

go to Project > Properties > Java Build Path > Libraries and ensure that there is no android.jar or maps.jar directly included. You should only have Google Apis with at leastmaps.jar and android.jar as leaves.

As u can see in the image there are two maps.jar . You have to delete the first one and just leave the leaves of Google APIs Unable to open sync connection

Mess a lot with the code and now the error i get is : Unable to open sync connection!

as u can check @ stackoverflow the answer is to enable/disable the usb-debugging

Some other tips that may can help u is to reboot your device, unplug it from the PC and plug it again.