I have a FrameLayout, containing various overlayed ImageViews:
<FrameLayout
android:id="#+id/australia_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/australia"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/map_australia"
/>
<ImageView
android:id="#+id/nsw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/map_nsw"
android:visibility="invisible"
/>
<ImageView
android:id="#+id/victoria"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/map_victoria"
android:visibility="invisible"
/>
...etc...
</FrameLayout>
In my code I try and render my FrameLayout:
final Dialog dialog = new Dialog((Context) parent);
dialog.setContentView(R.layout.dialog_region);
FrameLayout fl = (FrameLayout) dialog.findViewById(R.id.australia_frame);
final int height = fl.getHeight();
Validate.isTrue(height > 0);
My code crashes with:
java.lang.IllegalArgumentException: The validated expression is false
at org.apache.commons.lang3.Validate.isTrue(Validate.java:180)
The line that's crashing is Validate.isTrue(height > 0);.
Why does my FrameLayout not have a height? Is there a way to get the exact px height and width that my FrameLayout is rendering at?
This may help you
FrameLayout target = (FrameLayout) dialog.findViewById(R.id.australia_frame);
target.post(new Runnable() {
#Override
public void run() {
int width = target.getWidth();
int height = width/2;
/*your code with width and height*/
}
});
}
Related
I want to match an image on all layout.
I got an image here:
I tried everything, here is the code
<LinearLayout
android:id="#+id/mid_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
...>
<TableLayout
android:id="#+id/tiles_tables"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/row1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3">
<ImageView
android:id="#+id/tile1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
/>
<ImageView
android:id="#+id/tile2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
/>
here I got the main function:
ImageView[] tiles = new ImageView[9];
for (int i = 0; i <= 8; i++)
{
tiles[i].setImageResource(getResources().getIdentifier("tile_image1","drawable", getPackageName()));
// this is just to get one image for all imageViews
// here stars the problem
tiles[i].getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
tiles[i].getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
}
I want image to match all tile layout.
Any help would be appreciated.
You Can use list with recyclerView and set grid adapter with 3 colums.
for make square of particular item you need to use ContraintLayout as parent then in chlid apply ratio 1:1 for image.
<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="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="1:1"
tools:src="#drawable/ic_chat_bubble_black_24dp"
/>
</android.support.constraint.ConstraintLayout>
Or you can refer this also
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.item_list, null);
int height = parent.getMeasuredHeight() / 4;
int width = parent.getMeasuredWidth();
view.setLayoutParams(new RecyclerView.LayoutParams(width, height));
return new ViewHolder(view);
}
Is there a way from which I can know what percentage of my layout inside a recyclerView is visible on the Screen.
The layout inflated inside RecyclerView has params of
android:layout_width="match_parent"
android:layout_height="match_parent"
Edit added layout file
Layout file - custom_card.xml
<?xml version="1.0" encoding="utf-8"?
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#id/imageView"
android:layout_alignTop="#id/imageView"
android:scaleType="fitCenter"
android:src="#drawable/image_view_screen" />
</RelativeLayout>
Activity Layout File.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:visibility="gone"
android:layout_width="56dp"
android:layout_height="56dp"
android:id="#+id/progress_bar"
android:layout_centerInParent="true"/>
</RelativeLayout>
I am using a Vertical LinearLayoutManager
After much Try I figured it out. If it helps anyone.
Since my layout height was match_parent it was easier for me. But this method will work for everyone with some calculations.
int firstItemPos=mLayoutManager.findFirstVisibleItemPosition();
View firstItemView=mLayoutManager.findViewByPosition(firstItemPos);
int lastItemPos=mLayoutManager.findLastVisibleItemPosition();
View lastItemView=mLayoutManager.findViewByPosition(lastItemPos);
Now all items between firstItemPos and lastItemPos is visible 100%.
For firstItemView-
Math.abs(firstItemView.getY()) / firstItemView.getHeight()
Similarly for lastItemView-
Math.abs(lastItemView.getY()) / lastItemView.getHeight()
use this;
in your adapter;
private final Rect mCurrentViewRect = new Rect();
... your inits...
in your onBindViewHolder;
View firstItemView = linearLayoutManager.findViewByPosition(position);
int pos= getVisibilityPercents(position);
use these metods;
public int getVisibilityPercents(View currentView) {
int percents = 100;
currentView.getLocalVisibleRect(mCurrentViewRect);
int height = currentView.getHeight();
if (viewIsPartiallyHiddenTop()) {
percents = (height - mCurrentViewRect.top) * 100 / height;
} else if (viewIsPartiallyHiddenBottom(height)) {
percents = mCurrentViewRect.bottom * 100 / height;
}
return percents;
}
private boolean viewIsPartiallyHiddenBottom(int height) {
return mCurrentViewRect.bottom > 0 && mCurrentViewRect.bottom < height;
}
private boolean viewIsPartiallyHiddenTop() {
return mCurrentViewRect.top > 0;
}
btw, dont remember get your recyclerView and linearLayoutManager to your adapter in constructor.
For more => https://github.com/danylovolokh/VideoPlayerManager
So I've read quite a few questions on here as well as tried lots of stuff to try to get this to work. I'm either misunderstanding all of the examples I've read or am looking at the wrong stuff.
What I'm trying to do is set up a relative layout within a relative layout that contains an image view. I want to be able to have the relative layout match the size of the imageview (which I want scaled based on specific screen size) to keep a constant square size. This view will contain buttons as well.When I run the below code I get a square for the image view but the buttons are displayed on the top of the screen. So it appears to me that the imageview is properly scaling down into a square but the layout itself is still matching the overall screen size. I have an XML file set up as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="#+id/layout_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/bg" />
<Button
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_one" />
....
</RelativeLayout>
</RelativeLayout>
Java code below:
public class MainActivity extends ActionBarActivity {
RelativeLayout layoutTwo, mainLayout;
Button buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive;
Button buttonSix, buttonSeven, buttonEight, buttonNine;
ImageView scalingBackground;
int screenWidth, screenHeight, finalSquare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
layoutTwo = (RelativeLayout) findViewById(R.id.layout_two);
scalingBackground = (ImageView) findViewById(R.id.image_view);
initializeButtons();
squareBackground();
}
private void squareBackground() {
screenWidth = mainLayout.getLayoutParams().width;
scalingBackground.getLayoutParams().height = screenWidth;
scalingBackground.getLayoutParams().width = screenWidth;
layoutTwo.getLayoutParams().height = screenWidth;
layoutTwo.getLayoutParams().width = screenWidth;
}
Not quite sure whats going on here, I'd appreciate a kick in the right direction. Thanks
You can use a custom imageview in which height and width are same.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="#+id/layout_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<com.packagename.SquareImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/bg" />
<Button
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_one" />
....
</RelativeLayout>
</RelativeLayout>
Create a class for custom imageview.
public class SquareImageView extends ImageView {
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
I am trying to get the height and width of my imageview so I can scale my image down the the appropriate size but everytime I try to get the height I get the height as 0. I thought maybe it just was not measured yet so I put a onPreDrawListener on the imageview so that I know it would be measured at that time but I still get 0 so I am not sure the problem.
this is my full 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:id="#+id/content"
android:layout_above="#+id/comment_layout">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/comments"
android:background="#android:color/transparent"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/comment_layout">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/send_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/send_button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</RelativeLayout>
here is where I try to get the height
ViewTreeObserver obs = imageView.getViewTreeObserver();
if(obs != null){
obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if(image != null){
imageView.getViewTreeObserver().removeOnPreDrawListener(this);
int height = imageView.getHeight(); // 0
int width = imageView.getWidth(); // 720
}
return false;
}
});
}
the width displays 720 but the height always displays 0.
putting in a hard width also has no effect
This should work with an OnGlobalLayoutListener like so
final ImageView image = new ImageView(this);
image.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
int height = image.getHeight();
}
});
This callback is invoked when the global layout state or the visibility changes
I am developing an android application and I want to set my relative layout's width and height as half of my screen width and height.
Thanks
Use Parent as the LinearLayout and use weightSum and weight attributes
Sample
<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"
android:weightSum="100" >
<RelativeLayout
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50" >
<!-- Your RelativeLayout Items -->
</RelativeLayout>
</LinearLayout>
To Divide a Relative layout into two equal relative layouts
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
</RelativeLayout>
and to Divide a linear layout into two equal relative layouts
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
*#SuppressLint("NewApi")
public static int getDeviceWidth(Activity activity) {
int deviceWidth = 0;
Point size = new Point();
WindowManager windowManager = activity.getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(size);
deviceWidth = size.x;
} else {
Display display = windowManager.getDefaultDisplay();
deviceWidth = display.getWidth();
}
return deviceWidth;
}
#SuppressLint("NewApi")
public static int getDeviceHeight(Activity activity) {
int deviceHeight = 0;
Point size = new Point();
WindowManager windowManager = activity.getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(size);
deviceHeight = size.y;
} else {
Display display = windowManager.getDefaultDisplay();
deviceHeight = display.getHeight();
}
return deviceHeight;
}*
above two function is get device height and width.
Use on this function, you can get half of the size of screen.
I hope its useful to you.
You need to put the RelativeLayout inside a LinearLayout and use android:weightsum=2 and android:layout_weight=1 to get the desired proportion
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2" >
<RelativeLayout
....
android:layout_weight=1
....>
</RelativeLAyout>
</LinearLayout>