I have something like that:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/photo1"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/photo2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<Fragment>
</Fragment>
</FrameLayout>
And I want to resize that photo2 FrameLayout.
public void setScreenSize() {
Display display = getWindowManager().getDefaultDisplay();
FrameLayout layout = (FrameLayout) findViewById(R.id.photo2);
int screen_height = display.getHeight();
screen_height = (int) (0.60*screen_height);
LayoutParams parms = (LayoutParams) layout.getLayoutParams();
parms.height = screen_height;
}
Unfortunately this doesn't work and I don't know why.
Are you setting the height back to the layout?
public void setScreenSize() {
Display display = getWindowManager().getDefaultDisplay();
FrameLayout layout = (FrameLayout) findViewById(R.id.photo2);
int screen_height = display.getHeight();
screen_height = (int) (0.60*screen_height);
LayoutParams parms = (LayoutParams) layout.getLayoutParams();
parms.height = screen_height;
// Set it back.
layout.setLayoutParams(parms);
}
If you want to change the size of FrameLyaout programmatically then copy and paste this code, its working 100%.
FrameLayout frame1 = (FrameLayout) findViewById(R.id.framelayout1);
frame1.getLayoutParams().height =460;
frame1.getLayoutParams().width = 350;
frame1.requestLayout();
Related
I'm looking for a way to get my two imageviews on the same size.
Imageview A is : width: match_parent
height: wrap_content
scaleType: centerInside
adjustViewBounds: true
Imageview B is :
width: match_parent
height: wrap_content
And ImageView B is over Imageview A. But, the height of A is determinated by his content. So I would like that my ImageView B be at the same size than A.
I tried this : (In my RecyclerView adapter).
final ImageView v2 = holder.miniatureVideo;
final ImageView shadow2 = holder.shadow;
v2.post(new Runnable() {
#Override
public void run() {
shadow2.getLayoutParams().height = v2.getHeight();
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) shadow2.getLayoutParams();
params.height = v2.getHeight();
shadow2.setLayoutParams(params);
}
});
But there is a little delay, before it is correctly resize. And sometimes I have to scroll to make it resized.
Thanks
Did you try RelativeLayout ?
like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="#drawable/imageA"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#id/image_a"
android:layout_alignBottom="#id/image_a"
android:src="#drawable/imageB"/>
</RelativeLayout>
int finalHeight, finalWidth;
final ImageView imageView1= (ImageView)findViewById(R.id.scaled_image);
ViewTreeObserver vto = imageView1.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
imageView1.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = imageView1.getMeasuredHeight();
finalWidth = imageView1.getMeasuredWidth();
return true;
}
});
First get height of ImageOne as above.. Then Set it to ImageTwo as below
android.view.ViewGroup.LayoutParams layoutParams = imageView2.getLayoutParams();
layoutParams.width = finalWidth;
layoutParams.height = finalHeight ;
imageView2.setLayoutParams(layoutParams);
I'm trying to determine the screen's width and height to use them in a fragment.
My fragment's layout has the id background.
If inside onViewCreated I use:
background = (RelativeLayout) view.findViewById(R.id.background);
background.post(new Runnable() {
#Override
public void run() {
int width = background.getMeasuredWidth();
int height = background.getMeasuredHeight();
Log.d("askj", width + "+" + height);
}
});
I get 1440+2276
but if I use:
WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
Log.d("askj", width + "+" + height);
or other related methods I get:
1440+2560
I'm using those parameters to set a background and I can clearly see that the whole screen size is not taken if I use the second approach. I don't really want to use that Runnable() so is there any way in which I can solve this ?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:id="#+id/background"
tools:context="com.example.home.background.HomeScreen">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textStyle="bold"
android:layout_centerInParent="true"
android:textSize="22sp"
/>
</RelativeLayout>
You can measure the background View before your activity has added it to its view hierarchy by yourself using measure method.
background = (RelativeLayout) view.findViewById(R.id.background);
background.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
int backgroundHeight = background.getMeasuredHeight();
int backgroundWidth = background.getMeasuredWidth();
In my case I measured an inflated View but it had the width and height set to match_paent.
View view = inflater.inflate(R.layout.table_field_text, null, false);
view.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int viewHeight = view.getMeasuredHeight();
Use ViewTreeObserver like Below:
background = (RelativeLayout) view.findViewById(R.id.background);
final ViewTreeObserver vto = background.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout() {
//get View Width and height here
int width = background.getWidth();
int height = background.getHeight();
//Call anyfunction which uses width and height here
function(width,height);
//Then remove layoutChange Listener
ViewTreeObserver vto = background.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
vto.removeOnGlobalLayoutListener(this);
} else {
vto.removeGlobalOnLayoutListener(this);
}
}
});
Is there any way I can make my HorizontalScrollView (SlidingTabLayout) hover my ViewPager and its fragments instead of displaying them linearly?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zerohora.app.fragments.ContentFragment">
<com.zerohora.app.widgets.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:layout_gravity="top"
/>
</LinearLayout>
Already tried FrameLayout and RelativeLayout, and it didn't work. Swipe stops working after this chang.
Also changed their marginTop, but the ViewPager always get on top of the ScrollView.
The idea here is to hide the HorizontalScrollView (SlidingTab) whener users scrolls a list in a ViewPager fragment.
Thank you
I was able to find a workaround for it:
First bring the view that you want to be on front of the others by calling brinToFront() method:
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.bringToFront();
Then you can place the view wherever you want. If you want to place it on top but just after the action bar, you can do the following:
// Get viewport's heigth
WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int viewportHeight = size.y;
// Get ActionBar's height
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
}
// Get status bar height
int statusBarHeight = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
// Set tabs position
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.topMargin = -(viewportHeight) + actionBarHeight + statusBarHeight;
mSlidingTabLayout.setLayoutParams(params);
//Redraw display
mSlidingTabLayout.invalidate();
mViewPager.invalidate();
I'm trying to add an element to a screen in Android using X and Y co-ordinates that's beneath the screen but when it loads the screen won't scroll.
My code for the layout
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical"
android:id="#+id/ScrollView"
android:background="#16A901"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/inner"
android:background="#b200C4"
>
</RelativeLayout>
</ScrollView>
My Java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xycords);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int maxX = size.x;
int maxY = size.y;
System.out.println("bar maxX "+maxX+" maxY "+maxY);
ScrollView sv= (ScrollView)findViewById(R.id.ScrollView);
RelativeLayout inner = (RelativeLayout)findViewById(R.id.inner);
Button button = new Button(this);
button.setBackgroundResource(R.drawable.black_rect_border_yellow);
button.setText("Test 1 2 3");
button.setX(10);
button.setY(661);//652 is the maxY in my set up so this will obscure part of it
LayoutParams wrap= new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
wrap.setMargins(0, 0, 0, 0);
//wrap.addRule(RelativeLayout.ALIGN_LEFT,R.id.vLine);
//wrap.addRule(RelativeLayout.ALIGN_TOP,R.id.hLine);
inner.addView(button, wrap);
}
I end up with a button where part of it is offscreen but it won't scroll to show the rest.
Any idea what I'm doing wrong?
You must set inner layout_height to a value greater than button y value:
...
inner.addView(button, wrap);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)inner.getLayoutParams();
params.height = 661 + 48;
inner.setLayoutParams(params);
I see some questions about "ImageView"s, but I need a FrameLayout.
I have a FrameLayout, which I need its width:height = 2:1, and the width is fill_parent. There are many image views inside the frame layout, I can just set them width=fill_parent and height=fill_parent.
I don't find a way to do this, please help .
My xml code is:
<FrameLayout
android:id="#+id/cardView"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/frameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/card_style1"
/>
</FrameLayout>
See if this is what you want:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
final FrameLayout target = (FrameLayout) findViewById(R.id.frame_id);
target.post(new Runnable() {
#Override
public void run() {
int width = target.getWidth();
int height = width / 2;
LayoutParams lp = target.getLayoutParams();
lp.height = height;
target.setLayoutParams(lp);
}
});
}