Skip to main content

Posts

Showing posts with the label Android

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

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"/>

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 ...

Android: Pagination in Custom List View

Points To Remember You can refer to blog - Create list view with custom list view adapter . Now we will implement pagination in this list view. Pagination in Custom List View Download Source from GitHub We will add 20 items each time to the list view. After 18th element is displayed on the screen we will call a method to load new items to the list view, so that user does not wait for new items to be loaded.(In real cases you might be loading from a server and this might take much time). We will restrict our pagination after 400 elements have been displayed.( It is a replication of the scenario when the server has no more items to be displayed). We will be setting a onScrollListener() on the listView to check when to load new items and add them to the list. listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) { } @Override public void onSc...

Android : Create a List View with custom List View Adapter

Points To Remember You need to create a custom List Adapter to show a model class in the list view. You need to create a xml file to display, how each item of the list will look on the UI. You need to set the Tag in the view, so that your data does not refresh due to scroll. Download from GitHub Create a List View with custom List View Adapter You need to make Person Model class - show data on UI from this class Person Adapter - create the list view and inflate the view Main Activity - to contain the list view  The List View will look like as shown in image below Download from GitHub

Android : Difference between gravity and layout_gravity

Points To Remember There are two attributes for a View in android to align its content gravity layout_gravity The basic difference between the two is that android:gravity is used for child elements of the view. android:gravity_layout  is used for this element with respect to parent view . So, if you have a layout like <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="EkiRas" android:textSize="22dp" android:gravity="center" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_h...

Android : How to hide and show a view

Points To Remember There are three things yo can do to set the visibility of a VIEW in android VISIBLE - it makes the VIEW visible . INVISIBLE - it makes the VIEW invisible but it still takes the space occupied by the View element on the screen. GONE - it makes the VIEW invisible and also hides the space occupied by the view on the screen. How to make a TextView Visible The following code makes the TextView invisible to the user and also hides the space occupied by the TextView on screen <TextView android:id="@+id/text_view_name" android:visibility="gone" android:layout_height="30dp" android:textColor="@android:color/holo_blue_dark" android:paddingLeft="15dp" android:textStyle="bold" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:gravity="center_vertical" android:layout_width="match_parent" android:b...

Android : How to change the Android Application Label in Action Bar

Points To Remember Android saves the Application level Settings like App Icon, App Label etc in the AndroidManifest.xml file. How to change the Android Application Icon  Go to the Application > manifests > AndroidManifests.xml  file and look for the following lines. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ekiras.activity" > <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" > // Your activities are declared here <activity></activity> </application> </manifest> You can set the Application icon in strings.xml by name app_name  in the values folder. <resources> <string name="app_name">Ekiras</string> ...

Android : How to create a Basic List View

Points To Remember You need to create a ListView in your activity. Then create a view for Item of the ListView. Create an Adapter and a list of objects to display. Pass the List of objects to the ListView. How to create a Basic List View  Your Activity may look like the following activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="Planet List" android:textAlignment="center" android:textSize="24dp" /> <ListView android:id="@+id/list_view_drawer" ...

Android : How to change ActionBar/ ToolBar color

Points To Remember You can just override the primary_material_dark  in the colorx.xml file in values folder. How to change ActionBar/ ToolBar color We will create a new file colors.xml if it does not exist. Override the Primary color of the theme  Theme.AppCompat.Light.DarkActionBar See the results <?xml version="1.0" encoding="utf-8"?> <resources> <color name="primary_material_dark">#1B5891</color> </resources> Your styles.xml file may look like the following <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> </style> </resources> and the AndroidManifest.xml may look like following <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com....

Android : Add Action Icon / Action button on Action Bar

Points To Remember You need to create a file in res > menu > menu.xml You can use showAsAction property to decide if to show the action icon on action bar or not.  Adding Action Icon Action button on Action Bar You can choose either of the following app:showAsAction="never" This will show the action in the list view when menu icon is pressed. app:showAsAction="ifRoom" This will show the action in the action bar if the amount of space required to show the action is available on the Action bar. <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".HomeActivity"> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="#Settings" app:showAsAction="never" /> <item ...

Android : How to change application icon

Points To Remember Android saves the Application level Settings like App Icon, App Label etc in the AndroidManifest.xml file. Add your icon image as icon.png  in the drawable folder. How to change application icon Your Application structure looks like the following. Add the icon.png folder in the drawable  folder Open the Application > manifests > AndroidManifests.xml  file Add android:icon="@drawable/icon" in the application root. Your AndroidManifests.xml file should look like following <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.ekiras.activity" >       <application         android:allowBackup="true"         android:icon="@drawable/icon"         android:label="@string/app_name"     ...