Skip to main content

Posts

Showing posts from December, 2015

Android : How to send Object from One Activity to Another Activity

Points To Remember You can send an Object from one Activity to another using two ways Make a class extend Parcelable  Make class Serializable Parcelable is a faster way to send and receive data, but it needs a lot of code to be written and manage in a particular way, however Serializable is a Java technique and is much simpler than the former way. Send Object from one Activity to Another Activity using Serializable We will make a POJO class Person that needs to be sent from ActivityOne to ActivityTwo. The Person class just needs to implement the serializable interface. The code may look like below Activity to send Object intent.putExtra("name", object); Activity to receive object Object object = (Object) getIntent().getSerializableExtra("name"); The full code example looks like the following

Java : How to Make a Http GET request and read response

How to Make a Http GET request and read response Steps to make a HTTP GET request Make URL object. Make HttpURLConnection object from URL object and type cast it to  HttpURLConnection Set Request method to GET. Add Headers using method setRequestProperty()  if required. Use BufferedReader to read the input stream of the connection. Read the response from buffered reader object. The code to implement the above steps is shown below.

Android : How to disable back button to Splash Screen Activity

Points To Remember Ideally splash screen should be shown only at the start on the Android Application. Application should not go back to the Splash screen when back button is pressed How to disable back button to Splash Screen Activity In order to disable your application to go back to the your can do the following Disable the Back Button on the Activity called just after the Splash Screen. Remove the Splash Activity from Application life-cycle/history. You can disable the back button on any activity programmatically using the following   @Override public void onBackPressed() { } You can also do the same by disabling back to activity by removing it from history in AndroidManifest.xml <activity android:name=".SplashActivity" android:noHistory="true"/>

Java : How to make enums inside java class

How to make enums inside java class You can use enums inside a class as shown in the code below. User.Gender.MALE or FEMALE  is always associated with the user property. However if you need Gender to be generic and u want to use it with animals also, then you might want to keep this enum in a separate class. package com.ekiras.demo; public class User { public enum Gender{ MALE, FEMALE } private String name; private String address; private String email; private String password; private Gender gender = Gender.MALE; } You can also use the enums with parameters inside class as shown below. package com.ekiras.demo; public class User { public enum Gender{ MALE("male"), FEMALE("female"); String value; Gender(String value){ this.value=value; } } private String name; private String address; private String email; private String password; private Gender gender = Gender.MALE; }

Android : How to implement Material Design Tab Layout

How to implement Material Design Tab Layout Android Material Design Tab Layout with toolbar Android Material Design Tab Layout without toolbar looks like the following. You can implement the Tab Layout in android by using the following steps. Step 1 : Add the design dependency in the project dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' compile 'com.android.support:design:23.1.0' } After adding the dependency, synchronize your project so that gradle downloads and add the dependency. Step 2 : Layout Structure for the activity Your activity structure should be like following. --CoordinatorLayout ----AppBarLayout ------TabLayout ----ViewPager Now let's understand why we need to follow this hierarchy to create the tab layout. CoordinatorLayout provides an additional layer of control over the touch events between child views. It is