diffculty choosing an item from a list view - java

Hello I've built a list view that in its XML file have 2 layouts with background and 1 text view on each layout,I'm getting from the strings array text, And changing the text view,this is my code for the row I'm getting for for the list view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/purple"
android:layout_weight="1"
android:id="#+id/rl1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/purple"
android:layout_weight="1"
android:id="#+id/rl2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
Now my problem is when I'm trying to choose only one of the boxes on list view on item selected and doing a switch on the positions I'm getting the 2 boxes on the same position so it don't matter what what box I'm choosing, Iv'e try to give the linear layouts id and then use set on click listener on them with no successes,I'll appreciate and suggestions.

I'm not sure I understand.
Do you use this layout to create ListView items ?
If that is the case, the problem may be that when the ListView is refreshed, it inflates instances of this layout (in its default configuration, the components are in their default position). In order to preserve the changes, you will need to save each item's state and modify its layout every time the list is refreshed.

Related

How do make layout in android studio?

I am trying to make an activity which should look like the following:
here
The problem is that it does not get the same when I start the app. The buttons are cut or the text is cut. And if I change the position of one button, everything gets in the upper-left-corner.
My question is: What do I have to do that everything gets the position I want?
I used a Relative Layout. Maybe this is the problem?
<?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" >
<Button
android:id="#+id/button3"
android:layout_width="245dp"
android:layout_height="68dp"
android:layout_below="#+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="83dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="83dp"
android:layout_marginBottom="238dp"
android:text="Info"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="normal" />
<Button
android:id="#+id/button"
android:layout_width="245dp"
android:layout_height="68dp"
android:layout_centerInParent="true"
android:text="Kartenansicht"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="normal" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_above="#+id/button2"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="71dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="73dp"
android:layout_marginBottom="18dp"
android:text="Um zu überprüfen, ob Sicherheitskameras in der Nähe sind, bitte hier drücken:"
android:textSize="20sp" />
<Button
android:id="#+id/button2"
android:layout_width="244dp"
android:layout_height="62dp"
android:layout_above="#+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="83dp"
android:layout_marginEnd="84dp"
android:layout_marginBottom="24dp"
android:text="Start"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
</RelativeLayout>
You have a big mess in your layout. For example:
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
These 2 parameters can't co-exist.
For your desired layout you should use LinearLayout with Orientation set to vertical. That way, all you'll need to do is write your UI objects 1 below the other and they will be placed 1 above the other. To center them, you can then use gravity and layout_gravity
android:layout_gravity="center"
android:gravity="center"
And that should be all.
Edit:
Just for you to know, Relative layout basically places any given object at position 0,0 of the layout. If you create few objects, they will be placed one on top of the other.
use the constraintlayout and for example you give each view a constraint of start and end after you can set the width to 0dp
if you would like to create the design by dragging and dropping then it would be better to use constraint layout instead of relative layout, but if you like to use relative layout anyway, you should use "centerInParent = "true" "in the middle button and set "layout_above = "#+id/your_centered_view_id" for the above button and "layout_below = "#+id/your_centered_view_id" ".

Widget ListView loading view

I'm trying to display a dynamic list on a widget and so far i've been able to display my 3 headers (which are simply textview inside a linearlayout) but for my items (2 textview in one linearlayout inside another linear layout) the view displays Loading... instead of the content (a task label).
I've already bumped getViewTypeCount to 2 in my RemoteViewsFactory to match the number of views returned by the getViewAt.
I can't figure out what is wrong inside this layout that would cause a problem:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="3dp"
android:clickable="true"
android:focusable="true"
style="#style/SelectableItemBackground"
android:id="#+id/widget_item"
>
<LinearLayout
android:paddingLeft="22dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="•"
android:paddingRight="6dp"
android:textSize="17dp"
android:textColor="#color/colorText" />
<TextView
android:id="#+id/widget_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textColor="#color/colorText" />
</LinearLayout>
Thank you for reading :)
To anyone reading this, i have been able to solve it by suppressing the style attribute

How to keep views from disappearing in wrap_content

I have a problem where a view of mine is disappearing in my RecyclerView when the layout of that view is wrap_content. The RecyclerView is match_parent both directions, and uses a LinearLayout that inflates the below xml file. Here is what each piece of the RecyclerView is supposed to look like:
And here is the XML for that:
<?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"
android:layout_margin="10dp">
<View
android:id="#+id/scheduleBlockColor"
android:layout_width="15dp"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/scheduleBlockText"
android:background="#color/colorCougarRed" />
<RelativeLayout
android:id="#+id/scheduleBlockText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/scheduleBlockColor"
android:layout_toRightOf="#id/scheduleBlockColor"
android:paddingEnd="0dp"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingStart="5dp">
<TextView
android:id="#+id/scheduleBlockTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="#string/schedule_block_time_default"
android:textSize="16sp" />
<TextView
android:id="#+id/scheduleBlockClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/scheduleBlockTime"
android:ellipsize="end"
android:padding="2dp"
android:singleLine="true"
android:text="#string/schedule_block_class_default"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/scheduleBlockRoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/scheduleBlockClassName"
android:padding="2dp"
android:text="#string/schedule_block_room_default"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
My question is, how do I keep that colored piece from disappearing when the RecyclerView loads? If I have the parent RelativeLayout above set to match_parent, it works fine. I've tried this answer, but haven't had the same success
It is because of the height set on the <View>. It gets messed up (even with match_parent) by the fact that it doesn't have content. Since the intention is to have a vertical stripe, you could anchor it to the top and bottom of the parent. You already are doing it for the bottom part (kind of, aligning it to the bottom of the text view container), so you only need to take care of the top anchoring:
<View
android:id="#+id/scheduleBlockColor"
...
android:layout_alignParentTop="true"
... />
Instead of padding using layout_margin inside your RelativeLayout.

Elements below the expandable list - not floating footer

Is there any option to put elements below the expandable list, let me explain: I already implemented elements below the list, but they are behaving like footer - floating at the bottom of the screen.
But if i place elements below the list in xml, elements become hidden when any group is expanded.
at the moment my layout is behaving like this
http://shrani.si/f/2s/ba/ga2VKfi/lay2.jpg
but i want to be like this at the end of the list
http://shrani.si/f/42/4m/2GAcNu6T/lay3.jpg
My layout at the moment
<LinearLayout
android:id="#+id/linearLayoutListExer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#ffffff"
android:gravity="center_vertical"
android:orientation="vertical">
<ExpandableListView
android:id="#+id/ExerciseELV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayoutStatus"
android:layout_weight="1"
android:groupIndicator="#null" />
<LinearLayout
android:id="#+id/relativeFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/linearLayoutListExer"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="#+id/textViewBellowExercise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="The following exercises will help you lose body weight"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textViewBellowExercise">
<Button
android:id="#+id/buttonFaceit"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#color/green"
android:text="FaceIT!"
android:textColor="#color/white" />
<Button
android:id="#+id/buttonChallenge"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#color/red"
android:text="Challenge"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Your following property should change.
Instead of
android:layout_below="#id/linearLayoutListExer"
you should write
android:layout_below="#id/ExerciseELV"
for the LinearLayout with android:id="#+id/relativeFooter"
This will make the bottom layout appear below the listview
I'm new to android so i'm sorry for any "stupid" questions i may be asking.
I ended up using addfooterview method which works great.
You need to separate footer layout in its own xml and then inflate it and add it to expandable listview.
View footer=inflater.inflate(R.layout.activity_main_footer,null);
elv.addFooterView(footer);

GridView not conforming to Relativelayout alignment

I'd like to right align this gridview so that it's flush against the right side of the screen, but it seems to always immediately follow whichever textview I set it to the right of. Not only that, but all of the textviews are displayed on top of each other.
<?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">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/time_left"
android:textSize="30sp"
android:layout_alignParentLeft="true"
android:id="#+id/time_left_label"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/time_left"
android:layout_alignParentLeft="true"
android:layout_below="#id/time_left_label"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/progress"
android:textSize="30sp"
android:layout_alignParentLeft="true"
android:layout_below="#id/time_left"
android:id="#+id/progress_label" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/progress"
android:layout_alignParentLeft="true"
android:layout_below="#id/progress_label"/>
<GridView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/main_grid"
android:numColumns="2"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/time_left_label"
android:stretchMode="none" />
</RelativeLayout>
Here's a picture of what I want drawn quickly in paint!
http://imgur.com/x7Ypr
I'd like to right align this gridview
How exactly do you want it aligned? You mention what it does, but not what you want.
it seems to...follow whichever textview I set it to the right of
Welll, you're using layout_alignParentRight and layout_toRightOf.
the textviews are displayed on top of each other.
Placing your TextViews underneath each other won't stack them. You can use a LinearLayout with its orientation set to vertical or you could use layout_below to place them under each other.

Categories