Introduction
This article demonstrates how to add a device year class library using the android studio and XML code.
Android library that implements a simple algorithm that maps a device's RAM, CPU cores, and clock speed to the year where that combination of specs was considered high end. This allows a developer to easily modify application behavior based on the capabilities of the phone's hardware.
RAM |
Condition |
Year Class |
768 |
1 core |
2009 |
1GB |
2+ core |
2010 |
1GB |
<1.3GHz |
2011 |
1.5GB |
1.3GHz |
2012 |
1.5GB |
<1.8GHz |
2013 |
2GB |
1.8GHz |
2014 |
3GB |
2.0GHz |
2015 |
more |
2.2GHz |
2016 & 2017 |
Let's start,
Step 1
Create a new project in Android Studio from File >> Project and fill all the necessary details.
Next, go to Gradle Scripts >> build.gradle (Module: app).Select build.gradle, The app Gradle compile the code and build types will appear. Just replace that the following code. To make a Device year class in your layout XML and add the device year library in your project or you can also Gradle
Download the latest JARs or grab via Gradle,
Compile Code
- compile 'com.facebook.device.yearclass:yearclass:2.0.0'
or Maven
- <dependency>
- <groupId>com.facebook.device.yearclass</groupId>
- <artifactId>yearclass</artifactId>
- <version>2.0.0</version>
- </dependency>
Step 2
Next, go to app >> res >> layout >> activity_main.xml.
Select the activity page. The XML code will appear, Just the following code. Create the layout of the TextView and Button.
Design View
Step 3
Next, go to app >> src >> >>main >> java >> MainActivity.java. The jave code will appear. Just replace that with the following jave code
Calculate Device Year Class
Calculating the current device's year class is simple
- int year = YearClass.get(getApplicationContext());
Then, later on, you can use the year class to make decisions in your app or send it along with your analytics.
- package com.example.ravi.myapplication;
-
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
-
- import com.facebook.device.yearclass.YearClass;
-
- public class MainActivity extends AppCompatActivity {
-
- Button button;
- TextView textView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- button = (Button) findViewById(R.id.button);
- textView = (TextView) findViewById(R.id.textView);
-
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
-
- int year = YearClass.get(getApplicationContext());
- String text;
-
-
-
- if (year >= 2013) {
- text= "Do advanced animation";
- } else if (year > 2010) {
- text = "Do simple animation";
- } else {
- text = "Phone too slow, don't do any animations";
- }
- textView.setText("Device year=" + year +"\nDevice capacity="+ text);
-
- }
- });
-
- }
- }
Next, go to Android Studio and Deploy the application, Select Emulator or your Android mobile with USB debugging enabled. Give it a few sec to make installations and set permission
Run the application in your desired emulator (Shift + F10)
Summary
Finally, we have successfully created a Device Year Class application. Later we will discuss more Android Applications.