Tuesday, June 6, 2017

Making HTTP connection in Android

1.You  need to set network security configuration.
    1.1 Add network security config  xml file in project
    1.2 Make above xml file entry in manifest file under <application >  element for attribute android:networkSecurityConfig 
for more details find:
https://developer.android.com/training/articles/security-config.html#manifest

2.add permission to accesses internet in manifest file
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
3.Never call http connection on main thread it will generate exception  for not_allowed_on_main_thread.
Create separate task handler/ Asynctask/ New background thread and process http connection in it.

Ex:

AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();

 private class AsyncTaskRunner extends AsyncTask<String, String, String> 
  {


        @Override        protected String doInBackground(String... params)
        {
            HttpURLConnection urlConnection = null;
            try            {
                URL url = new URL("http://www.android.com/");
                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                readStream(in);

                String  str =  in.toString();

                BufferedReader r = new BufferedReader(new InputStreamReader(in));
                StringBuilder total = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    total.append(line).append('\n');
                }
                Log.i("",str+ "\n"+total);
            }
            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch ( IOException e)
            {
                e.printStackTrace();

            }
            catch ( Exception e)
            {
                e.printStackTrace();

            }
            finally            {
                urlConnection.disconnect();
            }

            return resp;
        }


        @Override        protected void onPostExecute(String result) 
        {
           
        }


        @Override        protected void onPreExecute()
        {

        }


        @Override        protected void onProgressUpdate(String... text) 
        {

        }
    }

Monday, May 8, 2017

margin vs padding in android

Padding is for inside/within components. Eg. TextView , ButtonEditText etc.
Eg. space between the Text and Border
Margin is to be applied for the on-outside of the components.
Eg. space between left edge of the screen and border of your component

Source: http://stackoverflow.com/questions/21959050/android-beginner-difference-between-padding-and-margin

What's the difference between fill_parent and wrap_content?


down voteaccepted
Either attribute can be applied to View's (visual control) horizontal or vertical size. It's used to set a View or Layouts size based on either it's contents or the size of it's parent layout rather than explicitly specifying a dimension.
fill_parent (deprecated and renamed MATCH_PARENT in API Level 8 and higher)
Setting the layout of a widget to fill_parent will force it to expand to take up as much space as is available within the layout element it's been placed in. It's roughly equivalent of setting the dockstyle of a Windows Form Control to Fill.
Setting a top level layout or control to fill_parent will force it to take up the whole screen.
wrap_content
Setting a View's size to wrap_content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children.
It's roughly the equivalent of setting a Windows Form Control's Autosize property to True.
Online Documentation
There's some details in the Android code documentation here.

Wednesday, March 1, 2017

What is the difference between ArrayAdapter , BaseAdapter and ListAdapter

ArrayAdapter :
It is a concrete class which works with array of data. you just  need to override only getview method

Listadapter
It is a an interface . So you need to implement it with concrete adapter classes.

BaseAdapter 
It is abstract class. It implements ListAdapter, SpinnerAdapter.
Its a base class for ArrayAdapter<T>, CursorAdapter, SimpleAdapter .
So you need to extend the BaseAdapter class by concrete class.  you need to implement all the methods like getcount,getid etc.

Array adapter and listadapter classes are developed since in general we deal with the array data sets and list data sets
BaseAdapter  is a base class for all the adapters.