Inflate layout to view - android

I want to add view to window manager and this view must be my layout. But when I try to inflate layout and then add it I can only see one part of my layout(Button) and not the whole layout.Can somebody tell me what I do wrong?
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This is my code:
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.launcher_activity,null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT;
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(v, params);

Related

Android chat head disables touch on the entire row

I initiate a Service to inflate a chatHead view with following params:
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
// use your custom view here
floatingView = LayoutInflater.from(this).inflate(R.layout.screenshot_layout, null);
// floatingView = View.inflate(getBaseContext(), R.layout.screenshot_layout,null); //floatingView.setOnTouchListener(mOnTouchListener);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
Following was reflected:
Now the problem is : That the entire area (strip) parallel to the icons have their touch disabled which is not intended. I have tried tweeking with the parameters of the window manager, but the problem persists. Looking for a solution here.
Adding the XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/screenShot_root"
android:background="#android:color/transparent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:id="#+id/ll_screenshot_head"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:id="#+id/iv_camera"
android:src="#drawable/screenshot_external_icon"/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:id="#+id/iv_close"
android:src="#drawable/cross_external_icon"/>
</LinearLayout>
</RelativeLayout>
Well the issue was resolved by finding height & width of the layout at runtime as follows:
int w = (int) (TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 67, getResources().getDisplayMetrics()));
int h = (int) (TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 114, getResources().getDisplayMetrics()));
Where, 67 is the hardcoded width of the UI in dp and 114 is the height in dp. Above gave me height and width in pixels as per phone screen resolution.
I defined the new params as:
params = new WindowManager.LayoutParams(
w,h,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
Hence solving the problem of disabled touch area parallel to the UI.

Fragment within WindowManager in android

I am developing chat bubble like Messenger but i want to create container layout with fragment. I want to open container layout on click of chat bubble and container layout also contain fragment.
I don't how i use fragment in WindowManager, i searching too much but can't find any solution.
WindowManager
mFloatingView = LayoutInflater.from(bubblesService.getBaseContext()).inflate(R.layout.bubble_content_layout, null);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
BubbleLayout bb = getBubble().get(0);
params.x = bb.getDisplaySize();
params.y = 100;
mContentWindowManager = (WindowManager) bubblesService.getBaseContext().getSystemService(bubblesService.getBaseContext().WINDOW_SERVICE);
mContentWindowManager.addView(mFloatingView, params);
Containlayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/chat_away"
android:clipChildren="false"
android:clipToPadding="false">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Working..." />
<fragment
class="com.txusballesteros.bubbles.app.HomeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

java.lang.RuntimeException: view android.widget.TextView being added, but it already has a parent

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.

modal/Dialog not applying match parent layout on width

I have a dialog/modal that I need to match_parent along its width. However, it keeps doing wrap_content. Any ideas how to rectify this?
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#55555555" >
<Button
android:id="#+id/dogs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#AAAAAA"
android:clickable="true"
android:gravity="center"
android:onClick="onDogsClicked"
android:text="Dogs" />
<Button
android:id="#+id/cats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#AAAAAA"
android:clickable="true"
android:gravity="center"
android:onClick="Cats"
android:text="Cat" />
<Button
android:id="#+id/snake"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#FF0000"
android:clickable="true"
android:gravity="center"
android:onClick="onSnakeClicked"
android:text="Snake" />
<Button
android:id="#+id/sheep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#AAAAAA"
android:clickable="true"
android:gravity="center"
android:onClick="onSheepClicked"
android:text="Sheep" />
</LinearLayout>
</LinearLayout>
Here is the program fragment
Dialog animalsView;
public void onAnimalsClicked(View v) {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.animals_layout,
(ViewGroup) findViewById(R.id.my_root));
animalsView = new Dialog(this, R.style.CustomDialog);
WindowManager.LayoutParams wmlp = animalsView.getWindow()
.getAttributes();
wmlp.gravity = Gravity.BOTTOM;
wmlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
animalsView.getWindow().setAttributes(wmlp);
animalsView.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
animalsView.setContentView(dialoglayout);
animalsView.getWindow().setBackgroundDrawable(new ColorDrawable(0));
animalsView.show();
}
I had the same problem and I couldn't find a solution for doing it in the XML. But there is a solution in the .java file.
WindowManager.LayoutParams params = animalsView.getWindow().getAttributes();
params.gravity = Gravity.BOTTOM;
params.height = 100;
params.width = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Try this...
public void onAnimalsClicked(View v) {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.animals_layout,
(ViewGroup) findViewById(R.id.my_root));
animalsView = new Dialog(this, R.style.CustomDialog);
........
animalsView.setContentView(dialoglayout);
animalsView.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
......
animalsView.show();
}
I think you should try this. I was working with an overlay when I faced this issue. Hope this helps you.
Dialog animalsView;
public void onAnimalsClicked(View v) {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.animals_layout,
(ViewGroup) findViewById(R.id.my_root));
animalsView = new Dialog(this, R.style.CustomDialog);
WindowManager.LayoutParams wmlp = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSPARENT);
wmlp.gravity = Gravity.BOTTOM;
wmlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
animalsView.getWindow().setAttributes(wmlp);
animalsView.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
animalsView.setContentView(dialoglayout);
animalsView.getWindow().setBackgroundDrawable(new ColorDrawable(0));
animalsView.show();
}
Got similar issue and went through tons of StackOverflow posts concerning this issue.
I tried all possible solutions (changing layout configuration of the dialog window in all possible ways mostly), but none of them works.
However, when comparing the XML layout of a dialog for which the match_parent worked and the XML layout of dialog for which it does not, I finally found a weird way to make it work:
I have to set the layout_height of the dialog to match_parent as follows:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="#dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="match_parent">
// ...
</LinearLayout>
My guess is that when it comes to Dialogs, Android tries to maintain the minimum possible ratio between height and width. When the layout_height is set to wrap_content, then this ratio is bounded by the height and the width is shrunk.
But by switching to match_parent for the layout_height, then the ratio is bigger and bounded that the width that now fills the entire screen.
Not sure if that's the correct explanation, but that's what I deduce from this observation.
Even though this is counter-intuitive, that may sound logical in that way...
Also, I tried on both landscape and portrait, both works after this change.
Finally, note that I have strictly nothing related to window configuration in my java code:
final Dialog dialogLayout = new Dialog(mContext);
dialogLayout.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogLayout.setCancelable(false);
View layout = mContext.getLayoutInflater().inflate(R.layout.popup_dialog_introduce_myself, null);
dialogLayout.setContentView(layout);
// ...
dialogLayout.show();

LinearLayout animate() clips the view, clipChildren doesn't work

I'm trying to animate the container LinearLayout (20dp) and btn button (20dp) (moving them right), but they get clipped after the full width of the main LinearLayout (40dp). I have tried clipChildren="false" on both, but it doesn't work. I can't use match_parent sizes for layouts, because it is an overlay app and will block the touches behind.
Any ideas?
My layout:
<LinearLayout
android:id="#+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false" >
<LinearLayout
android:id="#+id/container"
android:layout_width="20dp"
android:layout_height="80dp"
android:clipChildren="false"
android:clipToPadding="false" >
</LinearLayout>
<ImageButton
android:id="#+id/btn"
android:layout_width="20dp"
android:layout_height="80dp"
android:padding="0dp"
android:background="#drawable/btn" />
</LinearLayout>
My (simplified) code:
super.onCreate();
windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT, //can't have MATCH_PARENT
LayoutParams.WRAP_CONTENT, //can't have MATCH_PARENT
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
main = (LinearLayout) inflater.inflate(R.layout.main,null);
btn = (ImageButton)main.findViewById(R.id.btn);
container = (LinearLayout)main.findViewById(R.id.container);
btn.setOnClickListener(onClickListener);
windowManager.addView(main, params);
}
Animation code:
public void onClick(View v) {
btn.animate().xBy(100f).setDuration(2000);
container.animate().xBy(100f).setDuration(2000);
}
Turns out you cannot draw outside the bounds of the initial area that was added to the screen using windowManager.addView.
In my case to animate the views I had to use windowManager.updateViewLayout(view,layoutParams).

Categories

Resources