Android chat head disables touch on the entire row - android

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.

Related

How to set the cardview height programmatically?

I am new in android development and I have searched this question, but not getting any solution.I want to ask how to make the cardview with recyclerview divide into three pieces in one screen?
this is my XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:id="#+id/card"
android:layout_height="250dp"
app:cardCornerRadius="4dp"
app:cardElevation="1dp"
app:cardMaxElevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.siyamed.shapeimageview.RoundedImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher_background" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
and here is the code I try in Activity
View rootLayout = findViewById(R.id.root_layout);
double height =rootLayout.getLayoutParams().height/3.0;
CardView card= (CardView)findViewById(R.id.card);
CardView.LayoutParams layoutParams= (CardView.LayoutParams) card.getLayoutParams();
layoutParams.height=(int)height;
layoutParams.width=MATCH_PARENT;
Thanks.
try getting height of window and divide it by 3,
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card= (CardView)findViewById(R.id.card);
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) card.getLayoutParams();
layoutParams.height= height;
layoutParams.width=MATCH_PARENT;
card.setLayoutParams(layoutParams);
Just as #V-rund suggested
Get the Screen/Window height and divide it by 3 using,
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card = (CardView)findViewById(R.id.card);
but instead of using CardView.LayoutParams use the LayoutParams for the parent layout of your CardView which in your case will be a LinearLayout.
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams)
card.getLayoutParams();
layoutParams.height = height;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
card.setLayoutParams(layoutParams);
I hope this helps solve your problem.

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>

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).

How to move a DialogFragment out of the center?

Is it possible by using a DialogFragment to be able to move it out of the center and place it anywhere on the screen?
You can use
getDialog().getWindow().setAttributes(param);
in
onCreateView()
like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
getDialog().getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
WindowManager.LayoutParams param = getDialog().getWindow().getAttributes();
param.width = LayoutParams.MATCH_PARENT;
param.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
param.x = 100;
param.y = 100;
.
.
getDialog().getWindow().setAttributes(p);
.
.
}
I have success using
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.gravity = Gravity.TOP | Gravity.RIGHT;
lp.x = 100;
lp.y = 100;
window.setAttributes(lp);
puts my dialog in the Top Right slightly down from the corner. this code is in onCreateDialog().
I suffered a lot trying all the programmatically solutions with no results. Finally I've done it from the XML file without any extra code within the Java class.
All I've done is Make the parent height match_parent and set a gravity for it with value center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<!--The following two lines-->
android:layout_height="match_parent"
android:gravity=“center"
android:orientation="vertical">
<Button
android:id="#+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="#android:color/white"
android:gravity="center"
android:padding="10dp"
android:text="#string/hide"
android:textColor="#android:color/holo_blue_dark" />
<Button
android:id="#+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#android:color/white"
android:gravity="center"
android:padding="10dp"
android:text="#string/cancel"
android:textColor="#android:color/holo_blue_dark" />

Categories

Resources