Translate

Thursday, 11 October 2012

Download and Display images in Android

Aim: To download image from web and display it in android Application.

Procedure:  Here we are going to use URL of image to be downloaded. After downloading that Image we need to display it in application so we used Imageview, Bitmap, Bitmapfactory to our application.

Permission: For this application we need to take Internet permission from android.

<uses-permission android:name="android.permission.INTERNET" />

Code:

public class ImageCacheActivity extends Activity {
  
 ImageView image;
    Button download;
     Context context;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
        image=(ImageView) findViewById(R.id.image);
        download=(Button) findViewById(R.id.btnset);

        download.setOnClickListener(new OnClickListener() {
                   
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                               
                 try {
                       
                        URL url = new URL("http://www.google.co.uk/logos/holiday09_2.gif");
                                                      
                                             
                        URLConnection connection = url.openConnection();
                        connection.connect();
                       
                        InputStream is = connection.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is);
                        Bitmap bitmap = BitmapFactory.decodeStream(bis);
                        bis.close();
                        is.close();
                        image.setImageBitmap(bitmap);
                                          
                        } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
               
            }
        });
                       
    }
}

Output:

 Before running application check your Network setting that means Proxy,Port etc.

Tuesday, 9 October 2012

Consuming webservice using JSONObject in android

Fetching response data  in JSONObject

Here is the simple example for consuming webservice in android Using Json object. JSON is nothing but "JAVA SCRIPT OBJECT NOTATION ". Here i simply access the service which is written in .Net which gives response in JSON format.

Here is the Response from service:

{"d":{"__type":"Customer:#WcfService1","Address":"Pune","Name":"Shailesh"}}
 
 
Android Code: 
public class AndroidJsonActivity extends Activity {
    Button send;
      URI uri;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
send=(Button) findViewById(R.id.btnSend);
       
            
  send.setOnClickListener(new OnClickListener() {
   
public void onClick(View v) {
      
try {
     
 String Address,Name;
     
 DefaultHttpClient httpClient = new DefaultHttpClient();
 uri = new URI("http://192.168.1.2:90/Service1.svc/getcustomer");
     
 HttpGet httpget = new HttpGet(uri);
 httpget.setHeader("Accept", "application/json");
 httpget.setHeader("Content-type", "application/json; charset=utf-8");

 HttpResponse response = httpClient.execute(httpget);
  HttpEntity responseEntity = response.getEntity();
               
          
 long intCount = responseEntity.getContentLength();

 char[] buffer = new char[(int)intCount];
 InputStream stream = responseEntity.getContent();
 InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
              
              
 reader.read(buffer);
 String dataresponse = new String(buffer);
                   
          
                   
 JSONObject jsonObject = new JSONObject(new String(buffer));
                
 JSONObject idObject =   jsonObject.getJSONObject("d");
                
  Name = (String) idObject.get("Name");
 Address=idObject.getString("Address");
                
           
               
 Toast.makeText(getApplicationContext(), "Name-"+Name+"\nAddress-"+Address,Toast.LENGTH_LONG).show();
     
 } catch (URISyntaxException e) {
 Log.e("log_tag", "Error URI SYNTAX EXCEPTION "+e.toString());
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (ClientProtocolException e) {
 Log.e("log_tag", "Error CLIENT PROTOCOL EXCEPTION "+e.toString());
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IOException e) {
 Log.e("log_tag", "Error in IOEXCEPTION "+e.toString());
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (JSONException e) {
 Log.e("log_tag", "Error JSON EXCEPTION "+e.toString());
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
    
          
}
});
                                         
   }
} 
Output:- 



Friday, 14 September 2012

Facebook Web Search Coming Soon?


             The web is all about the queries and the results. For all those who like to spend most of the time on internet,surfing and searching  Google, you might get a new search engine soon. Now it seems like competition in search engines is now growing and we now might see a new competitor soon.
Recently, Mark Zuckerberg, the founder of famous social website Facebook indicated that the users might see a new web search engine developed by them. According to him, people are already using the Facebook search feature to find the social details of others and they are handling lots of queries daily. He also added saying that, they have a team of people working on developing the so called Facebook Search Engine, and at some point they will do it.



             According to him, there is something that Google and current search engines are missing. Instead of producing a pile of results, they should correctly and aptly answer a query just by proving a specific answer to a specific query made.
              So if we are to see this new Facebook SE, we should expect some major change how the search engines have been working till now. It won’t be wrong either to expect a major difference between Google and Facebook SEs. At least there is one thing that is clear, with all the info Facebook already has, they should not find it difficult to establish than any other new startup.

Adapter View in Android with Example

 What is Adapter in android??
               In android we cannot directly put any data into List View. So that we required adapter, which is act as mediator between data and listview. So first we take data into simple ListArray then pass it to adapter and then this adapter is set to List view.
 ----------------------------------------------------------------------------------------------------------- 


package shlsh.adapter.view;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class AdapterViewDemoActivity extends Activity {

    ArrayList<String> listOfCountryNames;
    ArrayAdapter<String> countryAdapter;
   
    EditText edtCountry;
    Button btnAdd;
   
    ListView listView;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   
        listOfCountryNames = new ArrayList<String>();
       
        edtCountry = (EditText) findViewById(R.id.edtName);
       
       
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
           
            public void onClick(View v) {
               
                listOfCountryNames.add(edtCountry.getText().toString());
                countryAdapter.notifyDataSetChanged();
            }
        });
       
       
        listView = (ListView) findViewById(R.id.listView);
       
        countryAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_single_choice,
                listOfCountryNames);
       
        listView.setAdapter(countryAdapter);
    }
}

OUTPUT:--------------------------------------

Android Architecture


           The Android software stack as shown in figure can be subdivided into five layers: The kernel and low level tools,  native libraries, the Android Runtime, the framework layer and on top of all the applications.

Android Architecture is categorize into layer  as well as by different meaningful color.

 RED LAYER:
                   The red layer represents the services offered by the Linux kernel and associated GNU utility packages ported to the ARM architecture. These components are licensed under the GNU [General Public License] the GPL.
-Core system services such as security, memory management, process management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware and the rest of the software stack.
                  

GREEN LAYER:
                   The green layer consists entirely of open source libraries available under various licenses. Some of the libraries come from the GNU project itself. The licenses here vary from the LGPL, BSD, MIT and are completely in the public domain in the case of the SQLite database. As such, any phone that would customize the libraries found in this layer, add libraries or remove them altogether would no longer be able to brand itself as an Android phone.
-Set of C/C++ libraries used by various components of the Android system.

BLUE LAYER:
                 The Blue layer, the application developer has access to what Android refers to as ‚ ‘service’ processes. These services are invisible to the user of the handset. Application developers can communicate with these services via a message bus. 

-Set of core applications including an email client, SMS program, calendar, maps, browser etc. All applications are written using the Java programming language
-Set of standard components and user services (written using the Java) which help to implement the standard structure of an application.

YELLOW LAYER:
A set of core libraries that provides most of the functionality available in the core libraries of the Java programming language.

Thursday, 13 September 2012

Code for Android Activity Life Cycle

Here is sample code to check different activity states throughout application.
 ----------------------------------------------------------------------------------------
package sheldroid.shlsh.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class ActivitylifecycleActivity extends Activity {

    void mt(String msg) {
       
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        mt("On Create");
    }
   
    @Override
    protected void onStart() {
        super.onStart();
       
        mt("On Start");
    }
   
    @Override
    protected void onResume() {
        super.onResume();
       
        mt("On Resume");
    }
   
    @Override
    protected void onPause() {
        super.onPause();
       
        mt("On Pause");
    }
   
    @Override
    protected void onStop() {
        super.onStop();
       
        mt("On Stop");
    }
   
    @Override
    protected void onDestroy() {
        super.onDestroy();
       
        mt("On Destroy");
    }
   
    @Override
    protected void onRestart() {
        super.onRestart();
       
        mt("On Restart");
    }
}
-----------------------------------------------------------------------------------
Now u can simply test this application by pressing Home, Backspace  buttons.
You will see Toast messages displaying their activity states. 

Wednesday, 12 September 2012

Android Activity Life Cycle.

What is an Activity in Android?

              Activity  is one of the application component, which gives you platform to interact with widgets, views etc. Activity gives you user interface to do many task as per requirement. In application we can use many activities with different user interface and functionality. 
                   Now, we see how activity Life cycle works.


 Activity Life Cycle