Android Wear: How to Scale List View Image? - android

I am trying to create a list view where every item in the list has an Image on top of a line of text. My problem is that I cannot get the Image to be any larger than 40X40, without it obscuring the view of the text underneath it. Here is my xml file:
<com.thinksai.saicloud.WearableListItemLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<ImageView
android:id="#+id/image"
android:layout_height="40dp"
android:layout_margin="2dp"
android:layout_width="40dp"
android:adjustViewBounds="true"
android:src="#drawable/firefox"/>
<TextView
android:id="#+id/name"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed-light"
android:textColor="#000003"
android:textSize="16sp"
android:layout_gravity="center_vertical"/>
<!--android:drawableTop="#drawable/firefox"/>-->
<!--android:drawablePadding="2dp"/>-->
</LinearLayout>
</com.thinksai.saicloud.WearableListItemLayout>
As you can see in the commented out lines of the Textview, I tried to use the "drawableTop" instead of the Imageview, but that didn't work either. The documentation for WearableListView.setMaximizeSingleItem (http://developer.android.com/reference/android/support/wearable/view/WearableListView.html) gives me the impression that Android Wear forces the list to show three items at a time, unless there is only one item in the list. Is this correct? Is there no work around to force the wear to show one item at a time, regardless of the number of items in the list?
NOTE: I don't set the text of the text view here in the xml, I do it programmatically.

Related

How to create something like a mobile homepage with lists of apps?

enter image description hereI want to create something like our mobile phone home screen layout, where there are many apps with their names (including text below the image buttons). How can I do that? I don't want the grey background behind the images. Is there any way that I can remove it?
I tried using a GridView but I don't really like the huge box around the button since I am trying to use an Imagebutton. Also I can't use layout_weight with GridView.
This is what I have:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageButton
android:id="#+id/gallery_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/Attach_Gallery"
android:layout_weight="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
<ImageButton
android:id="#+id/Camera_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/Attach_Camera"
android:layout_weight="1"/>
<ImageButton
android:id="#+id/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/Attach_Gallery"
android:layout_weight="1"/>
<ImageButton
android:id="#+id/gallery_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/Attach_Gallery"
android:layout_weight="1"/>
</LinearLayout>
I want the text can be under each image button and I also want a 2x4 chart with the image buttons and text.
You can also add recycler view on your home screen Layout XML with Recycler View,
You have to add recycler view dependency as well into your app level build.gradle file
as
implementation 'com.android.support:recyclerview-v7:28.0.0'
You can create a Simple List view, Staggered Layout, as well as Grid Layout.
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchMode="columnWidth"
android:numColumns="3"/>
After that, you have to create a Custom adapter for your recycler view like.
RecyclerView.LayoutManager layoutManager1 = new GridLayoutManager(getContext(), 3);
rvSummary.setLayoutManager(layoutManager1);
rvSummary.setAdapter(alarmSummaryAdapter);

Android ScrollView with heavy layout Memory issue

In my app, I made a list view in MainActvity it opens 12 fragment. To open them I used fragmentTransaction.replace. Every fragment has swipe tabs with 3 fragment. In one of them I used two scroll view. I divide the screen
left and right side.Like this [ scroll view | scroll view ]
I wrote every codes in layout file. (Java files are empty I don't know what to write).
Here what scroll views have. (There are 20 items in it)
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:isScrollContainer="false"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="400dp"
android:layout_height="400dp"
android:src="#drawable/akrep_ahmet_kural"/>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:background="#color/burc_akrep"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Ahmet Kural"
android:textColor="#color/white"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:background="#color/burc_akrep"
android:text="10 Kasım 1982"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textColor="#color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="400dp"
android:layout_height="400dp"
android:src="#drawable/akrep_alp_kirsan" />
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:background="#color/burc_akrep"
.
.
.
The problem is when user click the scroll view tab it comes slowly but scrolling has no lags. Then the user go back memory usage stay the same. Then user open another scroll view tab, memory usage increse again and it becomes like stairs. After that force close comes.
So what to do?
You should use either Listview or RecyclerView as it wont load all the views at the beginning but only what are visible plus one or two more which will be quite less resource-intensive than your approach.
It does look like you should be using a ListView and an adapter.
Please also note that "wrap_content" is quite unhelpful for efficiency as changes to the width (for example, setting the text in a TextView with width set to "wrap_content") can cause the entire view to be re-drawn, slowing down the application.

How to dynamically add a TextView/ImageView to a ListView item in Android

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.

Android custom spinner

I have a custom spinner that uses two custom views, one for the drop down and one for the currently selected item.
My problem is, the custom view for the currently selected item is always at least as wide as the longest item in the spinner list and it leaves a sizeable gap on the left hand side of my custom view. If I select the longest item from the spinner, the gap disappears. I have tried android:gravity="left" for my custom view. Also note that this only seems to be an issue with Android 3.0+.
Here is the code for my custom spinner view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/folder_detail" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content">
<TextView android:text="Folder Name"
android:id="#+id/folder_name" android:textColor="#ffffff"
android:layout_width="wrap_content" android:textSize="16sp"
android:textStyle="bold"
android:layout_height="wrap_content" android:layout_marginTop="0dp">
</TextView>
<TextView android:id="#+id/folder_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:text="(0) " android:textColor="#color/light_blue" android:layout_marginLeft="2dp"/>
<ImageView android:id="#+id/icon" android:src="#android:drawable/arrow_down_float"
android:layout_marginRight="1dp" android:scaleType="fitXY"
android:layout_gravity="bottom" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
This is intentional. It prevents the spinner from drastically changing size as the selection changes.
You're not meant to include an arrow as part of the item view; this is normally included as part of the background 9-patch of the spinner itself. The gravity setting on the spinner itself controls the alignment of the selected item view within the spinner. The Holo theme aligns it to the left, while the legacy themes horizontally center.

can't view a listview inside a listview correctly in android app

I want to display data from a very generic JSON string in my android app. One view/screen shall display an arbitrary number of "details" where each detail has an arbitrary number of "values".
I'm trying to use a ListView inside a Listview, and all the data is nicely put into each list, but the lists won't display fully (see image below).
In the lower part of the screen is the first ListView. It contains a number of TextView labels (e.g. Usage and Info), which in turn has a number of values (units, sw version etc). The values is put in a new list which displays a TextView.
The top part is also a ListView, but is displayed ok.
Looks like this:
First part, status tab with the two lists, first (fine) and second (problem):
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="#+id/tabStatus">
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:id="#+id/listThingStatus"/>
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:id="#+id/listThingDetails"/>
</LinearLayout>
The lower ListView with the label and the second ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="70px">
<LinearLayout android:orientation="vertical" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:id="#+id/textDetails" />
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/listThingValues"/>
</LinearLayout>
</LinearLayout>
And finally the TextViews inside the second ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="70px">
<LinearLayout android:orientation="horizontal" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center_vertical">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:id="#+id/textValuesLabel" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30px" android:gravity="right|center" android:id="#+id/textValuesUnits" />
</LinearLayout>
</LinearLayout>
The result is this (see image), where the content in the inner lists is mostly "hidden" (SW version and text below etc). I use "wrap_content" almost everywhere but the content is too wrapped here :(. How and where do I format my XML to make the content in the inner lists display properly?
Image (from emulator, but same result on phone):
http://i.imgur.com/8mcDq.png
Placing a ListView inside a ListView will only bring you headaches.
Try using an ExpandableListView instead.

Categories

Resources