Listview Item highlight - java

I have an android app where it has two list views in same screen. With one ListView i can get right result(I can highlight the pressed item). If i set the Adapter of the second ListView with setOnItemClick() method of first Listview, my first Listview's setSelected() method don't working. Can anyone help me?
And I also have *.xml files for state_selected tags. I want to highlight only one selected item and not multiple items.

If you just need to change the color of the currently selected item in the ListView, just use change choiceMode attribute to "singleChoice", and select the color using listSelector attribute
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:listSelector="#android:color/darker_gray"
/>

Related

Issue with android studio and radiogroup

I'm trying to make a simple app in android studio but running into trouble with layout whenever I place down a RadioGroup. In my first image here I've created a RadioGroupand dragged two RadioButtons into it by dragging them to the radiogroup option on the component tree trying to drop them into the radiogroup on the layout thing is impossible as the radiogroup is completely invisible on it.
I drag a button below it, lining it up with the center, and the box that shows where the buttons going to go shows it being below the radiogroup. But once I release the mouse button, the button im attempting to place ends up here.
I've been fighting with android studio for a while now but if I get the button even remotely close to the RadioGroup it gets catapulted up the screen, and I don't have enough room if I place the button way down low for my other elements.
tl;dr
radiogroups are catapulting any elements underneath them above them. its probably a layout or gravity issue or something like that. but I don't know how to fix it and would like my buttons to not be underneath all the other elements kthx.
I'm not sure what is causing your issue, because you aren't providing enough information. But If you want the button to appear under your radio group, then in your xml file, give your radio group an id android:id="#+id/radioGroup" and in your button add android:layout_below="#id/radioGroup".
Try to android:layout_below="#+id/radioGroup" in your button.As your are using Relative Layout by default it takes you top while dragging.Here is full Button view.
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:id="#+id/button"
android:layout_below="#+id/yourRadioGroupId"
android:layout_centerHorizontal="true" />

AutoCompleteTextView dropdown style vs spinner dropdown style

I have spinner items showing like this
On 4.1+ My AutoCOmpleteTextView shows the same. However in 5.0+ the AutoCompleteTextView shows white dropdown where spinner shows the same Black as above.
How can i set AutoCompleteTextView to show old black,
Something
spUniversity.setDropDownBackgroundResource()
What resource to put here.
You can style your AutoCompleteTextView
Check it here.
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownSelector="#drawable/list_selector" />
What i did was went to android sdk resources and get the standard spinner background and replaced it with popupbackground.

Scrolling header with two list views

I have a header section that I would like to scroll with a list view below. The problem is that I have a toggle below the header section that allows the user to switch between two list views. What would be the best way to have a header section scroll in sync with the two list views below?
I can share a short overview to help you:
<ScrollView ..>
<LinearLayout>
<HeaderView></HeaderView>
<ToggleButton></ToggleButton>
<ListView></ListView>
<ListView></ListView>
</LinearLayout>
</ScrollView>
The tricky thing is: You have to flip the visibility of the two listView by visible and gone in the toggle button.
1st click on toggle will make it
listView1.setVisibility(View.GONE)
listView2.setVisibility(View.Visible)
2nd click on toggle will make it
listView2.setVisibility(View.GONE)
listView1.setVisibility(View.Visible)

Determining when ListView position changes

fairly new to Android development and I've got a question I've Google'd the crap out of and can't find a solid answer on.
I simply want to know how to determine when the position in a ListView changes. Specifically, I've got a ListView whose TextView text changes color when I click on it. The problem is, when I click on a different position in the ListView, the color of the text of the position I was just on stays what it was changed to.
Basically, is there a way to listen for a position change in a ListView, and then execute code when that position changes?
Thanks a lot!
If you only want one item to be selected at a time make sure you set the choice mode to single when you're setting up your ListView.
myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE)
You'll need to set an onItemClickListener.
myListView..setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
// Your Code Here
}
});
When you set up your ArrayAdapter you can use one of the default Android item layouts such as simple_list_item_activated_1, which highlights the background of the selected item.
myArrayAdapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_list_item_activated_1, myArrayList);
If you want your own custom behavior such as changing the text color rather than the background, then simply replace the item layout with one of your own. Have a look at the default Android item layouts to see how they write them. For example, below is the default Android item layout android.R.layout.simple_list_item_activated_1, where you can see they've used a selector called activatedBackgroundIndicator to define the background color.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
<Additional>
In your case, to get the text color to change instead of the background, you will want to use a selector for your text color. Make a color folder in your res directory. Create a selector in it:
my_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:color="#color/my_red_color />
<item android:drawable="#color/my_blue_color" /> <!-- color when not selected -->
</selector>
Then in your custom item layout set the color of your text to my_color_selector

Can i extend the size of a listview dynamically in java code

I am using a listview in a layout with a size- not in full size of layout.
Below the layout there are components like button.
I need the layout to get extended when more number of listitems adds dynamically.
I have kept the listview and buttons in scrollview.
Is there any attribute or option to make listview of variable length
You should not have a listview inside a scrollview. Instead of having a listview you should add your items to a linearlayout, which will grow to accomodate the items and not enable the scrollbar within itself. This will solve your UI issue. You can set the onclickListeners in a for looop.
Update
<ScrollView.....>
<LinearLayout .....>
<LinearLayout ..../> // this has your list items
<Button .... /> // you can have a layout here if you have multiple buttons.
</LinearLayout>
</ScrollView>
Set the ListView's layout_height value to wrap_content, and it will stretch to accomodate all the items' views in its adapter.

Categories