Confuse about recyclerview and imageview sizes - android-studio

i'm currently using a recyclerview to display a menu consisting of an image each row.
i've created the image to be 1080 x 300 in order so it can be scale down withouth mutch issue
I can't quit understand what width options to use for the RecyclerView, and for the items layouts and imageview.
I need the image view to be, around 150dp in Height and the With to ocupy the entire screen, just like on the editor
EDIT 1:
Now theres a huge empty space between rows

Use this code for ImageView in layout resource file created by yourself like custom.xml i.e. res/layout/custom.xml:
<?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="wrap_content">
<ImageView
android:id="#+id/imgItem"
android:layout_width="match_parent"
android:layout_height="150dp" />
</RelativeLayout>
Use this code for RecyclerView :
android:width="match_parent"
android:height="match_parent"

Try to change the Scale of Image View to Center Crop or Fit XY
Like This:
android:scaleType="centerCrop"

Use RecyclerView in your default activity like this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<ImageView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:src= "" />
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
Note that the height for ImageView is wrap content. This will work fine given that the height of each image is equal

Related

Wrapping two ScrollViews into a LinearLayout

I am struggling to find a solution for one of the Android Studio (Kotlin) class problems by Google: how to add another image that scrolls down with the first ScrollView. I created a LinearLayout that has two ScrollViews within it. The first ScrollView has a large amount of text which scrolls down perfectly. However, in the second ScrollView, for the image above the text, it does not scroll down with the text.
Here is a view of the Design element of the activity_main (the green bar below the star is ImageView1 which is in the second ScrollView):
Here is the code for the ScrollViews in my activity_main:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="#+id/bio_scroll2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/button_onoff_indicator_on" />
</ScrollView>
<ScrollView
android:id="#+id/bio_scroll"
android:layout_width="match_parent"
android:layout_height="463dp">
<TextView
android:id="#+id/bio_text"
style="#style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:text="#string/bio" />
</ScrollView>
</LinearLayout>
Some guidance will be greatly appreciated.
The reason I told you to use only one ScrollView is that you don't need two. You can use any number of views in a Scrollview, just make sure the child of ScrollView is a Layout. You can use any layout and then put whatever you have in that layout. That's how ScrollView is used.
As the official docs of ScrollView state :
Scroll view may have only one direct child placed within it.
To add multiple views within the scroll view, make the direct child
you add a view group, for example LinearLayout, and place additional
views within that LinearLayout.
So, change your code as :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="#+id/bio_scroll2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/button_onoff_indicator_on" />
<TextView
android:id="#+id/bio_text"
style="#style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:text="#string/bio" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Furthermore, You can remove the root LinearLayout if you don't want to add any other view in it and that will make the ScrollView the root layout.
Also, the height as match_parent doesn't work in ScrollView and it works only as wrap_content or any fix size so if you want to make it take the full height of its parent, you've to use android:fillViewport="true" in ScrollView's tag and height of its parent should be match_parent.

ImageView not showing full size in LinearLayout

For fun I am working on the Udacity Popular Movies app. I'm at the point where I'm clicking the movie image in the GridView and going to the detail view where i'm hoping to display the backdrop image on top, title below that, and overview below that. All in LinearLayout with the gravity for everything at the top.
Main Activity
Detail Activity
Here is the code to set the image in the ImageView:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this).load("http://image.tmdb.org/t/p/w500/" + movie.backDrop).into(imageView);
Here is the Detail Layout File:
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="top"
android:layout_gravity="top"
android:orientation="vertical"
tools:context="com.ferr3t.don.gurumovies.MovieDetail">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/text_view_title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/text_view_description" />
</LinearLayout>
I'm not sure why the image is centered and small I want it to Fill the width of the screen. Any help would be greatly appreciated.
Try This in in xml Imageview property
android:scaleType="fitXY"

How to allow softkeaybord cover only defined views?

My layout has the following structure:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent" >
....
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:layout_gravity="bottom"
android:baselineAlignBottom="true"
android:src="#drawable/logotyp" />
</LinearLayout>
When softkeyboard appears, ImageView is moved up and ScrollView is resized. I want to make the ImagView was obscured by softkeyboard and ScrollView was still resized. Is it possible?
I think the solution is to monitor for height change events and then set the ImageView visibility to View.GONE when things get smaller.

Place WebView below GridView in Android Layout?

Below is the xml layout for my Android App. I am trying to positions a webview below the gridview on the screen.
Can anyone help explain what I need to do to get that to work?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/HomePage"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="#+id/Grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:scrollbars="vertical" />
<WebView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/webView1" />
</LinearLayout>
</LinearLayout>
Thanks
I think two minor changes will help make this work for you. First, when creating a LinearLayout, the android:orientation attribute is required to indicate whether the children should be added horizontally or vertically. Since you want one view on top of the other, add android:orientation=vertical to the HomePage LinearLayout. Second, your GridView currently has a android:layout_height of fill_parent. This will cause it to expand vertically and fill up the entirety of the LinearLayout, which will keep your WebView from being displayed. Try changing this attribute to wrap_content.
Making the height of both children wrap_content could possibly cause you to have some empty space in your app. If you need either the GridView or WebView to take up all of the empty space in the view, you can use the android:layout_weight attribute. Assign the view you want to expand to fill all remaining space after the rest are drawn the attribute like this: android:layout_weight=1. (Here, the 1 represents 100% of the remaining space) If you want more granular control over what percentage of the parent your children occupy, look into using decimal values with the android:layout_weight attribute or android:weightSum on the parent in conjunction with a android:layout_weight on each of the children. Hope this helps!
Use android:layout_height="wrap_content" in Gridview
Set orientation attribute in inner linear layout.
android:orientation="vertical"
Give fixed height of WebView and set android:layout_weight="" in both child.
Try below code and change according to your webview height and grid view height.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/HomePage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="2"
android:orientation="vertical">
<GridView
android:id="#+id/Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:scrollbars="vertical" />
<WebView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_weight="1"
android:id="#+id/webView1"
/>
</LinearLayout>
</LinearLayout>
In above code I give Height of WebView 200dp change according to your requirement.

Android app RelativeLayout not showing

so im trying to use a relative layout (for basic app tabs at the bottom of the screen) and it is not showing when i place it inside the LinearLayout after 2 other layouts (a LinearLayout and a ScrollView). The ScrollView contains several textViews and scrolls nicely on its own and the linearlayout before it also works nicely, but the relativelayout that i have tried to place at the bottom of the screen does not work.
Here is the general code for what i am trying to do:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#drawable/common_bg"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="50dp">
<TextView android:layout_height="50dp"
android:text="Heading"
android:gravity="center"
android:textSize="17dp"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_width="fill_parent">
</TextView>
</LinearLayout>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:layout_gravity="top"
android:layout_marginBottom="60dp">
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
android:paddingTop="15dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold"
android:text="#string/text"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/bottomMenu"
android:layout_width="wrap_content"
android:layout_height="60dp" />
</LinearLayout>
UPDATE: the Relative layout that im using is custom in a way that i cant actually show the code for it here, but it contains a radio group with several buttons.
UPDATE 2: Ok, so I solved the problem by manipulating the layout_height="wrap_content" on the 3 layouts (the first linear, the one holding the scroller, and the bottom relativelayout) as well as manipulating the layout_wieght of each of them until I was satisfied with the way it looked... it doesnt seem like this is the best possible solution, but it worked so i cant complain too much lol...
The relative layout in itself wont be displayed in an xml graphical layout or when you are running the app, it needs a child element to occupy the parent layout, try putting a text view inside the relative layout, it'll be displayed, position it right below the linear layout having the scroll view, it'll work properly, i just tried it with your code and it works

Resources