HorizontalScrollView in PopupWindow - android

i try to create popupWindow with preView where user can see images from gallery.
But first and second images are hiden and at the end of scrollView is empty space like in a screenshot.
I am try to use
layout.addView(imageView);
private void showAttachmentPopup() {
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.attachemnts_file_popup, null, false);
PopupWindow pw = new PopupWindow(popupView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, findViewById(R.id.layout_sent).getId());
popupView.setLayoutParams(params);
LinearLayout layout_attachment = (LinearLayout) popupView.findViewById(R.id.layout_attachment);
RoundedImageView roundedImageView = new RoundedImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
lp.setMargins(5, 5, 5, 5);
roundedImageView.setLayoutParams(lp);
roundedImageView.setScaleType(RoundedImageView.ScaleType.FIT_XY);
roundedImageView.setImageBitmap(bitmap);
layout_attachment.addView(roundedImageView);
pw.setOutsideTouchable(true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.showAtLocation(mBtnAttach, Gravity.BOTTOM | Gravity.LEFT, locateView(mBtnAttach).bottom, locateView(mBtnAttach).right);
}
And if I try to add this bitmap few time this error happens.
In my xml just HorizontalScrollView and LinearLayout into here

I ( guess ) i had the same issue, looked the same.
What helped me out was to set adjustViewBounds:
roundedImageView.setAdjustViewBounds(true);
Hope it suits your case!

Related

Android margins not working when the view was added dynamically

I am adding some View to the LinearLayout dynamically:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(100, 100, 100, 100);
view.setLayoutParams(params);
parent.addView(view);
However, the margins are not getting applied. The following:
view.invalidate();
view.requestLayout();
parent.invalidate();
parent.requestLayout();
didn't work. However, if I force the activity to recreate (e.g. turn off and on my mobile phone), the margins get applied. Calling activity.recreate() also works, but it's too slow.
How to force layout to recalculate margins? Probably, there's something wrong in the flow? I tried to add my views to the root after creation, add children before and after applying properties, but this didn't work for me.
UPD:
I tried to repeat the bug programmatically and got it with this code:
LinearLayout base = new LinearLayout(context);
LayoutParams params1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
base.setLayoutParams(params1);
base.setBackgroundColor(Color.CYAN);
root.addView(base);
LinearLayout another1 = new LinearLayout(context);
LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
another1.setLayoutParams(params2);
another1.setOrientation(LinearLayout.VERTICAL);
another1.setBackgroundColor(Color.BLUE);
base.addView(another1);
TextView tv1 = new TextView(context);
tv1.setText("SOME TEST TEXT 1");
tv1.setTextColor(Color.BLACK);
LayoutParams params4 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params4.setMargins(100, 100, 100, 100);
tv1.setLayoutParams(params4);
another1.addView(tv1);
TextView tv2 = new TextView(context);
tv2.setText("SOME TEST TEXT 2");
tv2.setTextColor(Color.BLACK);
LayoutParams params5 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params4.setMargins(100, 100, 100, 100);
tv2.setLayoutParams(params5);
another1.addView(tv2);
I expect margins to be applied, but they are not. What is the correct order of initializing such a view?
So, my trouble was in DP to PX converter that just wasn't initialized at that moment. However, to clarify everything, my example above is just an error (I didn't change params4 to params5 when adding the second TextView.
Also while testing everything, I found out that the order of adding layouts and their parameters doesn't matter at all.
You probably added child views to the parent view in onCreate().
In onCreate(), parent view does not have actual size.
If you have to do it in onCreate(), try following code.
final ViewTreeObserver observer = parent.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(100, 100, 100, 100);
TextView view = new TextView(context);
view.setLayoutParams(params);
parent.addView(view);
observer.removeOnGlobalLayoutListener(this);
}
};
Hope this will help!
Try this:
LinearLayout ll = findViewById(R.id.linearLayout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30,20,30,0);
Hope this will help!

Show a TextView in a FrameLayout

I'm trying to show a TextView in a FrameLayout when the marker is visible.
Like in the code below:
FrameLayout frameLay = new FrameLayout(context);
FrameLayout.LayoutParams layoutParamsFrame = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
frameLay.setLayoutParams(layoutParamsFrame);
TextView theText = new TextView(context);
theText.setText("text_test");
theText.setTextColor(Color.WHITE);
theText.setTypeface(Typeface.DEFAULT_BOLD);
LayoutParams layoutParamsText = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
theText.setLayoutParams(layoutParamsText);
theText.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
first = true;
frameLay.addView(theText);
This does not work for me. The marker is visible and I execute this code, but nothing happens.
How can I fix this?
you can read this post it is explained how to add a TextView to a FrameLayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
((FrameLayout) findViewById(R.id.mainLayout)).addView(mEditText, params);
So basically you have to add to addView two parameters
so becomes
frameLay.addView(theText,layoutParamsText );

how can I set gravity to center in webview to play gif?

I want to play gif in alertdialog.
I write this code:
AlertDialog.Builder builder = new AlertDialog.Builder(myThis);
builder.setCancelable(false);
WebView view = new WebView(myThis);
view.loadUrl("file:///android_asset/gif_pokemon.gif");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.gravity=Gravity.CENTER;
view.setLayoutParams(params);
builder.setView(view);
dlg_p=builder.create();
WindowManager.LayoutParams wlmp = dlg_p.getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER ;
dlg_p.show();
but the problem is gif is not places in center. Its gravity is left.
How can I set it to center?
Wrap the WebView using LinearLayout,
the LayoutParams of the LinearLayout is set to match_parent for the width and wrap_content for the height, if needed fully center then use match_parent for the height too.
set the LinearLayout gravity to center, you can also use centerVertical or centerHorizontal.
Hope it helps :)
AlertDialog.Builder builder = new AlertDialog.Builder(myThis);
builder.setCancelable(false);
LinearLayout linearLayout = new LinearLayout(myThis);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.setGravity(Gravity.CENTER);
WebView view = new WebView(myThis);
view.loadUrl("file:///android_asset/gif_pokemon.gif");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER;
view.setLayoutParams(params);
linearLayout.addView(view);
builder.setView(linearLayout);
dlg_p=builder.create();
dlg_p.show();

SetMargin Not Working in RelativeLayout

drawer.xml; have RelativeLayout id:relative.
I want to show another relative layout on the drawerlayout;
RelativeLayout drawer = (RelativeLayout) getApplicationContext().findViewById(R.id.drawerrelative);
RelativeLayout view = (RelativeLayout) getLayoutInflater().inflate(R.id.relative, (ViewGroup) findViewById(R.id.relative));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
drawer.addView(view, params);
I show relative layout(id:relative) on main layout(id:drawerrelative) and CENTER on screen.Everythings normally but when i set margin ;
params.setMargins(0, 100, 0, 0);
not working. I was add view.requestLayout() but not working..
How i can solve this problem? (Thank you and sory for my bad english)
Try this code block
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
relativeParams.setMargins(0, 100, 0, 0);
relativeLayout.setLayoutParams(view);
relativeLayout.requestLayout();

How to set TextView Dynamically left of paraent?

here is my code i got error params is null
viewDynamic = new TextView(getApplicationContext());
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)viewDynamic.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ABOVE,userEditTxt.getId());
viewDynamic.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
viewDynamic.setText(userEditTxt.getText());
viewDynamic.setTextColor(Color.RED);
viewDynamic.setTextScaleX(2);
viewDynamic.setTextSize(20);
viewDynamic.setPadding(10, 10, 10, 10);
//viewDynamic.setVisibility(View.GONE);
relativeLayout.addView(viewDynamic,params);
If you haven't added the view to any parent container, or havent called setLayoutParams manually, then getLayoutParams() will return null.
Make soure you call viewDynamic.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); before you call viewDynamic.getLayoutParams();, or add the view to a container, so LayoutParams will be generated for the view.
I suggest you add your View like this:
viewDynamic = new TextView(getApplicationContext());
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ABOVE,userEditTxt.getId());
viewDynamic.setText(userEditTxt.getText());
viewDynamic.setTextColor(Color.RED);
viewDynamic.setTextScaleX(2);
viewDynamic.setTextSize(20);
viewDynamic.setPadding(10, 10, 10, 10);
relativeLayout.addView(viewDynamic,params);//you attach params to your View here

Categories

Resources