I'm trying to make a list, much like in reddit, where there is a upvote button on the left and some text to the right of that (a title, user who asked, number answers).
I'm trying to make a list containing viewgroups like so:
I haven't been able to add that Upvote button on the left side of the image.
Whenever I add a button there, I can no longer click on the list item itself (previously if you clicked on the list item it would take you to another intent)
How can I create a list, where each item of the list is clickable to go to an intent based off that row item, but also have an upvote button for each row item of the list?
Here is what I am currently at:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="2dip"
android:paddingBottom="2dip"
>
<Button
android:layout_width="16dip"
android:layout_height="8dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:layout_marginTop="4dip"
android:background="#drawable/vote_up_gray"
android:id="#+id/vote_up_button"/>
<TextView
android:layout_width="40dip"
android:layout_height="32dip"
android:layout_alignParentLeft="true"
android:layout_below="#id/vote_up_button"
android:singleLine="true"
android:ellipsize="marquee"
android:gravity="center"
android:textSize="14sp"
android:textStyle="bold"
android:text="0"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/Q_list_descriptions">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/title_text_view"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/askedBy_text_view" />
<TextView
android:layout_width="fill_parent"
android:gravity="right"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/answers_text_view" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Use RecyclerView instead. Never had such problems with it. You can specify OnClickListener for each item in your list_item, and also for the list_item as a whole, inside a ViewHolder in your adapter.
https://www.codexpedia.com/android/defining-item-click-listener-for-recyclerview-in-android/
Related
My app has two sections, stuff to do for today, and for upcoming days.
I am able to hide/show these by pressing on the corresponding button but when I open both, there can be too much info and I can only scroll the listview for the upcoming days and not the entire screen.
I did some searching and I kept seeing ScrollView pop up but I can't get that to work, it crashes my app.
Here is my XML file
<?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" 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="eu.agp_it.agpmobilemover.OverviewActivity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0"
android:gravity="center"
android:id="#+id/layout_button_today">
<Button
android:id="#+id/overview_button_today"
android:layout_height="wrap_content"
android:layout_weight="0.95"
android:layout_width="0dp"
android:textSize="20sp"
android:text="#string/overview_today"
android:gravity="center"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:background="#drawable/custom_buttonoverview"
android:onClick="buttonTodayOnClick"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_listview_today"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_button_today">
<ListView
android:id="#+id/listviewtoday"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:visibility="visible"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_button_upcoming"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0"
android:gravity="center"
android:layout_below="#id/layout_listview_today">
<Button
android:id="#+id/overview_button_upcoming"
android:layout_height="wrap_content"
android:layout_weight="0.95"
android:layout_width="0dip"
android:textSize="20sp"
android:text="#string/overview_upcoming"
android:background="#drawable/custom_buttonoverview"
android:gravity="center"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
android:onClick="buttonUpcomingOnClick"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_listview_upcoming"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_button_upcoming">
<ListView
android:id="#+id/listviewupcoming"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:visibility="gone"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
You probably don't need the ScrollView at all. The ListViews already do the scrolling for you. Nesting scrolling views isn't recommended.
Try taking out the ScrollView. Each LinearLayout could take up half the screen, or whatever amount you'd like. You could also use an ExpandableListView which would allow you to expand and collapse the assignments for today and upcoming days based on their header (Today, Upcoming). This way you would only need one ListView. So it would be easier to maintain.
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>
I'm a begginner in android developping.
I'm trying to display 3 ImageButton in the top, one next to the other horizontally, and after those 3 buttons i want to display a list of items.
The problem is that I obtain the listview on top the 3 buttons, so I m not able to click on the buttons.
Here is my layout
<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: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="com.example.twitter.MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="32dp" >
<ImageButton
android:id="#+id/hometimeline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home" />
<ImageButton
android:id="#+id/mytimeline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mytimeline" />
<ImageButton
android:id="#+id/posttweet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Any help please ?
You need to add layout_below to show listview below the buttons.
android:layout_below:
Positions the top edge of this view below the view specified with a resource ID.
First assign an id to the linearlayout which wraps the 3 buttons.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/layout1"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="32dp" >
Later add layout_below=layout1 to show the listview below the layout.
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout1"" />
I am a beginner on Android. I have created 2 activities. First activity has 1 button (say Button1) and 1 TextView (say TextView1) and the Second activity has 1 TextView (say TextView2). The text value for TextView in 1st activity is "Hello World!", when the Button1 is pressed opens up 2nd Activity passing the value of the TextView1 (i.e. "Hell World!"). The 2nd Activity assigns the obtained value to the TextView2 and displays.
All of this is working fine.
When I add a new Button (say Button2) to the 1st activity and write a onClick() method to change the text of TextView1 from "Hello World!" to "Hello Android!" and vice versa. Now when I run this on my device or emulator I cannot see this added button (Button2) it still works as earlier when I click Button1, but I cannot see Button2.
Can anyone help me on this ?
XML Layout : MainActivity
<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: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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/ourButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView"
android:layout_below="#+id/textView"
android:layout_marginRight="37dp"
android:onClick="buttonClick"
android:text="#string/button_string" />
<Button
android:id="#+id/ourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView"
android:layout_below="#+id/textView"
android:layout_marginLeft="50dp"
android:focusable="false"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
</RelativeLayout>
It's because you have android:layout_below="#+id/textView" twice. Try this instead:
<!-- below the previous view : ourButton -->
<Button
android:id="#+id/ourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textView"
android:layout_below="#id/ourButton"
android:layout_marginLeft="50dp"
android:focusable="false"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
Also, don't use #+id/ for align your layout. You use it right when you add an id but when the id is created, you just have to align on it as android:layout_toLeftOf="#id/prevId" with #id.
Example:
Here the layout:
|---- text -----|
<- both are below text
|---- view1 ----|| <- view align right
view align left -> ||---- view2 ----|
As you can see, the left and right alignment are on the "border" of the TextView because you set an alignment below the TextView. So it looks like this:
|------ text ------|
<- both are below text
view1 align left -> ||---- view 1&2 ----|| <- view2 align right
Your views are overlapping each other. If you want to have the two buttons below the text and side by side, try to add a container, like this following:
<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:gravity="center_horizontal|center_vertical"
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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/ourButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:onClick="buttonClick"
android:text="#string/button_string" />
<Button
android:id="#+id/ourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
</LinearLayout>
</RelativeLayout>
Hope this helps.
Please try to remove
android:layout_marginRight="37dp"
and then try. Are you able to see output on xml graphical layout ? When I tried this on my xml I am able to see in Graphical Layout(I used the same code which you mentioned along with your question).
<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: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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/ourButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView"
android:layout_below="#+id/textView"
android:layout_marginRight="37dp"
android:onClick="buttonClick"
android:text="#string/button_string" />
<Button
android:id="#+id/ourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ourButton"
android:layout_marginLeft="50dp"
android:focusable="false"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
</RelativeLayout>
// try this way
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="#+id/ourButton"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:layout_marginRight="5dp"
android:text="#string/button_string" />
<Button
android:id="#+id/ourButton2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:focusable="false"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
</LinearLayout>
</LinearLayout>
In my Java android application I use a listview to display information loading from the SQLite DB And a page footer also there in this page.
The problem is When there are more items loaded to the listview and need to scroll, since footer appears in the bottom of the page, the footer covers the the last item in the listview.
Simply I want to stop overlapping listview items by the footer.
Can any one help me achieve this please. Thanks inadvance.
Edits
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
<ListView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/companylistView"
android:listSelector="#drawable/item_focus_bkg">
</ListView>
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:layout_alignParentBottom="true" android:background="#android:color/darker_gray" >
<TextView android:text="Footer Text"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" >
</TextView>
</LinearLayout>
</RelativeLayout>
Based on your requirement(And as you haven't posted XML layout here that you have tried so far), i assume and can suggest the following:
give bottom margin to your listview: android:layout_marginBottom="60dip"
If you are using RelativeLayout then just give listview as android:layout_above="#+id/footer"
I suggest to you go with 2nd option.
Update:
Based on the XML you have posted, try this correct XML layout:
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/companylistView"
android:layout_above="#+id/textView1">
</ListView>
<TextView
android:text="Footer Text"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true">
</TextView>
</RelativeLayout>
You should use list footer like this:
View footerView = ((LayoutInflater)getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listview_footer, null, false);
list.addFooterView(footerView,null,false);
try this:
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical" >
<ListView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/companylistView"
android:listSelector="#drawable/item_focus_bkg" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#android:color/darker_gray" >
<TextView
android:text="Footer Text"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</LinearLayout>