I am using broadcastreceiver to display the values from SMS where i am using WindowManager to display it. But i am getting an Runtime exception which says it already has parent.
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.activity_check_sms,null);
TextView otpnum = (TextView)linearLayout.findViewById(R.id.optnum);
otpnum.setText(m.group(0));
otpnum.setBackgroundColor(Color.BLACK);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
windowManager.addView(otpnum,params);
Where my XML file is below.
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.snatarajan.otpreader.CheckSMS"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/optnum"/>
</LinearLayout>
What is wrong with this code?
otpnum
has already a parent which is linearLayout
.
So you maybe want to add the whole layout:
windowManager.addView(linearLayout,params);
or create a seperate xml which only includes your TextView and add that.