I ran into a strange problem when dealing with AutoCompleteTextViews.
I've got the following layout for my items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:orientation="horizontal"
android:padding="8dp" >
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Some place I already visited"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ToggleButton android:id="#+id/starred"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:background="#drawable/starred"
android:textOn=""
android:textOff=""/>
</LinearLayout>
Now my Adapter sets the Visiblity of the ToggleButton for some items to Gone and for other to Visible. The ítems where it's gone work as expected you can click them and the text gets put into the AutoCompleteTextView but where the ToggleButton is visible the items don't respond to touch. Altough you can interact with the ToggleButton.
Is there a way to make this work with AutoCompleteTextViews or do I have to use another type of View.
Simple make the ToggleButton from the row not focusable(with android:focusable="false" or in code, in the adapter, btn.setFocusable(false)).
Related
I am creating a chatting app in Android.
I was able to create the normal text chat list view with the following as the list item layout:
<?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" >
<TextView
android:id="#+id/chat_msg_view"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/msg_send_time"
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_below="#+id/chat_msg_view"
android:layout_margin="2dp"
android:paddingTop="2dp"
android:textColor="#android:color/holo_blue_dark" />
<TextView
android:id="#+id/msg_status"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/msg_send_time"
android:layout_margin="2dp"
android:paddingTop="2dp"
android:textColor="#android:color/holo_blue_dark" />
</RelativeLayout>
But now i am trying to send images as well via chat. So now i need to replace the first TextView in the above layout to ImageView if content being sent is IMAGE.
What is a good approach to do this.
There are lots of approaches you can use. If you are using a list view adapter you can use itemType to change the entire list item layout (good if you are changing the entire layout, but can be a bit complicated) or you can place an image view at the same location in your layout as the text view and show or hide which ever one you are using. I.E. If showing image, show the image view and hide the text view, if you are showing the text view, show the text and hide the image. Then you don't need to insert/remove views at run time, which is a little more complicated.
Your layout might look a bit like this...
<?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" >
<TextView
android:id="#+id/chat_msg_view"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="#+id/chat_image_view"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:visibility="gone"/>
<TextView
android:id="#+id/msg_send_time"
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_below="#+id/chat_msg_view"
android:layout_margin="2dp"
android:paddingTop="2dp"
android:textColor="#android:color/holo_blue_dark" />
<TextView
android:id="#+id/msg_status"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/msg_send_time"
android:layout_margin="2dp"
android:paddingTop="2dp"
android:textColor="#android:color/holo_blue_dark" />
</RelativeLayout>
Notice how chat_image_view is also aligned to parent left.. it will be on top of the text view, but it has visibility "gone" so you wont see it.
Hope this helps, good luck.
I'm trying to add a footer containing an imagebutton, an textview and another imagebutton to my activity (I want to make an app like whatsapp and it should nearly look the same). I have tried many ways, but all didnt work.
I tried to make a footer like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/chatBottom"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:background="#D8D8D8"
android:layout_alignParentBottom="true" >
<ImageButton
android:id="#+id/emojiButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:src="#drawable/emoji_button"
android:background="#D8D8D8"
android:padding="10dp"
android:contentDescription="#string/empty"/>
<EditText
android:id="#+id/enterMessage"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:layout_toLeftOf="#+id/sendMessage"
android:layout_toRightOf="#id/emojiButton"
android:background="#drawable/textfield_multiline_activated_holo_dark"
android:hint="" />
<ImageButton
android:id="#+id/sendMessage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#D8D8D8"
android:contentDescription="#string/empty"
android:padding="10dp"
android:src="#drawable/action_send_message" />
and add it using include in xml or via addView() in java, but nothing worked properly. Most important is that the footer has its on background colour (always when I tried to set a colour to the layout where the footer is in, the whole activity got this colour) and that the textview resizes automatically.
Best regards, Oliver
I set one Gribview and one listview in an Activity,In the Gridview there is a TextView and a Button,What I want do is ,when I click the button ,can add a data to the listView .and the button and listView in different layout files.
the xml code is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:descendantFocusability="blocksDescendants"
>
<ImageView
android:id="#+id/image"
android:layout_width="180dp"
android:layout_height="160dp"
android:layout_gravity="fill"
android:padding="4dp"
android:scaleType="fitXY" />
<LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:text="prices"
/>
<TextView
android:id="#+id/prices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:text="name"
/>
</LinearLayout>
<LinearLayout android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_weight="9" />
<Button
android:id="#+id/add"
android:layout_width="45dip"
android:layout_height="45dip"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:text="+"
android:layout_weight="0.2"
android:textSize="12pt"
/>
</LinearLayout>
</LinearLayout>
I want to realize when I click the button ,the sequence number of the image I pick can add to the listView.
ADD:
maybe I didn't say it clear.I have gridView and listView in one layout file.In the gridView I define an layout file ,which contain an ImageView and a Button.What I realize is ,when the app run,I have obtain the button's action in getView,What I want:in button events ,how to write the position values in listView.I haven't got the solution yet ,it really bother me ,thankx for any help.
ADD2:
Now I have achieved the adapter.notifyDataSetChanged()adding data in ListView.but I don't know how to obtain the string variable in Acitivity,the string variable is given by text(TextView android:id="#+id/prices") in ListView to Activity.When I refresh View(adapter.notifyDataSetChanged()),I get "0". why?could anyone help?thank a lot.
You'll need to begin by adding a ListView to your layout file, and adding a button handler.
Take a look at Dynamically add elements to a listView Android
This links to a Stack Overflow question/answer that should get you started
I'd like to recreate the CheckedTextView's functionality using my own custom views so I can have two TextViews on the left with a CheckBox on the right, centered vertically between the two TextViews. I have the Layout working for it, which I will include below. I also have it so that when you click on the outer LinearLayout (LinearLayout1) it will pass that click to the checkbox. The only thing that I can't figure out is when you press down on a checkbox it briefly highlights the checkbox (in yellow on my device) before marking it checked. I'd like to have it do the same if you touch anywhere on the outer LinearLayout, but I don't know where I'd need to hook in to make that happen.
Here is my layout.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearLayout1">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:id="#+id/LinearLayout2">
<TextView
android:id="#+id/FieldValueTextView"
android:text="Value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/FieldLabelTextView"
android:text="Label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<CheckBox
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/LinearLayout2"
android:id="#+id/CheckBox"
android:gravity="center_vertical" />
</LinearLayout>
Thanks,
Dan
The problem I was experiencing was solved by putting the following android:focusable="false" on the CheckBox and the two TextViews so that when the ListView is clicked, it gets the focus, not the inner views.
Hope that helps someone else.
I have a ListView that inflate for each row a xml that contain a CheckBox and more TextViews that are in a RelativeLayout.
The problem is that I can't figure out how to pass onClick events from checkbox to back row.
I want to achieve this behavior: The user press the checkbox and the whole list row gets pressed. I saw this behavior if I inflate for each row android.R.layout.simple_list_item_multiple_choice but I can't figure out how to do that without this android specific layout.
Can anybody give me an idea or direction?
Below is the code:
Listview :
<ListView
android:id="#+id/include_sent_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:cacheColorHint="#color/white"
android:layout_alignParentTop="true"/>
And the xml that is inflated for each row:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="#+id/checked_radio_button"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:focusable="false"
/>
<TextView
android:id="#+id/account_number_text"
android:layout_width="100dp"
android:layout_height="fill_parent"
style="#style/config_simple_small_text_view_style"
android:paddingTop="15dp"
android:layout_toRightOf="#id/checked_radio_button"
/>
<TextView
android:id="#+id/account_name_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/config_simple_small_text_view_style"
android:paddingTop="15dp"
android:layout_toRightOf="#id/account_number_text"/>
</RelativeLayout>
The Checkbox consumes focus for the List item. You need to set this in your layout file:
<!-- Must make this non-focusable otherwise it consumes -->
<!-- events intended for the list item (i.e. long press) -->
<CheckBox
android:id="#+id/item_entry_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
/>