how to focus an activity to its top elements - android

i have an activity in which, on top i have a image slider, below this a layout in the form of grid which covers almost all of my phone's screen and below this at last i have a list view of of size approax half of my phone's screen.(i am using scroll view to scroll elements)
so altogether they are nearly of size twice of my phone's screen.
now whenever i start this activity, the screen automatically gets focused to the bottom part of my elements. in simple words, it does not show me the image slider and upper part of my grid view, it just show the lower part of grid view and the list view.
so i need to scroll it up manually when i start this.
i searched a lot but didn't get any way to focus it automatically to top elements of my activity. please suggest me how to proceed now.
thank in advance.
this is my xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.gehlot.nitin.kitchenspices.Main2Activity"
android:id="#+id/scrollView"
tools:showIn="#layout/app_bar_main2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_weight="1.0"
android:id="#+id/viewPager"/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1.0"
android:text="Shop catagory"
style="#style/textStyle"
android:layout_gravity="center"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:background="#drawable/back"
android:layout_weight="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1.0">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:background="#drawable/back"
android:id="#+id/img1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:background="#drawable/back"
android:id="#+id/img2"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#drawable/back"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:id="#+id/img3"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1.0">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#drawable/back"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:id="#+id/img4"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:background="#drawable/back"
android:layout_margin="5dp"
android:id="#+id/img5"/>
<ImageView
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:scaleType="fitXY"
android:background="#drawable/back"
android:layout_margin="5dp"
android:id="#+id/img6"/>
</LinearLayout>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/lv_main"
android:layout_below="#id/img4">
</ListView>
</LinearLayout>
</ScrollView>

Try to add this line to your direct child of your ScrollView in xml. It prevents the auto scrolling to bottom part.
android:descendantFocusability="beforeDescendants"
Example:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:orientation="vertical">
//children views
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

Add this to your root layout in xml:
android:id="#+id/root_layout"
In your Acivity.java add:
ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
layout.requestFocus();

Related

How to center CardView inside LinearLayout?

I have a portrait display and I want to translate it to landscape mode.
In landscape mode, first child of encompassing LinearLayout, which is also a LinearLayout, should contain CardView with ImageView (rounded, I'm not sure how is that gonna turn out). In second LinearLayout, I would like to have Username and Password EditText next to each other horizontally. And finally, the last LinearLayout should contain Login and Sign Up buttons also horizontally.
I don't know how to center CardView inside first child LinearLayout and how to make ImageView rounded.
This what I have:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:gravity="center"
tools:context=".LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<androidx.cardview.widget.CardView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="match_parent"
app:cardBackgroundColor="#090909"
app:cardCornerRadius="250dp">
<!--<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="145dp"
android:gravity="center"
app:cardCornerRadius="250dp">
</androidx.cardview.widget.CardView>-->
</androidx.cardview.widget.CardView>
<!--<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/login_logo" />-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<!--HERE WILL BE Username and Password-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<!--HERE WILL BE Login and Sign up-->
</LinearLayout>
</LinearLayout>
android:gravity handles the alignment of its children,
android:layout_gravity handles the alignment of itself.
If you want to more rounded card view then fix heigh of cardView.
Give weight to other Linear layout "2", and Layout with card view weight "1" for small size.
Thanks You! Happy Coding
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#abcdef"
android:layout_gravity="center"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="match_parent"
app:cardBackgroundColor="#090909"
app:cardCornerRadius="150dp">
<!--<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="145dp"
android:gravity="center"
app:cardCornerRadius="250dp">
</androidx.cardview.widget.CardView>-->
</androidx.cardview.widget.CardView>
<!--<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/login_logo" />-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal">
<!--HERE WILL BE Username and Password-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal">
<!--HERE WILL BE Login and Sign up-->
</LinearLayout>
</LinearLayout>
[This is your project(image) => https://i.stack.imgur.com/68ARf.png ]
your problem is simple. just change your orientation your linear layout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" <!--change this-->
android:layout_weight="1">
<androidx.cardview.widget.CardView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_gravity="center" <!-- and this-->
android:layout_height="match_parent"
app:cardBackgroundColor="#090909"
app:cardCornerRadius="250dp">
</androidx.cardview.widget.CardView>
<!--<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/login_logo" />-->
</LinearLayout>
after this manipulation your cardview image will be in the center

Scrollview top and bottom

I'm building and android application using android studio and I'm using scroll view in my layout, but the problem is that the scroll view doesn't start after the action bar and leaves huge white space at the bottom (as you can see in the screenshots below).
and here's my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="false"
android:background="#fefcf8"
android:orientation="vertical"
android:weightSum="1">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/info1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/info1" />
</LinearLayout>
.
.
.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/info6"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/info6" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:background="#3a3c3a"
app:menu="#menu/navigation"
app:itemTextColor="#ffffff"/>
</LinearLayout>
As you can see my xml above, i didn't any padding or margin. Yet there's a white space at the bottom and the scroll view starts at the very top of the activity not after the action bar. How to solve this?
Try by putting this inside your ScrollView or FrameLayout containing ScrollView
<ScrollView
...
app:layout_behavior="#string/appbar_scrolling_view_behavior"
...>

LinearLayout position is shifting to up , When video is playing in Videoview

When the application is launched layout is fine. But when the video is playing. the Layout of the button is moving up.
How to resolve this?
When the application is launched?
When the video is playing the button is moving upwards
Below I had placed the layoutfile.xml which i had been working on.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:background="#android:color/background_dark">
<ListView
android:id="#+id/VideoList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:foreground="#android:color/background_light" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1"
android:layout_weight="0.7">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:layout_gravity="center_vertical">
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/videoView"
android:layout_gravity="center" />
</FrameLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="New Button"
android:id="#+id/button"/>
</LinearLayout>
</LinearLayout>
You should make the LinearLayout(that contains FrameLayout and Button) height match_parent
No need for weight-sum, just make the layout_weights integers like 1 and 9 instead of 0.1 and 0.9
Also, fill_parent is deprecated, use match_parent instead.
You have a LinearLayout with single child. No need, just put ListView with same layout_params.
Lastly, your vertical LinearLayout on the right should have a height of match_parent.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<ListView
android:id="#+id/VideoList"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#android:color/darker_gray"
android:foreground="#android:color/background_light" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="7">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:layout_gravity="center_vertical">
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/videoView"
android:layout_gravity="center" />
</FrameLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="New Button"
android:id="#+id/button"/>
</LinearLayout>
</LinearLayout>
Edit: Fixed layout Params on LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<ListView
android:id="#+id/VideoList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#android:color/darker_gray"
android:foreground="#android:color/background_light" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="7">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:layout_gravity="center_vertical">
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/videoView"
android:layout_gravity="center" />
</FrameLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="New Button"
android:id="#+id/button"/>
</LinearLayout>
</LinearLayout>
I have done changes in listview "layout_width" and "layout_height" both should be match_parent ,Now it is working

Centering an element on the current Viewport in a Scrollview

I want to display a dot, which is always in the physical center of my device, even when I scroll the content around it, which is in a scrollview. This is my current code, but the dot is missing here, because I don't know where to place it. The scrollposition is only set via code, so it wouldn't be a problem, if the centered element blocks the scrollview.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:deviceIds="wear_square"
android:gravity="center">
<android.support.v4.widget.NestedScrollView
android:id="#+id/vertsv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<HorizontalScrollView
android:id="#+id/horisv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/map" />
</LinearLayout>
</HorizontalScrollView>
</android.support.v4.widget.NestedScrollView>
Solution :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:deviceIds="wear_square">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="#+id/vertsv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<HorizontalScrollView
android:id="#+id/horisv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/map" />
</LinearLayout>
</HorizontalScrollView>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_my_location_black_24dp" />
</RelativeLayout>
Put your dot below anything else in your layout. Also you should use FrameLayout/RelativeLayout instead of LinearLayout as a top parent container.

Distribute grids evenly to fit screen

i am trying to get three rows in my recyclerview to fit the screen perfectly. In other words distributed evenly with no scrolling. I have a toolbar and a recyclerview with two columns/three rows. As of now i have just set the items height to fit the screen close enough but there has to be a better way?
activity_main.xml
<RelativeLayout
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"
tools:context=".MainActivity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_below="#id/toolbar"
android:background="#CCCC"
android:layout_width= "match_parent"
android:layout_height = "match_parent"/>
</RelativeLayout>
item.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="match_parent">
<View
android:id="#+id/colorBlock"
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="#CCCCCC" />
<ImageView
android:id="#+id/item"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerHorizontal="true"
android:padding="16dp"
/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:textColor="#android:color/white"
android:textSize="16sp"/>
</RelativeLayout>
By definition, the RecyclerView is supposed to be used in situations where recycling views can happen - a.k.a. scrollable because large amount of views. I don't mean to say it can not be used for smaller collections, but there's no value in using it upon other layouts. As a matter of fact, not being built for such scenarios, it becomes very difficult to make use of it in scenarios like in this question. If you absolutely want to use RecyclerView, I am not aware of any way to do it in xml only. I think you'll have to implement your own layout manager for this.
If you are open to using other layouts, here's a solution involving a bunch of linear layouts and weights. Not optimal, having to use nested weights, but a solution nevertheless:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/toolbar"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ff0000"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff000"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0f00"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff00f0"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00ff00"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f0ff00"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0fff00"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00fff0"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000ff"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f000ff"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0f00ff"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00f0ff"/>
</LinearLayout>
</LinearLayout>

Categories

Resources