Checking Android Version using Code Android Java by Rajesh Kumar Sahanee - April 8, 2016September 12, 20170 Post Views: 5,807 Hello Friends, It’s being long time I’ve not shared anything with you but today I was working on an android application which was needed, two different code for different android versions so I needed to check android version running on current device to implement those two different codes. So I found the solution and created a test project for checking android version and I want to share this code. So here is the code and you can also download android studio project from the link which is at the end of this post. activity_main.xml activity_main.xml XHTML <?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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zatackcoder.versioncheck.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/messageTV" android:textAlignment="center" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check" android:id="@+id/checkB" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> 12345678910111213141516171819202122232425 <?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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zatackcoder.versioncheck.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/messageTV" android:textAlignment="center" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check" android:id="@+id/checkB" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /></RelativeLayout> MainActivity.java MainActivity.java Java package com.zatackcoder.versioncheck; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button checkB = (Button)findViewById(R.id.checkB); final TextView messageTV = (TextView)findViewById(R.id.messageTV); checkB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2) { messageTV.setText("Your Version is less than Jelly Bean or Jelly Bean"); } else { messageTV.setText("Your Version is higher than Jelly Bean"); } } }); } } 12345678910111213141516171819202122232425262728293031 package com.zatackcoder.versioncheck; import android.os.Build;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button checkB = (Button)findViewById(R.id.checkB); final TextView messageTV = (TextView)findViewById(R.id.messageTV); checkB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2) { messageTV.setText("Your Version is less than Jelly Bean or Jelly Bean"); } else { messageTV.setText("Your Version is higher than Jelly Bean"); } } }); }} Download Android Studio Project Android Version Check 1 file(s) 6.78 MB Download Thanks