Apply a different style to certain rows of RecyclerView - android

I have the following use case: using a RecyclerView with a custom adapter for listing a simple set of data (each row has something like an icon, a title, and a description). Think of your favorite email or newsfeed app and this is most likely the same visually.
I've reached the point where I need to stylize some key elements from the list quite differently to indicate a special situation - that they are "unread" or that they are "recommended" (e.g. with a different background color, the title font becomes bold, the icon is made larger, the description is hidden, etc).
All of these variations, being purely representation/visual, I have followed the good practices and defined them as 2 different <style> blocks in the values/styles.xml file. However, I am having a hard time applying one of these 2 styles to the row in the onBindViewHolder() method of the adapter, based on some programmatical condition.
(Of course, I can go down the bad way and modify the styling programmatically by manually calling setBackgroundColor() etc on each individual View, but this seems like an ugly workaround for a seemingly trivial problem; additionally, this way we are skipping all of the benefits the .xml styling provides like resolution or locale specific styles).

Related

Android: Naming Resources

This is a question for Android developers but it is not a programming-related question as it affects nothing but the developer.
What conventions are the most commonly used when naming various resources like colors, drawables and strings and etc?
I have no doubts naming my layouts activity_main or layout_secondary. However, I have always doubts when naming resources mentioned previously. I never know whether I should name these resources after their use or the content. For example:
Color: dark_blue vs text_highlighted
Drawable: blue_gradient vs top_bar_background
String: welcome_to_app vs first_time_prompt
Is there any community-created resource for good practice?
Naming is pretty much personal preference. The name is perfect as long as the name indicates what the functionality of the defined thing is. Also you and any other developer using these definitions should know how what the names mean and which definition to choose. Quite easy if you are consistent with names throughout the project.
For example dark_blue is obviously a blue color while text_highlighted is the color of highlighted text. The name you should use depends on what you need: if you want to classify colors by their name take the first, if you like to abstract from the actual color take the second. For general layouts using text_highlighted will often make more sense since the actual color does not matter and the functionality (text highlight vs text regular etc.) is more important. In this example choosing between text_highlighted and text_regular is a lot more obvious than choosing between color_light_blue and color_dark_blue although they could refer to the same color. The name can help prevent errors.
Android uses prefixes for names in [android.R.drawable]
(http://developer.android.com/reference/android/R.drawable.html) for example:
btn_ for button graphics
ic_ for icon graphics
ic_menu_ for menu icons
ic_dialog_ for dialog icons
stat_ for status icons
The schema is certainly not perfect but the advantage of using prefixes that start with the most generic classification is that you can use code completion to search for specific items step by step. So color_blue_dark could be better than dark_blue_color, at least if you consider the color classification more important than the dark / light classification. The same applies to first_time_prompt. If you have a lot of prompts it would make sense to name them prompt_first_time, promt_other_time, ... If they can be classified by an activity for example that could be used as super category: mainactivity_prompt_*, secondactivity_prompt_* so you know where they belong to.
Android SDK will be a good place to start for the good practices. You can open up any sample code in the SDK and go through the code and see the variable names.
I usually name assets like colors and pictures for their content, but I will name a style or multiple state drawable for it's function.
for example:
button_On.png; button_Off.png; button.xml
That way if I want to use the same resource in multiple places it does not get confusing.
For example using a color as a text color in one style file and a background in another style file.

Listview theming issues

This is probably a simple question, but I really have tried searching here and google with no joy.
I can make listview lists by following the many tutorials on the net. The problem is, they always seem to churn out lists that don't seem similar in appearance to the bulk of the lists I see in apps.
For example, I've attached a screenshot of a menu in the Chainfire3D app. It uses the 'standard' blue dividers, white titles and smaller blue 'description' text for the second line. This style of menu/list is used in many of the market apps I have. Feedly is another example.
Every time I create a listview I get either all white text (or text which is themed by whatever I have used in the layout).
Are there 2 line menus, with white and blue text that are easy to create, because I'm having to specify the colours in XML etc if I want them to look this way.
Also, to get two line listviews I have been using a custom adapter and 2 textviews on top of each other in a layout. I tried a 'simple_list_item_2' and that worked but again it didn't take this 'standard' theme. I'm sure I'm missing something.
Anyone know what?
Ideally, can someone share some code with me that will create a 2 item listviews, ideally (if poss) with a suitable adapter that will allow the running of different activities based on menu items presses. Here's hoping you can help.
The easiest way is to customize your layout.
You can find good examples here or here.

which one is better? -Creating buttons at Run time or design time in android

i need to create around 26 buttons for simple task like display alphabets. i can do this by using layout design.
if i create this button at run time will it give more performance(Considering memory, speed,apk size!)?
Important Requirements:
this layout will be used by 4 different activities.
I need to display 26 buttons at a time to user.
These button may contain background
image.
edit: This layout is like pop up window for other four activities. user can press any alphabets in this layout. As soon as alphabets get selected layout will be closed
Since everything is static residing in your assets, it is fine to have everything in xml files.
Still, one can argue that the 'notion' of parsing the xml layout files of your project introduces an overhead to the process of creating the views.
I would go with a well-designed layout defined in xml.
Yikes. While XML is the best practice answer, 26 of anything screams for some dynamic run-time creation, or at least a combination of the two. You're not going to see much difference in processing time or apk size either way - it will come down to code maintenance down the line.
For instance, consider what will happen when you want to change or add a new attribute, say padding, for each of your letter buttons. Do you want to have to manually go change all those XML elements, or think about a clever regex to properly find/replace?
I'd go with a combination of styles, <include> statement, and run time modification for a comprehensive, maintainable solution. First create a single button styled how you think you want all your buttons to look. Extract your "LetterButton" style out to style.xml and use the android:style="#style/LetterButton" attribute on your button instead. This will allow you to change your style in a single file and have it affect all your letter buttons.
Next, extract the button itself into an <include> file. You can do this by right clicking on the GUI version of the button and choosing "Extract include...". Then arrange your <include>-buttons however you need to, perhaps in a <TableLayout>. Make sure you give each one a unique id, like #+id/letter_button_0 up through _25. The text attribute for all these buttons can be anything, you'll set those dynamically later.
Finally, in your onCreate, define an array of ints of the form {R.id.letter_button_0, ...}, and an array of Strings of the form {"A", ...}, and iterate over those, doing a button = findbyId(int), button.setText(String) to put a letter on each of your buttons.
It may seem like more work this way, but you're doing all the heavy lifting creating a smart UI, so that down the line you can change code in a single place (style or include) and all your buttons will be updated.
Strongly recommend XML layouts for best practice and more understandable code. Also, If you are worried about performance for large view, use relative layouts, these are faster to render than other types of layouts such as LinearLayouts.
Showing XML is best practice:
http://developer.android.com/guide/topics/ui/declaring-layout.html
Also to increase performance keep the Buttons as Activity member variables, then they only need to be loaded once from resources.
Hope that helps,
Marc
You should create it at xml file and make visible and invisible as you need.
You could create a layout with having static assets in it & have dynamic text content & for dynamic backgrounds.You can have use the button properties of gone & visible in it.

How to Get Android System Colors

I've seen references on how to SET system colours, but I need to find out how you GET them - how do you find out what they are?
On the Samsung Galaxy S for example, the tab views, ListView highlights when you select an item, and the Summary text line on the preference screen are all blue.
There are many apps which immitate this style and I want to do the same. Obviously I cannot just hard code and set the colour to Blue, as other handsets use different colours.
The question is, is there a way to programmatically find out what colour the Preference Screen Summary Line, Tabs, or ListView selections are, so that you can then set that against a TextView elsewhere in your app?
How do I get the android system colours?
There is an answer to this question, but it is probably not the one you wanted to hear. There is no way to reliably do this. The "selection color" is actually part of a nine-patch image, which is provided on a platform specific basis. Some use the standard orange color, some (Sense) uses green, and others use red. With an exhaustive list of these you might be able to create a mapping from hardware to color, but this is not very effective because new hardware comes out all the time, and some of these phones allow sense to be uninstalled.
The only real thing you can do is to make your buttons consistent within the application itself, which is a hard enough task by itself. If you really have to have a custom item with a selection color (which is common enough), then my advice would be to copy the button resources from the platform of your choice (I like the default sdk resources myself) and then manually set them throughout your application. This way they will always look the same no matter which platform you are on, and so will always match your custom views. Note that this will require you to do more than just buttons. Dialogs and menus also will need to be modified, which is possible, but hard.
Really this is a flaw in the way Android was designed, and it causes a lot of us grief. I wish I had a better answer for you, but I think this is the best we've got.
You can specify colours to elements in your XML layout using the #android:color system variable:
<TextView android:background="#android:color/white" android:textColor="#android:color/black" />

How to structure Android views in xml logically

I want to put view elements that belong together in a container (e.g. a label and input field). This is useful for example to show/hide these elements all at once. The container, however, is only to group them logically, i.e. I don't want to change the layout with the additional containers.
If I look at HTML, there you can use a div element to structure elements together. When applying a style or removing this element, then its children are affected by that as well. I am looking for something like this in Android.
Android has an abstract ViewGroup, yet I cannot use this directly. Android Studio tells me "Element ViewGroup is not allowed here". I don't want to use a LinearLayout because I don't want to change the layout. Is there a ViewGroup that does nothing, besides adding structure to the XML?
Alternative idea:
Maybe I could use the android: tag attribute for this. Such that I construct a method to "hide all elements that contain tag X". Or more generally "perform action Y of elements with tag X". With this approach I would try to emulate what classes do in CSS/HTML: Give elements attributes, query elements using these attributes, apply styles/actions on these elements.
Does anyone have experience with such an approach in Android?
Bonus question:
When looking at Android I get the feeling that very flexible and useful concepts, which are mature and well known from web development, have been lost. For example, in Android XML you can set one style on a view. However, using HTML/CSS you typically set a multitude of classes to elements and can create a style for each one of them. For instance elements with class "important" should be bold, with class "title" should have a larger font, thus an element with both "important_text" and "title" would be bold as well as large. How would you do this in Android?
Take a look how <include> and <merge> layout tags are working. Probably that's could be close to that you are searching.

Categories

Resources