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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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
Thanks
Comments