I currently have a ListView which contains a Radiobutton and a TextView on each row. The ListView uses a custom adapter, and I've given the ListView the android:choiceMode="singleChoice" directive, to insure only a single element could be chosen from the list. However, I can't seem to highlight the selected item.
Here is the XML declaration of the ListView:
<ListView android:id="#+id/employees_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="2dp"
android:paddingRight="1dp"
android:background="#drawable/borderlist"
android:choiceMode="singleChoice"
android:listSelector="#48ad82"
android:layout_below="#id/employees_header">
</ListView>
Here is the XML declaration of the ListView's rows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row_employee"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:descendantFocusability="blocksDescendants">
<RadioButton android:id="#+id/radio_employee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"/>
<TextView
android:id="#+id/text_employeename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_toRightOf="#id/radio_employee"
android:layout_marginTop="3dp"
android:layout_marginLeft="5dp"
android:focusable="false" />
</RelativeLayout>
Since I want to alternate the row's colors, I set a selector on each row by programming, I have this bit of code in the custom adapter's getView:
// Alternate row colors.
if (position % 2 == 0) {
v.setBackgroundResource(R.drawable.selector_list1);
} else {
v.setBackgroundResource(R.drawable.selector_list2);
}
Here is the XML declaration for selector_list1, selector_list2 is identical except for the for the first drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_pressed="false" android:drawable="#color/white" />
<item android:state_pressed="true" android:drawable="#color/lightgreen_listpressed" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="#color/lightgreen_listpressed" />
</selector>
State_pressed works, but I tried using state_activated, state_selected, state_enabled, state_checked and nothing works out to change the color of the selected list item. Am I doing something wrong?
EDIT 1: If I simply throw my selectors out of the window (IE not use them), selection coloring works with only the android:listSelector="#48ad82" directive I have in the ListView declaration. Why doesn't this work with the selectors?
EDIT 2: Tried all the different states again, including state_focused when removing the listSelector directive entirely. Still no results.
EDIT 3: I suppose the issue is that the ListView itself allows the selection, the RelativeLayout view never really gets "selected" persay, thus setting the selector on it is useless. If that's the case, where and how am I supposed to set the selectors to have them do what I want them to do?
EDIT 4: I tried setting colors on the adapter based on this tutorial: http://eureka.ykyuen.info/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview-with-simpleadapter/ No results, I still don't get any selection color unless I call android:drawSelectorOnTop="true" which hides all the elements in the ListView element.
EDIT 5: I'm going to drop this for now and just remove the alternating colors altogether. None of the explanations I have found on StackOverflow or Google talk about this issue. The only way to make this "work" is to call android:drawSelectorOnTop="true", which does change the color of the element currently selected in the ListView, however, it hides everything in that one row.
Related
Is it possible to add multiple TextView inside one ImageButton with colour background ?
The core need is to have a button with the action text on it, and a subtext nearby explaining the action or giving other information related to the action. This subtext can vary from time to time.
Considering this requirement, one solution is to have a normal button and a subtext below, not clickable. But I find it messy. A better approach which I like is, on iOS for instance, to have a clickable UIView containing the action as bold text and the explanation as light text. See the image bellow containing 4 buttons :
How to achieve the same on Android with Java ? The closest I can have is to have an ImageButton bellow a TextView, and it does not sound right.
Is possible to nest TextViews inside an ImageButton ? If not, what is the best alternative ?
I hope this may be useful it explains how to position a textView within and in front of a imageView in the XML.
TextView inside of ImageButton/ImageView XML - Android Dev
Obviously make sure each view has a unique id/name which you can assign as shown here on this link
Sorry I cannot explain specifically myself but it has been a while since developing in Java for Android.
I dont know why you want this behaviour but you can make a container for your views and add a click listener to the whole view. you can also use it anywhere.
an example of this would be.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:background="#drawable/container_background"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
add a selector background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/text_pressed" />
<item android:color="#color/normal" />
</selector>
and the listener
findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I need to create a custom looking SearchView that opens when an ImageButton, that I already have in my MainActivity.xml, is pressed.
I would really appreciate if you could explain me how to do it, because all I could find on SO was either people not having a custom looking SearchView or having it permanently on their TitleBar which I do not have since I'm using the light.NoTitleBar theme.
This is what I would need it to look like, this is a design I made with photoshop:
You have mutliple ways to obtain what you are looking for in term of design.
A solution i use (wich may not be the best) is to put my EditText (your search field) in a FrameLayout, this way i can have a search button and delete text button overlapping my textView:
XML Example :
<FrameLayout
android:id="#+id/SearchTextInputLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:minWidth="100.0dp">
<EditText
android:id="#+id/SearchTextInput"
android:textColorHint="#layout/inputselector"
android:hint="Search terms here..."
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="#+id/ClearSearchButton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="end|center_vertical"
android:background="#android:drawable/ic_delete" />
</FrameLayout>
I've got a single lined TextView with a text in it that is too long to be displayed at once. Now I want the app to scroll horizontally smoothly to a certain position in the text when the user does certain things. So the scrolling is initiated by user actions but the user doesn't decide himself to which position the app will scroll (so I don't want to implement usual scrolling by swiping). I'd like to have a function scrollTo(int position) which accomplishes the scrolling.
Use an EditText and animate the selection property. You can easily style it to behave like a TextView if that's important, for instance:
<EditText
android:enabled="false"
android:background="#null"
android:textColor="#android:color/black"
android:id="#+id/ttt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="very long text"
android:singleLine="true"/>
And then use property animation to scroll smoothly:
ObjectAnimator anim = ObjectAnimator.ofInt(yourEditText, "selection", from, to);
anim.setDuration(duration);
anim.start();
Note that you should not use a hard coded color for the text because on some phones the background might be different (too dark, for instance). If you have your own theme or using Holo light it you're good, but you should be aware of a possible problem.
Use this code
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text=" This post will help to share your urls and text values into social networks like facebook,twitter and linkedin.in facebook we have to share your urls only, but twitter and linkedin able to share both urls and text." />
</LinearLayout>
I have an XML RelativeLayout snippet that I would like to include several times (from a loop) in my main View. The problem seems to be -- is there a way to avoid hard-coding the parent of the RatingBar, since each time I include the RelativeLayout snippet my elements will need to have different ids?
As far as I can tell, the recommended way is to get the layout snippet and then override the android:id for each element to be unique, and then override the android:layout_below manually for each element that has relative positioning. This seems a little kludgy -- is there any way to have these bindings get done automatically?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:id="#+id/relativeView">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="Label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView1" />
</RelativeLayout>
you just need to change the id of the RelativeLayout
like
int BASEID=200;
View v = mLayoutInflator.inflate(R.layout.myRelativeLayout, null);
for (int i;i<10;i++){
v.findViewById(R.id.relativeView).setId(i+BASEID);
}
mRootView.addView(v,...);
then when you need to get the RatingBar for suppose the 4th RelativeLayout you added you can call
RatingBar mRatingBar = (RatingBar)mRootView.findViewById(BASEID+3).findViewById(R.id.ratingBar1);
I have a ListView with some custom sections in it. Each section has it's own header View. I want the elements in the list to be clickable, but obviously do not want the section headers to be clickable. So in the xml for the section headers I added android:clickable="false".
When debugging I noticed that the section headers were still responding to my setOnItemClickListener(). Then I tried setting android:clickable="true" in the XML. And sure enough, the section header views no longer respond to the clicks...
So what is the deal here? Why is it that setting clickable = true telling it that it is NOT clickable? Am I misunderstanding something here? Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:background="#android:color/transparent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingLeft="30dp"
android:clickable="true" />
If I set that clickable="false" at the bottom, then this view begins to respond to the setOnItemClickListener()...
When you set the OnItemClickListener, the event onItemClicked will only be called if the child of the ListView does not have the OnClickListener set. Setting clickable to true will provide the child view (in this case your TextView) with an empty OnClickListener. Since the TextView's OnClickListener is set the OnItemClickListener will not be called.
I think you should not write android:clickable="true" when it was a child of the list item view. If you have a selector for the listitem, just setbackground on the root tag.