Developing a Screen Mirroring Android App

Aditya Rohilla
MindOrks
Published in
3 min readMar 28, 2018

Have you ever tried Screen mirroring (or casting) your phone on a Smart Tv? You must have. There are tons of available applications for this task. If you haven’t heard of Screen mirroring, let me explain:

Screen mirroring (sometimes called Screen-casting) allows you to mirror your mobile device’s content on your TV screen.

It can be used for numerous things including playing games or watching videos on a big screen. It generally has this icon:

Screen Mirroring icon

Now we will develop our own Android application for this task.

We will use Android Studio for this. If you’re new to Android application development, these can help you get started:

Installing Android Studio

Android Basics by Udacity

So let’s dive into some code!

First, we’ll create a toolbar for our application as generally, this icon lies on the top of the screen. For making a toolbar, let’s start with adding the dependencies to build.gradle (module: app)

compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.android.gms:play-services-appindexing:8.4.0'

Now in res/layout/activity_main.xml file, we’ll add the toolbar widget from android.support.v7.widget library :

<android.support.v7.widget.Toolbar 
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp" />

Now we have to add a menu for the toolbar. In res/menu we’ll add main_menu.xml.

<menu 
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" tools:context=".MainActivity"
android:layout_height="wrap_content" android:layout_width="wrap_content">
<item
android:id="@+id/action_fav"
android:orderInCategory="300"
android:title="cast"
android:icon="@drawable/icon"
app:showAsAction="always">
</item>

</menu>

In main_menu.xml, we defined an item to show in the toolbar. The icon used is of screen mirroring same as one above.

After defining the xml elements, now we will head over to the java code to make changes to our MainActivity. Inside java/MainActivity.java, initialize the toolbar widget in the onCreate function. For importing any library like android.support.v7.widget.Toolbar in this case, just press Alt + Enter after defining it in the function.

@Override protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(mToolbar); }

After adding the toolbar, we need to inflate the menu bar and handle the click on our screen mirroring icon.

@Override public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.main_menu, menu); 
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_fav)
{
startActivity(new Intent("android.settings.CAST_SETTINGS")); return true;
}
return super.onOptionsItemSelected(item);
}

action_fav represents our screen mirroring icon. Inside the onOptionsItemSelected function, we sent an intent to start an activity to “android.settings.CAST_SETTINGS”.

This sends a signal to internal settings to launch the inbuilt screencast services. This service will automatically search a device, make a connection through Wifi-Direct and render your Android device screen on the secondary device. We don’t need to take the pain of going through this whole process ourselves, this little intent will take care of everything!

The application should be working now. Build the project and run.

The results should be like:

You can find the entire application code at:

ScreenMirroring Application

In this post, we developed a simple screen mirroring android application. In the coming post, we’ll develop some more simple daily use Android applications just for fun!

If you have any doubts or queries, post in the comment section.

If you liked it, hit the clap icon!

Originally published at adityarohilla.com on March 28, 2018.

--

--