This example will show how you can put your activity in car dock mode. CAR DOCK is a safer way to access the applications while driving.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it CarDockExample.
2.) Write following into main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="The purpose of this application is to show how to create a custom car dock application." android:gravity="center_vertical|center_horizontal"/> </LinearLayout>
3.) Run for output.
Steps:
1.) Create a project named CarDockExample and set the information as stated in the image.
Build Target: Android 4.0
Application Name: CarDockExample
Package Name: com. example. CarDockExample
Activity Name: CarDockExampleActivity
Min SDK Version: 2.2
2.) Open CarDockExampleActivity.java file and write following code there:
package com.example.cardockexample; import android.app.Activity; import android.app.UiModeManager; import android.content.Context; import android.os.Bundle; import android.view.KeyEvent; public class CarDockExampleActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean dispatchKeyEvent(KeyEvent event) { return super.dispatchKeyEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { UiModeManager manager = (UiModeManager)getSystemService(Context.UI_MODE_SERVICE); manager.disableCarMode(UiModeManager.DISABLE_CAR_MODE_GO_HOME); super.onDestroy(); } }
3.) Compile and build the project.
Note: – For better results check on device.
Output