Posts

Showing posts from December, 2023

Using Socket.IO on android Always Returns XHR Poll Error

The problem I faced is strange and typical. Using Socket.IO on android Always Returns XHR Poll Error . Not much help available on the net regd. XHR Poll Error but StackOverflow has a quickfix. Finally the issue was resolved in two steps: 1.  CORS (Cross-Origin Resource Sharing). CORS Configuration: Ensure that your server-side configuration allows cross-origin requests if your client and server are on different domains. 2. In AndroidManifest.xml tweak  android:usesCleartextTraffic Indicates whether the app intends to use cleartext network traffic , such as cleartext HTTP. The default value for apps that target API level 27 or lower is "true". Apps that target API level 28 or higher default to "false". When the attribute is set to "false", platform components, for example, HTTP and FTP stacks, DownloadManager, and MediaPlayer, refuse the app's requests to use cleartext traffic. Since we are running on IT and yet to implement Positive SSL, we have to set...

Get Android App Version No and Display in a Text Field

 This is how we get Version Name of Android App from Google PlayConsole. 1.  In onCreate Method navigationView = findViewById( R . id . nav_view ); if ( navigationView != null ) { getVersionName(); } else { // Handle the case when navigationView is null Toast . makeText (getApplicationContext(), "Navigation Not Initialized" , Toast . LENGTH_LONG ).show(); } 2. Now define getVersionName() private void getVersionName () { Menu menu = navigationView .getMenu(); MenuItem nav_version = menu .findItem( R . id . nav_version ); PackageInfo pinfo ; try { pinfo = getPackageManager().getPackageInfo(getPackageName(), 0 ); String versionName = "App Version: " + pinfo . versionName ; nav_version .setTitle( versionName ); } catch ( PackageManager . NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Here we have added a menu item inside DrawerLayout to display version...

App Update Request for Android App :: Check Google PlayStore for Updated Version - Download - Install Update

App Update Request for Android App :: Check Google PlayStore for Updated Version - Download - Install Update. OK. Google has made this task easy for us. Here goes the steps: 1) Add this line of code in build.gradle file implementation 'com.google.android.play:core:1.12.0' 2) Inside onCreate Method, write appUpdateManager = AppUpdateManagerFactory . create ( this ); appUpdateManager .getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> { if (appUpdateInfo.updateAvailability() == UpdateAvailability . UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed( AppUpdateType . FLEXIBLE )) { try { appUpdateManager .startUpdateFlowForResult( appUpdateInfo, AppUpdateType . FLEXIBLE , this , REQUEST_CODE_UPDATE ); } catch ( Exception e) { e.printStackTrace(); } } }); 3) Now add following methods @Override protected void on...

Android Navigation Drawer Menu

 Creating Android Navigation Drawer Menu is a complex task involving bunch of steps. So, let's start. 1) In activity_main.xml file change the root level layout to  DrawerLayout. So whatever layout is there, make it  DrawerLayout at the very beginning. 2) Add NavigationView before the closing TAG of DrawerLayout < com.google.android.material.navigation.NavigationView android :layout_width ="wrap_content" android :layout_height ="match_parent" android :layout_gravity ="start" android :fitsSystemWindows ="true" android :id ="@+id/navigationView" /> 3) Add following properties inside the opening DrawerLayout android :fitsSystemWindows ="true" tools :openDrawer ="start" 4) Now Create a LayoutResouceFile and name it as header.xml. We will use this file as the header for DrawerLayout <? xml version ="1.0" encoding ="utf-8" ?> < LinearLayout xmlns: android ="htt...

Implementation of Socket on Android Java Platform

 Implementation of Socket on Android  Let us go through the steps for implementation. I have ready fair amount of tutorials and videos. And finally able to get it running with little tweak. Here goes the code. Here we apply socket to MainActivity.java file. This is where we display different counts which changes based on the value emitted from socket. We write this code before start of onCreate event. private Socket mSocket ; TextView textview; // Here we display result obtained from server { try { mSocket = IO . socket ( "https://your_server_name.com" ); // http://chat.socket.io } catch ( URISyntaxException e) { e.printStackTrace(); } } Inside onCreate method: textview = findViewById( R . id . tv_ textview); // Then inside onCreate method, initialise socket mSocket .on( "new message" , onNewMessage ); mSocket .connect(); // Here "new message" is the function we will observe. onNewMessage is is an object of the class Emitter.L...

Attach Custom Toolbar in Your Android Mobile Application (Android Java) - The Quickest Way

  Attach Custom Toolbar in Your Android Application - The Quickest Way <Code Snippet for my personal reference> If anyone else finds useful, it is their credit and kindness. 1. Add a LinearLayout to you activity where you want to show the toolbar < LinearLayout android :id ="@+id/toolbarLayout" android :layout_width ="match_parent" android :layout_height ="wrap_content" android :layout_alignParentLeft ="true" android :orientation ="vertical" > < include android :id ="@+id/toolbar" layout ="@layout/toolbar" /> </ LinearLayout > 2. Add another layout toolbar under layout directory      <? xml version ="1.0" encoding ="utf-8" ?> < androidx.appcompat.widget.Toolbar xmlns: android ="http://schemas.android.com/apk/res/android" xmlns: app ="http://schemas.android.com/apk/res-auto" android :layout_width =...