Android. RadioButtons in a ListView - android

I have dynamically generated ListView, which consists radio-buttons as list items.
Is it possible to use radiogroup functionality in that listview or for these radiobuttons.
I mean, I'd like, that if the user select a radio button the radio button selected before would deselected.
That is my solution at the moment, which I don't like very much. I just save selected radio button and if another one will be selected, deselect the saved one.
Thank you for your suggestions or links.
Here is my layout:
<?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"
android:orientation="vertical"
android:background="#drawable/bg_tile"
android:padding="10dp">
<TextView
android:id="#+id/text_station_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="TEST"
android:textColor="#color/black"
android:background="#color/transparent_white"/>
<ListView
android:id="#+id/list_lines"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rb_lineId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:paddingLeft="50dp"
android:background="#drawable/selector_custombutton"/>
But I also tried with this one:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/bg_tile"
android:padding="10dp">
<TextView
android:id="#+id/text_station_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="TEST"
android:textColor="#color/black"
android:background="#color/transparent_white"/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/list_lines"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RadioGroup>
</LinearLayout>

I think you want android:choiceMode="singleChoice". It's a bit tricky to implement (I don't know the exact specifics myself), but it's a starting point.

Related

For Android is there an example of controls outside list view?

<!-- test.xml -->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/my_location_table_layout_results">
<ListView
android:id="#+id/button_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
<Button
android:id="#+id/fragmenttab1_item_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Help" >
</Button>
</FrameLayout>
I have not found example like this so not sure what I need to do is possible.
I need to open up a dialog that has multiple fragments and on one of those fragments have a textview with button that when the button is selected it
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:id="#+id/my_location_table_layout_results"
android:layout_width="match_parent"
android:layout_height="35dp"
android:stretchColumns="1"
android:shrinkColumns="0"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<EditText
android:id="#+id/fragmenttab1_item_edittext"
android:layout_width="0dp"
android:layout_weight="0.7"
android:gravity="center"
android:textSize="20sp"
android:layout_height="50dp" />
<Button android:layout_alignParentRight="true"
android:id="#+id/fragmenttab1_search_btn"
android:layout_width="0dp"
android:layout_weight="0.3"
android:layout_height="50dp"
android:gravity="top"
android:textSize="20sp"
android:text="Search"
style="WW3dBaseTheme" />
</TableRow>
</TableLayout>
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/test"
android:layout_below="#+id/my_location_table_layout_results" />
</RelativeLayout>
populates a listview of buttons that is shown below the textview/button on the same fragment.
Is that possible? I tried but for some reason instead of just the list of buttons I get copies of the textview/button pair and the button I want in the listview. It is almost as if because I have the listview in the xml it pulls in all the ui controls when it renders inside the listview. Here is the xml>
As I expected it turned out I needed to put the xml for control for the listview in another xml file in order to draw properly in the list.

checkbox'ed list on each tab

I am new to this community as well as to android development.
For my study I need to make a (yes/no)questionnaire divided into 3 groups, some of the questions are quite long, so I want to make a part of it as big text and other part below as a small one.
I from tutorial on dev.and.com and made a TabLayout and for the moment I also have a LinearLayout for my questions
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_large"
android:textSize="16sp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_small"
android:textSize="12sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:gravity="right">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBxc">
</CheckBox>
</LinearLayout>
</LinearLayout>
Same is going to be for all of groups.
Code for tabLayout is like here
And AgroupActivity just displays a line of text right now (for testing purposes).
How can I make my layout to look like a list in each tab I have? Or maybe there are some other possibilities? As I understood, a simple ListView can't have anything(checkboxes in my case) in its tag?
You could use ListView to display a layout look like a list in each tabs.
With each tab content you use a ListView for each tab:
<?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:orientation="vertical" >
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
And custom row (your_custom_row.xml) for each rows in ListView:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:gravity="left"
>
<TextView
android:id="#+id/row_textview1"
android:textSize="25sp"
android:textStyle="bold"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:lines="1"/>
<TextView
android:id="#+id/row_textview2"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="10dp"
android:lines="1"/>
</TableRow>
</TableLayout>
and use Adapter to bind data to your ListView.

relative layout issues/android

I am having issues with creating a menu (not menu when you click menu button -- but menu as in the welcome screen to select items). I went to ImageView and TextView float (left/right) Android and I did not solve my issue. I would like to create at least five or six options to select from on this menu class. This is my menu.xml file. Please let me know what I need to do.
The issue is that the first item appears, but the second item overlaps the first item. I been picking away with this for the last hour and cannot solve this issue.
<?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="wrap_content"
android:padding="5dp">
<TextView
android:textSize="18dp"
android:id="#+id/message_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Check Your Account" />
<ImageView
android:src="#drawable/ic_menu_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/icon" />
<TextView
android:textSize="18dp"
android:id="#+id/message_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" View our Site" />
<ImageView
android:src="#drawable/ic_menu_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
thannk you.
Why don't you use a LinearLayout (like a vertical one with severals little horizontal ones inside, each with a TextView and an ImageView) ?
Anyway, if you want to use RelativeLayout you should use something like android:layout_toRightOf, android:layout_toLeftOf, android:layout_above or android:layout_below in your xml to place your elements.
I advise you to take a look at http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html if you don't know those parameters.
You use a RelativeLayout but you don't place elements with reference to each other.
Example :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="#+id/topBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top"
android:layout_centerHorizontal="true">
</Button>
<Button android:id="#+id/leftBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
android:layout_below="#+id/topBtn"
android:layout_toLeftOf="#+id/topBtn">
</Button>
</RelativeLayout>
http://developer.android.com/reference/android/widget/RelativeLayout.html
A Linear Layout would be better.
Try this and tell me if this is what you needed:
<?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:padding="5dp"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/message_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Check Your Account"
android:textSize="18dp" />
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_add" android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/message_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" View our Site"
android:textSize="18dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_add" android:layout_gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>

layout messed up once spinner has entries

I have
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutPlayer"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/Spinner01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="100" />
<ToggleButton
android:text="#+id/ToggleButton01"
android:id="#+id/ToggleButton01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Which displays a spinner and a next to its a ToggleButton. Everything okay so far. Of course the spinner need some entries, so I add to the spinner the attribute:
android:entries="#array/myentries"
The problem now is that the ToggleButton is a bit lower than the spinner and the button of the ToggleButton is cut off, maybe 3 or 5 lines of pixels.
Anyone an idea what is wrong here? Android is version 2.2
Thanks!
Try wrapping your ToggleButton in a new LinearLayout. Your xml would then be:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutPlayer"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/Spinner01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/planets_array"
android:layout_weight="100">
</Spinner>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ToggleButton
android:text="#+id/ToggleButton01"
android:id="#+id/ToggleButton01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1">
</ToggleButton>
</LinearLayout>
</LinearLayout>

Checkbox state problem in ListView

Hello with regard to the known issue of unwanted checkboxes being checked while scrolling through a listview, can anyone please give me an idea how to use a checkedTextView properly so that i can maintain check state properly when scrolling through the list. I thought the listview takes care of maintaining the checkbox state when using a checkedtextView but that does not seem to be the case. or do i have to use the default Id for the checkedTextView. I can't seem to find an example that uses a cursorAdapter or a SimpleCursorAdapter for this case. Thank you
i have tried using this, but am not really sure what to do with the set tag position. here is a small piece of code:
//bindView() method in SimpleCursorAdapter
//viewHolder holder;
//Cursor c;
holder.checkedText = (CheckedTextView)view.findViewById(R.id.view_checked);
holder.checkedText.setTag(c.getPosition());
holder.checkedText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
v.getTag();
((CheckedTextView) v).toggle();
}
});
my xml standard layout:
<?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">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="multipleChoice"/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_notes"/>
</LinearLayout>
my custom layout:
<?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:orientation="horizontal"
android:padding="6dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/viewNameText"
android:id="#+id/viewNameId">
</TextView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start Date"
android:layout_gravity="right"
android:id="#+id/DateId">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start Time"
android:layout_gravity="right"
android:id="#+id/TimeId">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
>
<CheckedTextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
</LinearLayout>
</LinearLayout>
I thought the listview takes care of maintaining the checkbox state when using a checkedtextView but that does not seem to be the case.
It should, if your ListView is android:choiceMode="multiple".
do i have to use the default Id for the checkedTextView
You might. The only times I have implemented a multiple choice ListView, I have used the standard row layout rather than a custom one.

Categories

Resources