I’m very new to Android. I want to pop up a transparent screen when the user receives a call. I have this code to open up the MyActivity screen, but it’s white instead of transparent.
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
SystemClock.sleep(1);
Intent intent = new Intent(context, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
context.startActivity(intent);
}
}
}
Here’s the code for MyActivity:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
}
}
And here’s the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:windowBackground="@android:color/transparent"
android:windowIsTranslucent="true"
android:windowAnimationStyle="@android:style/Animation.Translucent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/incoming_call"
tools:context=".MyActivity" />
</RelativeLayout>
This successfully pops up a screen with my message on it, but it does so with a white background instead of a transparent background. Any idea what I might be doing wrong? I’m using the emulator with the Android 2.2 SDK.
I think the issue may be in the confusion between the window and the contentView.
These attributes probably won’t be respected by RelativeLayout and whatever you put in the content view, in general.
The window attributes are a property of the Window. You can change the Window in code or in the setup of your activity using a Theme.
Then in some res file:
Project/res/values/themes.xml
You could also set it up by setting these properties on the Window of the activity directly, similar to how you set
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);You can also just set the window to have a transparent background.