In this video it shows the code to check whether the HDMI input is connected to any source or not. Based on the value returned the output is displayed in the Android TV App.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.hdmiinputstate;
import androidx.appcompat.app.AppCompatActivity;
import android.media.tv.TvInputInfo;
import android.media.tv.TvInputManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
public void buttonShowHDMIState(View view){
TvInputManager tvInputManager = (TvInputManager) getSystemService(TV_INPUT_SERVICE);
List<TvInputInfo> tvInputInfoList = tvInputManager.getTvInputList();
StringBuilder stringBuilder = new StringBuilder();
for (TvInputInfo tvInputInfo:tvInputInfoList){
String stringInputID = tvInputInfo.getId();
if (stringInputID.contains("Hdmi") && tvInputInfo.isPassthroughInput()){
if (tvInputManager.getInputState(stringInputID) == TvInputManager.INPUT_STATE_CONNECTED){
stringBuilder.append("\nCONNECTED - " + stringInputID);
}else {
stringBuilder.append("\nNOT CONNECTED - " + stringInputID);
}
}
}
textView.setText(stringBuilder.toString());
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.171"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.264" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="372dp"
android:layout_marginTop="16dp"
android:onClick="buttonShowHDMIState"
android:text="Show HDMI Input State"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Excerpt:
The content features a tutorial video that explains how to check if an HDMI input has a source connected by using code in a custom Android TV app. The detailed guide goes over the source code, which involves the use of specific code libraries and syntax in the Android system. When executed, this code tells the app to show if the HDMI input is connected or not. The author provides additional resources and a contact for questions. The code can be beneficial for developers who need to monitor HDMI input states in Android TV applications.