I want to make one application which contains multiple choice question. Each question has three option. I want to show that option with one background image. And also want to change the color of that image randomly for each question. How can i do that?
It is possible to create list view by putting differnt background image for each row?
Result.xml
<LinearLayout
android:id="#+id/linearlayoutans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_toLeftOf="#+id/chrono"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_ans_true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/ic_launcher"
android:gravity="center"
android:textColor="#000000" />
<TextView
android:id="#+id/txt_ans_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/ic_launcher"
android:gravity="center"
android:textColor="#000000" />
<TextView
android:id="#+id/txt_ans_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/ic_launcher"
android:gravity="center"
android:textColor="#000000" />
</LinearLayout>
It is possible to create list view by putting differnt background
image for each row?
Yes. Take a look at articles on the internet that back a listview with a custom adapter and a custom view. This is done from the getView() method and there is no shortage of examples / tutorials out there.
Related
I’ve got a layout which consists of multiple views arranged in a relative layout. This layout will be inflated within an adapter of a listview.
Now I want to animate each element of my listview separately (not the element itself, but a textview within it), for instance on an onClick event. The animation should consist of a transition of a textview, and a fading of another one.
I don’t really have an idea here. I’ve got my ArrayList of Objects which holds the data for the elements of my listview. This ArrayList is given to the adapter which fills the textviews.
I guess I need a way now to approach the textviews. But how do I do that? I fiddled around with the getView Method of the Adapter class, without any success. To get the position of the clicked element within the adapter/arraylist is no problem at all.
Any ideads/different approaches?
I tried to attach an ObjectAnimator in my Adapter, which looks like this:
ObjectAnimator animation = ObjectAnimator.ofFloat(holder.txtPlayerPoints,"x", 200);
animation.setDuration(2000);
holder.txtPlayerPoints.setTag(R.id.tag_animation_in,animation);
And in my onClick I tried to start it like so:
((ObjectAnimator ) textView.getTag(R.id.tag_animation_in)).start();
The reference to the textView is correct, but it didn't move. The Debugger also says that the "mTag" porperty of my textView is "null". Any suggestions?
Edit: Here's the layout of each element:
<?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"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="#dimen/bar_upper_height"
android:paddingLeft="#dimen/bar_name_padding"
android:text="Hendrik"
android:textSize="#dimen/font_player_name"
android:id="#+id/txtNamePlayer"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/txt_default"
android:textStyle="bold"
android:textColor="#android:color/white"
android:gravity="center_vertical" />
<TextView
android:layout_width="#dimen/bar_rank_width"
android:layout_height="#dimen/bar_rank_height"
android:layout_marginLeft="#dimen/bar_upper_margin"
android:layout_marginBottom="#dimen/bar_upper_margin"
android:textSize="#dimen/font_bar_info"
android:text="1"
android:id="#+id/txtRankPlayer"
android:layout_alignLeft="#id/txtNamePlayer"
android:layout_alignBottom="#id/txtNamePlayer"
android:background="#android:color/white"
android:textStyle="bold"
android:textColor="#color/txt_default"
android:gravity="center_vertical|center_horizontal" />
<ImageView
android:layout_width="#dimen/bar_arrow_width"
android:layout_height="#dimen/bar_rank_height"
android:id="#+id/imgActivePlayer"
android:layout_toRightOf="#id/txtRankPlayer"
android:layout_marginLeft="#dimen/bar_upper_margin"
android:layout_alignBottom="#id/txtNamePlayer"
android:layout_marginBottom="#dimen/bar_upper_margin"
android:background="#drawable/active_player" />
<TextView
android:layout_width="#dimen/bar_rank_height"
android:layout_height="#dimen/bar_rank_height"
android:layout_marginRight="#dimen/bar_upper_margin"
android:layout_marginBottom="#dimen/bar_upper_margin"
android:text="12"
android:id="#+id/txtPointsPlayer"
android:layout_alignRight="#id/txtNamePlayer"
android:layout_alignBottom="#id/txtNamePlayer"
android:background="#android:color/white"
android:textStyle="bold"
android:textColor="#color/txt_default"
android:textSize="#dimen/font_bar_info"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:layout_width="#dimen/bar_rank_height"
android:layout_height="#dimen/bar_rank_height"
android:layout_marginRight="#dimen/bar_upper_margin"
android:layout_marginBottom="#dimen/bar_upper_margin"
android:layout_toLeftOf="#id/txtPointsPlayer"
android:layout_alignBottom="#id/txtNamePlayer"
android:text="0"
android:id="#+id/txtCurrScorePlayer"
android:background="#color/txt_disabled"
android:textColor="#android:color/white"
android:textSize="#dimen/font_bar_info"
android:gravity="center_vertical|center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:id="#+id/aheadBehindPlayer"
android:layout_width="fill_parent"
android:layout_height="#dimen/bar_lower_height"
android:layout_below="#+id/txtNamePlayer"
android:layout_centerHorizontal="true"
android:background="#color/txt_disabled"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ahead"
android:textSize="#dimen/font_bar_info"
android:textColor="#android:color/white"
android:id="#+id/textView2"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"
android:textSize="#dimen/font_bar_info"
android:textColor="#android:color/white"
android:id="#+id/txtPlayerAhead"
android:layout_weight="5"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"
android:textColor="#android:color/white"
android:textSize="#dimen/font_bar_info"
android:id="#+id/txtPlayerBehind"
android:textStyle="bold"
android:gravity="right"
android:layout_weight="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/behind"
android:textColor="#android:color/white"
android:textSize="#dimen/font_bar_info"
android:id="#+id/textView5"
android:gravity="right"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
I want to animate txtPointsPLayer and txtCurrScorePlayer.
Just attach an ObjectAnimator to each view (using setTag()) and then retrieve it later during onClick. To create an animator you can do something like:
// Animate X from 0 to 200
ObjectAnimator animation2 = ObjectAnimator.ofFloat(myview,"x", 200);
animation2.setDuration(2000);
myview.setTag(animation2)
And then during onClick you get a reference to it and start it
((ObjectAnimator ) myview.getTag()).start()
Hope it helps.
I guess this is pretty much opinion based. Even though, I'm not an android expert, I believe that whatever approach you choose you will still need to get a reference of each TextView inside the ListView, specify the OnClickListener and start the animation. So, I'd approach this in such a way that I don't keep my activity/fragment polluted with child-views-specific logic by creating a custom view that extends from the TextView class where I would define everything related to this TextViews. So, basically, the activity/fragment only needs to know how the ListView...it doesn't care about the items inside it or what the items are doing...this is from an architecture-specific perspective
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.
My goal is to mimic the app list page of Google's Play Store.
to do that, I have made a Relative view for each app box which contains icon, title, developer, etc...
<RelativeLayout
android:layout_width="205px"
android:layout_height="310px"
android:background="#FFFFFF"
android:padding="10px"
android:layout_margin="4px">
<ImageView
android:id="#+id/icon1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_marginTop="5px"
android:layout_marginLeft="5px"
android:layout_marginRight="5px"
android:layout_marginBottom="20px"
android:src="#drawable/icon1" />
<TextView
android:layout_below="#id/icon1"
android:id="#+id/title1"
android:text="#string/t1"
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333333"
android:textSize="20px" />
<TextView
android:layout_below="#id/title1"
android:text="#string/company"
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="15px" />
</RelativeLayout>
I want to put 72 different app boxes in a big scroll view but that requires thousands of lines..
I know I can reuse custom view with but I have know idea how to change the properties of sub-level view(child view). (like Image src of the code above.)
Is there anyway to shorten the line or reuse the custom view?
Or.. Is there a easier way to make 72 different app boxes?
You'll want to use a GridView or ListView, and an ArrayAdapter inflated with the above layout. It's rather hard to help you any further until you investigate this route.
GridView Information
ListView Information
Adapter Information
I am trying a task that should probably be simple..I want one button at the bottom across the bottom of the screen (floating preferably), while I have a scrollable list above that (I was able to do this in a tutorial with a simple listview and buitton).But, my list is a LinearLayout that I fill with a SimpleCursorAdapter and a viewBinder. Since I am using this LinearLayout I keep getting One button per line item, instead of one at the bottom of the screen. I have tried wrapping it with a table layout, relative layout, using two LinearLayouts, etc. Every time I get one button per line. Is this because of the way I am getting the data with a cursor adapter and filling it into the listview? Do I need to use a "merge" in my xml file? Is there a way to make two xml files and then call them both? Do I need to switch to a ListView or another way of displaying the data? This is my first app that I am trying start to finish on my own, so some of this stuff trips me up. I will include my code for the LinearLayout, please note that this is just the list without my extra button added (i deleted all my failed attempts). So I would be looking to modify the code below to contain one button that floats at the bottom of the screen all the time.
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="290dp"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:hapticFeedbackEnabled="true">
<Button
android:id="#+id/BtnToClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:drawableLeft="#+drawable/android_button"
android:gravity="left|center_vertical"
android:background="#android:color/transparent"
android:hapticFeedbackEnabled="true"
android:layout_weight=".1">
</Button>
<TextView android:text=""
android:id="#+id/tvViewRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
</TextView>
<TextView android:text="#+id/text11"
android:id="#+id/text11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
</TextView>
<TextView
android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="left|center_vertical"
android:layout_weight=".20"/>
<TextView
android:id="#+id/text9"
android:layout_column="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="center|center_vertical"
android:layout_weight=".1"/>
<TextView
android:id="#+id/text10"
android:layout_column="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="left|center_vertical"
android:layout_weight=".15"/>
<TextView
android:id="#+id/text12"
android:layout_column="8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:padding="3dip"
android:gravity="left|center_vertical" />
<Button
android:id="#+id/BtnToClick2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler3"
android:gravity="center|center_vertical"
android:background="#+drawable/coup0a"
android:hapticFeedbackEnabled="true"
>
</Button>
Thanks in advance!
-Joe
You need to add the button as a footer or a header according to your needs. You can try this code .The R.layout.header and footer are separate xml layout files which you would have to define.
View header = inflater.inflate(R.layout.header,null);
View footer = inflater.inflate(R.layout.footer,null);
addHeaderView(header);
addFooterView(footer);
You should absolutely use a listview for this job. Listviews are highly optimized for displaying many entries. Just let your activity extend from a ListActivity and create a layout xml file with a listview widget that has the id "#android:id/list" and the listview activity will hook onto that list automatically. You are free to place other widgets in the layout aswell. Here is an example layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:id="#+id/chooseOther"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add_fav"/>
</LinearLayout>
It has a list with a button sitting on the bottom of the screen at all times, even if you have a long list of items.
I am looking to create a list view which contains a image and 2 textviews and one Button.like in this format
Textview1
image Textview2 Button
in the above format i have created another layout .now i want to inflate this layout onto listview.for images say i have 6 images in the drawable folder named as image_1,image_2,image_3,image_4,image_5,image-6.
i am not able to understand from the examples that i have found in google.
<?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">
<ImageView android:src="#drawable/stub"
android:layout_height="50sp" android:layout_width="100sp" android:id="#+id/image"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></ImageView>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="ADD" android:id="#+id/badd"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentRight="true" android:layout_marginRight="15dp"></Button>
<TextView android:layout_height="wrap_content" android:id="#+id/tvname" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:text="TextView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView>
<TextView android:layout_height="wrap_content" android:id="#+id/tvprice" android:layout_width="wrap_content" android:text="TextView" android:layout_alignBottom="#+id/image" android:layout_centerHorizontal="true"></TextView>
Please provide me some android code to achive this.
What you want to do can be achieved using CustomAdapter. Its is the most common thing which is used in android application.
Below links can be useful for you.
http://saigeethamn.blogspot.in/2010/04/custom-listview-android-developer.html
http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx
To get ListView with images(thumbnail) along with description, following tutorial can be helpful to you. And what exactly you are looking for,
Android Custom ListView Example with Images