Fix item in a scrollview - android

I want to have a static imageView inside a ScrollView. I need this image to stay in the same screen position even if the user scrolls. The problem is that the ScrollView accepts only one child element.
<ScrollView
android:id="#+id/scrollView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:id="#+id/finishedGameListView"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/yourTurnListView"
android:layout_marginTop="200dp"/>
<TextView
android:id="#+id/theirTurnTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/yourTurnListView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/their_turn"/>
<ImageView <!-- I want this to be fixed -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/ic_add_circle"
android:layout_below="#+id/theirTurnListView"
android:layout_alignRight="#+id/yourTurnListView"
android:layout_alignEnd="#+id/yourTurnListView"/>
</RelativeLayout>
Thanks

ScrollView can not be used in conjuction with ListView, ListView will not work properly.
If you really want to do this, try this code:
<FrameLayout>
<ScrollView/>
<ImageView/>
</FrameLayout>

Related

Android: How to create a specific horizontal linear layout

I am making an app where there is a custom search bar. I want there to be a search part that is big, a spinner for region selection that is a small square, and a search button that is also a small square.
Here is what I am talking about.
The code I have right now is this:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:hint="#string/summoner_name"
android:layout_width="wrap_content"
android:layout_weight="75"
android:layout_height="wrap_content"
android:id="#+id/summoner_name"/>
<Spinner
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/regions">
</Spinner>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/search_button"/>
</LinearLayout>
However, this makes the search half the screen, and the button for 2/5's the screen, and the spinner for a smaller ammount. Anyone know how to accomplish this?
Thanks :)
give weight 1 to EditText android:layout_weight="1" , and remove weight tags from other components.
Also you will require to set Spinners width statically, if above doesnt work because of Spinner's width part
like this?
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:layout_width="wrap_content"
android:layout_weight="75"
android:layout_height="wrap_content"
android:id="#+id/summoner_name"/>
<Spinner
android:layout_weight="1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/regions"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/search_button"/>
</LinearLayout>
</LinearLayout>

Make scrollview in layout (crash)

I'm trying to make a screen scrollable with scroll view but my application keeps crashing.
What I've done is to replace RelativeLayout by ScrollView, but it doesn't work.
I need your help with this. Here is the code of the screen:
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="0dp"
android:background="#drawable/background01"
tools:context=".MainActivity"
android:id="#+id/relativeLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/id"
android:background="#drawable/lightcontainer"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginBottom="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mainTitle1"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:textColor="#ffffff"
android:shadowColor="#000000"
android:shadowRadius="1.5"
android:shadowDx="2"
android:shadowDy="2"/>
</LinearLayout>
what you are trying to do is not an scrollable view it's just a relative layout. Try to put your code inside a ScrollView component, like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<!-- PUT YOUR CODE HERE -->
</ScrollView>
Make sure you close all tags correctly as well (your relative layout is not closed).
<RelativeLayout>
Content
</RelativeLayout> <-- This closing tag is always necessary

Android minus Margin/Padding

I work with android, I want to draw design like this
There are two LinearLayuots ( blue and white ), I want add picture their borders, I have tryied to use minus margin but not work
Can Anybody help me?
You'd want something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/firstRow"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#013032"
android:orientation="horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#ff3e7730"
android:layout_below="#+id/firstRow"/>
<ImageView
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:layout_width="140dp"
android:layout_height="140dp"/>
</RelativeLayout>
you can use a RelativeLayout for your layout's parent and then add two LinearLayout and one ImageView or Button. and set your Button (or ImageView,etc) to centerVertical and alignParentLeft and give some marginLeft to it!
<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="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.neo.myapplication.MyActivity">
<LinearLayout
android:id="#+id/aboveLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/darker_gray"
></LinearLayout>
<LinearLayout
android:layout_below="#+id/aboveLayout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/holo_green_dark"></LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher"/>
</RelativeLayout>

TextViews on top and bottom, remaining space filled with ListView

I'm trying to put a TextView on top, another on bottom, and a ListView in between using the remaining space.
However, my ListView is taking all the space to the bottom, making the last TextView to not even show up. The only way I can make it show up is to give the ListView a fixed height, like 100dp.
My layout:
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<ListView
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_title" />
<TextView
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_view"
android:text="OMG Test" />
</RelativeLayout>
Use LinearLayout as follows:
<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="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<ListView
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OMG Test" />
</LinearLayout>
This should solve your issue by placing your ListView between the TextViews using both android:layout_above and android:layout_below :
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<TextView
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="OMG Test" />
<ListView
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/id_task_list_test"
android:layout_below="#id/id_task_list_title"/>
</RelativeLayout>
It can be achieved one of the two ways
1) Using Linear Layout (Straight forward)
2) Using Relative Layout with referencing the view position with respected to the other views.
In your case
1) Change Relative to LinearLayout and work will be done.
2)
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<ListView
android:layout_below="#+id/id_task_list_title"
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_title" />
<TextView
android:layout_below="#+id/id_task_list_view"
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_view"
android:text="OMG Test" />
</RelativeLayout>
This can be done through coding by getting the height of screen and alloting layout param width to list view like this
height of list view=screen height-(height of textView1+Height of textView2).
Instead of Using RelativeLayout , use LinearLayout with Orientation Vertical.
I think it solves your problem
Thank you

ScrollView, LinearLayout chaos

I today added a scrollview to my app. I wanted something like google now cards. It worked, but I want it to be fullscreen, but on the bottom. On the bottom i got a EditText and a Button and i want the scrollview to stop there, so that they are both visible. How can i do that? :s
That's my activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/chat_layout"
android:icon="#drawable/ic_linkrholoorange"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".Chat"
android:text = "Now">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<com.linkr.chat.NowLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e3e3e3"
android:orientation="vertical">
<TextView
android:id="#+id/card"
style="#style/nowCardStyle"
android:layout_width="match_parent"
android:layout_height="100dp"
tools:context=".Chat"
android:layout_alignTop="#+id/linearLayout"
android:layout_alignLeft="#+id/linearLayout"/>
</com.linkr.chat.NowLayout>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="10dp"
android:id="#+id/linearLayout">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/chatTextArea"
android:layout_alignTop="#+id/sendButton"
android:layout_toLeftOf="#+id/sendButton"
android:layout_alignBottom="#+id/sendButton"
android:layout_alignLeft="#+id/scrollView"
android:enabled="false"
android:focusable="false"
android:hint="What's on your mind..."
android:visibility="visible"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sendButton"
android:gravity="bottom"
android:background="#drawable/social_send_now"
android:onClick="sendMethod"
android:layout_alignBottom="#+id/scrollView"
android:layout_alignRight="#+id/scrollView"/>
</LinearLayout>
</RelativeLayout>
<!-- The navigation drawer -->
<!--<ImageView
android:id="#+id/drawerimage"
android:layout_width="320dp"
android:layout_height="100dp"
android:layout_gravity="start"
android:background="#drawable/desert"
android:choiceMode="singleChoice"></ImageView>-->
<ListView
android:id="#+id/drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:background="#F3F3F4"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:hint="What's on your mind?"
android:divider="#android:color/darker_gray"
android:dividerHeight="1dp" ></ListView>
</android.support.v4.widget.DrawerLayout>
I hope you can help me
Thanks a lot!
You haven't said what is happening so its hard to say what the problem is. However, yo make this the way it sounds you want I would suggest adding the property
android:layout_alignParentBottom="true"
to your LineareLayout with the EditText and the Button. See if that works. Something like
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="10dp"
android:id="#+id/linearLayout"
android:layout_alignParentBottom="true"> <!-- Add this line here -->
The parent of your EditText and Button is a LinearLayout but they have properties such as these
android:layout_alignTop="#+id/sendButton"
android:layout_toLeftOf="#+id/sendButton"
android:layout_alignBottom="#+id/sendButton"
android:layout_alignLeft="#+id/scrollView"
but these are properties of a RelativeLayout so they won't have any effect, at least not any wanted effect, on Views whos parent is a LinearLayout.
See The Docs for the properties that each ViewGroup has.

Categories

Resources