How to create DigitalSpeedDashboard in android?

AndroidMobile DevelopmentApps/Applications

This example demonstrate about How to create DigitalSpeedDashboard in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Open build.gradle and add library dependency as shown below –

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.myapplication"
      minSdkVersion 19
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   configurations {
      cleanedAnnotations
      compile.exclude group: 'org.jetbrains' , module:'annotations'
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.github.ngallazzi:DigitalSpeedDashboard:master-SNAPSHOT'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
   implementation 'org.jetbrains:annotations-java5:15.0'
}

Now open build.gradle(application) and add the following lines-

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
   repositories {
      google()
      jcenter()
   }
   dependencies {
      classpath 'com.android.tools.build:gradle:3.2.1'
      // NOTE: Do not place your application dependencies here; they belong
      // in the individual module build.gradle files
   }
}
allprojects {
   repositories {
      google()
      jcenter()
      maven { url 'https://jitpack.io' }
   }
}
task clean(type: Delete) {
   delete rootProject.buildDir
}

Step 3 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout 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"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical"
   tools:context = ".MainActivity">
   <com.ngallazzi.speedandrpmdashboard.DigitalSpeedDashboard
      android:id = "@+id/srDashboard"
      android:layout_width = "350dp"
      android:layout_height = "350dp"
      app:idleColor = "#35ABABAB"
      android:background = "#000000"
      app:speedColor = "#FFFFFF"/>
   <TextView
      android:id = "@+id/text"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Start Dashboard"
      android:textSize = "25sp" />
</LinearLayout>

In the above code, we have taken text view and digitalSpeedDashboard. When user click on text view, it will start digitalSpeedDashboard.

Step 4 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.ngallazzi.speedandrpmdashboard.DigitalSpeedDashboard;
public class MainActivity extends AppCompatActivity {
   TextView text;
   int speed;
   boolean time;
   DigitalSpeedDashboard digitalSpeedDashboard;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      text = findViewById(R.id.text);
      digitalSpeedDashboard = findViewById(R.id.srDashboard);
      digitalSpeedDashboard.setMaxSpeed(200);
      text.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            new Thread(new Runnable() {
               @Override
               public void run() {
                  time = true;
                  while (time) {
                     speed++;
                     digitalSpeedDashboard.setSpeed(speed);
                     try {
                        Thread.sleep(10);
                     } catch (InterruptedException e) {
                        e.printStackTrace();
                     }
                     if (speed == 200) {
                        time = false;
                        speed = 0;
                     }
                  }
               }
            }).start();
         }
      });
   }
}

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

In the above screen is the initial screen, when the user clicks on the text view. It will start the digital dashboard and finally reaches to 200 and stop digital dashboard as shown below -


Click here to download the project code

raja
Updated on 30-Jul-2019 22:30:25

Advertisements