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).
Related
As the title description, when set layout_gravity for TextView in LinearLayout, this property does not work well, and when I set it through LinearLayout.LayoutParams.gravity, it does not work as well. But for ImageView with same way, it works.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.openssl.LayoutTestActivity">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
android:background="#color/colorAccent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="xml"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/ic_launcher_round"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Java code
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
TextView textView = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.setMargins(10, 0, 10, 15);
textView.setLayoutParams(layoutParams);
textView.setText("Code");
linearLayout.addView(textView);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher_round);
LinearLayout.LayoutParams layoutParams1 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 0, 10, 15);
layoutParams1.gravity = Gravity.BOTTOM;
imageView.setLayoutParams(layoutParams1);
linearLayout.addView(imageView);
in the TextView you are creating programmatically, you are giving the TextView a 15dp bottom margin, If you want to make the TextView itself at the bottom, set the gravity of the layoutParams without giving the view unnecessary margins:
layoutParams.setMargins(10, 0, 10, 0);
And talking about the textView in xml, if you want to align the text itself of the textView to be at the bottom, set the height to match_parent and set view gravity to bottom:
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="xml"/>
for the TextViewin the code, try to set the height to match_parent and give the view a gravity of BOTTOM inside the layoutParams :
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.MATCH_PARENT, Gravity.BOTTOM);
I am programmatically inflating combination of ImageView and TextView inside my LinearLayout.My linear layout is under a horizontal scroll view. But when I set layout_gravity="center_horizontal" to the parent of my linear layout, the content inside linear layout going out of the screen.
See what is happening:
I am using layout_gravity to achieve this
Without layout_gravity it appears like this
But if I use layout_gravity and there are many items inside that HorizontalScrollView then 1 or 2 items don't show. You can see the first uploaded image for understanding the scenario. In the first image I can't scroll more towards right and I can only see see of the ImageView and TextView combination and moreover, one more such combination is out of the screen completely.
Here's the xml structure
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none"
android:id="#+id/hotel_detail_services">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/imageViewLayout"
>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
Java manipulation
#Override
protected void onPostExecute(List<hotel_detail_model> hotel_detail_models) {
super.onPostExecute(hotel_detail_models);
LinearLayout layout = findViewById(R.id.imageViewLayout);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
if (hotel_detail_models.get(0).getTv()==1)
addHotelSpeciality(layout,"TV",R.drawable.tv);
if (hotel_detail_models.get(0).getAc()==1)
addHotelSpeciality(layout,"AC",R.drawable.ac);
if (hotel_detail_models.get(0).getGeyser()==1)
addHotelSpeciality(layout,"Geyser",R.drawable.geyser);
if (hotel_detail_models.get(0).getBus()==1)
addHotelSpeciality(layout,"BUS",R.drawable.bus);
if (hotel_detail_models.get(0).getCab()==1)
addHotelSpeciality(layout,"CAB",R.drawable.cab);
if (hotel_detail_models.get(0).getFood()==1)
addHotelSpeciality(layout,"FOOD",R.drawable.food);
if (hotel_detail_models.get(0).getRailway()==1)
addHotelSpeciality(layout,"Train",R.drawable.train);
if (hotel_detail_models.get(0).getAirport()==1)
addHotelSpeciality(layout,"Airport",R.drawable.airport);
if (hotel_detail_models.get(0).getMedical()==1)
addHotelSpeciality(layout,"Medical",R.drawable.medical);
progressDialog.dismiss();
}
//Add Image Dynamically
private void addHotelSpeciality(LinearLayout layout, String image_name, int image_id) {
LinearLayout linearLayout = new LinearLayout(hotel_details.this);
ImageView image = new ImageView(hotel_details.this,null,R.style.dynamicImageHotel);
TextView textView = new TextView(hotel_details.this,null,R.style.dynamicTextHotel);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen._25sdp),
getResources().getDimensionPixelSize(R.dimen._25sdp));
params.weight = 1.0f;
params.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params2.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
params3.weight = 1.0f;
params2.gravity = Gravity.CENTER_HORIZONTAL;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
textView.setLayoutParams(params2);
image.setImageDrawable( getResources().getDrawable(image_id));
textView.setText(image_name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextAppearance(R.style.dynamicTextHotel);
}else {
textView.setTextAppearance(hotel_details.this,R.style.dynamicTextHotel);
}
// Adds the view to the layout
linearLayout.addView(image);
linearLayout.addView(textView);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(params3); linearLayout.setPadding(getResources().getDimensionPixelSize(R.dimen._15sdp),0,0,0);
layout.addView(linearLayout);
}
Isn't that what you are trying to achieve by using HorizontalScrollView? ScrollViews allow content to be larger than the physical display so when you add enough "hotelSpecialities" it goes out of the screen and you can scroll through it. If you are trying to fit all the content on the screen then you would not use a HorizontalScrollView.
Okay now that I know what you are trying to achieve, putting your ScrollView inside a RelativeLayout will give you that.
This is what worked out for me
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<HorizontalScrollView
android:id="#+id/hotel_detail_services"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/imageViewLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
You can remove the parent LinearLayout for imageViewLayout too if you don't need it. Let me know if you need anymore help!
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.
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>
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();