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.



Wednesday, January 13, 2016

Emulator vs Simulator

Emulator vs Simulator:
Emulator is trying its best to run/behave exactly like device would .so emulator run slow because instruction to the processor setup exactly same as the processor setup for emulator such as ARM7.  Needs conversion of instruction to below processor.
Simulator is simulate as device and it is faster. Does simulation of your stuff.


Wednesday, September 9, 2015

what is ADB in android / Android Debug Bridge(ADB)

Its a program running on device which communicates with emulator/device.
It is a client-server program that includes three components:
1. Daemon : running on device or emulator.
2.Client:- running on ur dev machine. invoke by adb command from trminal. ADT plugin or DDMS also creates the adb client.
3.Server:- running as background process on ur dev machine.  Manages the communication between the adb daemon and client.

You can find the adb tool in <sdk>/platform-tools/.

When you start adb it will check and start server if not running. when server starts it binds to local TCP port 5307 and listens for all commands from adb client on it. Server set up connection to all running devices and emulator.  It locates emulator/device instances by scanning odd-numbered ports in the range 5555 to 5585, the range used by emulators/devices.

 server finds an adb daemon, it sets up a connection to that port. Note that each emulator/device instance acquires a pair of sequential ports — an even-numbered port for console connections and an odd-numbered port for adb connections. For example:
adb [-d|-e|-s <serialNumber>] <command>
  • <tty> — the tty for PPP stream. For exampledev:/dev/omap_csmi_ttyl.
  • [parm]... — zero or more PPP/PPPD options, such asdefaultroute, local,notty, etc.


Emulator 1, console: 5554
Emulator 1, adb: 5555
Emulator 2, console: 5556
Emulator 2, adb: 5557
and so on...
As shown, the emulator instance connected to adb on port 5555 is the same as the instance whose console listens on port 5554.
server set up connections to all emulator instances, you can use adb commands to access those instances. Because the server manages connections to emulator/device instances and handles commands from multiple adb clients, you can control any emulator/device instance from any client (or from a script).

To start adb deamon at device need to enable developer option on device. 
You can issue adb commands from a command line on your development machine or from a script. The usage is:

ex:
adb devices
adb install <apk_path>
adb kill-server
adb connect <device-ip-address>
option           meaning
devices          list of all attached emulator/device.
help               list  adb commands.
version          Prints the adb version number.
-d                 Direct an adb command to the only attached USB device.
-e                Direct an adb command to the only running emulator instance.

-s <serialNumber> Direct an adb command a specific emulator/device instance  (such as "emulator-5556").

logcat [option] [filter-specs]  Prints log data to the screen.

bugreport     Prints dumpsys, dumpstate, and logcat data to the screen,

jdwp           Prints a list of available JDWP processes on a given device.

install <path-to-apk>  Install  an Android applicationton an emulator/device.

pull <remote> <local>    Copies a specified file from an emulator/device to your computer.

push <local> <remote>   Copies a specified file from your computer to an emulator/device.


forward <local> <remote>  Forwards socket connections from a specified local port to a specified remote port on the emulator/device instance.

ppp <tty> [parm]...      Run PPP over USB.

Note that you should not automatically start a PPP connection.


get-serialno   Prints the adb serial number string.

get-state       Prints the adb state of an emulator/device.

wait-for-device  Blocks execution until the device is online — that is, until the instance state is device.

start-server     adb server process is running and starts it, if not.

kill-server      Terminates the adb server process

shell        Starts a remote shell in the target emulator/device instance.

shell [shellCommand]  Issues a shell command in the target emulator/device instance and then exits the remote shell.



 JDWP is the protocol used for communication between a debugger and the Java virtual machine (VM) which it debugs (hereafter called the target VM).


Tuesday, December 2, 2014

What is a bundle in an Android


Bundle is generally used for passing data between various Activities of android. It depends on you what type of values you want to pass but bundle can hold all types of values and pass to the new activity.

You can use it like .....

Intent intent = new
Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("myKey",AnyValue);  
startActivity(intent);

Now you can get the passed values by...

Bundle extras = intent.getExtras(); 

String tmp = extras.getString("myKey");