Static ListView for options - android

I want to achieve this list:
Which is basically a static list with options the user can click on. Is there a way to achieve this statically, with a XML or something like that? Somehow doing the regular listview procedure - adapter, list of items with model object - seems odd to me.

If this list is always the same, it's basically just four stacked, static buttons.
You could then just use a LinearLayout with vertical orientation and place the buttons inside of it.
<?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="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Related

Setting maximum height on RecyclerView

I have a dialog fragment that contains linear layout that involves a titleText above a RecyclerView, and at the very bottom, there's a button below the recyclerView.
Since a recyclerView expands or collapses based on the number of items the adapter sets, the button sometimes gets truncated and no longer appears to be on screen, since the recyclerView just covers the entire screen.
My question is, is there a way to set the maximum height of the recyclerView without ever hiding the button underneath. I also don't want to just give the view a random height just in case the recyclerView contains no items, and it would just be a blank section.
Please let me know if you've ever run into this issue, and how you resolved this. Thanks!
UPDATED
You can achieve this easily using layout weights. Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title"
android:textSize="21sp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="30dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Submit"/>
</FrameLayout>
The Title and RecyclerView will wrap content according to contents and button will always take up bottom place.
I suggest using RelativeLayout as it handles the positioning of views for cases like yours, so that you can actually focus on main design.
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Some title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title"
android:layout_above="#+id/button"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"/>
</RelativeLayout>
Above XML code is the skeleton code for what you need. you can add margins and dimensions to control the spacing. But in any case (until you provide negative margins) your views will never overlap each other.
Main trick of using RelativeLayout is the ability to use XML tags like
android:layout_below or android:layout_above or android:layout_start
or android:layout_end which perfectly aligns your view the way you
want.

Dynamic control inside a layout in android

I have an activity Which has Text to show then Some content under that then a list view.
While some content could be a picture, relative layout, Video control, or a linear layout. Depending upon the data it got from previous activity.
Is that possible to do that or I have to make separate layouts for all items?
You need a layout like this:
<?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="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<FrameLayout
android:id="#+id/flSpecialContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
You can add any View you want in code to the FrameLayout (flSpecialContainer) based on what you need.
you can create all possible items in one layout and set visible and gone them depend on data you receive
in this case you don't need create them in code , and can preview activity layout in design mode.

Sliding List View Layout Android

I am creating an application that contains a header with a button on the right that should slide a list view to left when I click on it(similar to the chat list in Facebook)
-->
When I was including this header without putting the list view in it, it was working properly. But now I am inserting this list view inside the .xml file of the header (because the header is always shown for the user and he can click on this list view button in any layout that is shown for him).
The problem that I want to include this header to all the other layouts without changing the way they looks and show this list each time he click on the button.
I searched and found a lot of answers but every solution was about making the same as I did in the previous screen shots in each page which is not an optimistic solution for multiple pages.
*I am working on API level 10 and want to find the solution without using Actionbar.
Tell me please if more information would be helpful, any help will be really appreciated.
Thanks in advance.
UPDATE my xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GeneralRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout android:id="#+id/rel_layout"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:background="#android:color/white" android:layout_alignParentRight="true">
<LinearLayout android:id="#+id/fake_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"></LinearLayout>
<ListView android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#android:color/background_dark"
></ListView>
</RelativeLayout>
<RelativeLayout android:id="#+id/rel_layout_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<LinearLayout android:id="#+id/lin_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:orientation="vertical"
android:layout_centerHorizontal="true" android:layout_alignParentTop="true">
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slide"
android:layout_gravity="right|center"></Button>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Try to create a RelativeLayout for the ListView with a android:visibility="gone" and all the other components in the same User Interface inside another RelativeLayout.
Then change its visibility upon the state of the menu if it's opened or closed.
Guess this should be a way to go:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:clickable="false">
<LinearLayout
android:id="#+id/thisistopbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="3dp"/>
<include here listview etc../>
</LinearLayout>
you can also make the top bar general and include it in every screen. But the basic idea is like above xml, and put the button on the top bar.

adding button to ListActivity

I'm developing and android application in which I have created a ListActivity which contains an ImageView and a TextView. There is also the ability to drag and drop those objects so that they can be moved. What I want to do now is to have a button at the bottom of the screen,not the list.
As the user scrolls the button will always stay there. I found this link but when i did what this link suggested no button did appear, and instead i got a huge blank space that takes over a quarter of the screen. Can anyone point me to the right direction?
ok so here is the .xml file that is set to the ListActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<DragNDrop.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</DragNDrop.DragNDropListView>
</LinearLayout>
hopefully it can help
Your view should be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/btn"/>
</RelativeLayout>
Populate the listview with the data and see the button will be on its place though list being scrolled.
I think that ListActivity is a bad practice.
What you need to do is to is to change the Activity from ListActivity to Activity. In the layout file, do the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_contant"
android:orientation="horizontal">
<DragNDrop.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_contant">
</DragNDrop.DragNDropListView>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
In on create you can get the list reference by doing:
ListView lv = (ListView)findViewById(R.id.list);
In order to make the button clickable, user OnClickListener

ListView hides the EditText

I am new to Android and am reading Wrox's professional android 4 app dev book. In chapter 4 of the book it explains how to modify the existing text view. The problem i am facing is that the listview in my app hides the edit text box. Its hidden (can be seen in the background) but still works that is more stuff can be added to the list through it. Below is the code for my main activity xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/addItemContentDescription"
android:hint="#string/addItemHint"
/>
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
and my todolist_item xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.wroxexample.ToDoListItemView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="#color/notepad_text"
android:fadingEdge="vertical"
/>
The first option you have is to use a LinearLayout instead of a RelativeLayout.
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/addItemContentDescription"
android:hint="#string/addItemHint"
/>
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
A RelativeLayout will allow you to position the elements relatively to the others.
On the other hand a LinearLayout will position the elements one below the other in the order they appear in the xml file.
The second option you have is to keep your RelativeLayout and just add the following tag to your ListView:
android:layout_below="#id/myEditText"
This will position the ListView below the EditText.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/addItemContentDescription"
android:hint="#string/addItemHint"
android:layout_alignParentTop="true"
/>
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/myEditText"/>
</RelativeLayout>
Use a LinearLayout and the property android:layout_weight
http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html
Try something like this:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:entries="#array/testea"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/myEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/addItemContentDescription"
android:hint="#string/addItemHint"
/>
</LinearLayout>
This way ListView will grow to fill only the unused space.
Timothee got there before me but ill just add a little more.
you can, as he says, use a linear layout, or as user1387035 says, you can set the listview to be below the editText.
Relative Layout means "i want to lay things out relatively" and if you dont tell things where to go they will just float to where the 'gravity' is pulling them. The default gravity is top - so I'm guessing your items both ended up bunched at the top left?
As a rule of thumb - do you want your items to come one after another, bunched together (either horizontally or vertically)? if yes then use linear layout. If you want them to be pushed in different directions, use a relative layout. There are some exceptions, normally involving the "weight" attribute you can set in a linearlayout. (here's one I've just had to use: http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/)
If you have a Relative layout and are just using the layout_below/above attributes, without any 'alignParentBottom' or other thing set, then you probably just want a linearlayout
In your case I would say it sounds like you want Timothee's solution. If you want a little separation between the objects, you can use padding/margins to space them a little.
As for gravities, here is a useful blog entry that helped me get my head around LinearLayout's gravities (as well as generally): http://sandipchitale.blogspot.co.uk/2010/05/linearlayout-gravity-and-layoutgravity.html

Categories

Resources