Avoid duplicating UI codes - java

I want to write new UI class which contain Check box and Label. At the moment there is existing UI class with same elements but their element descriptions are different. But data model for both UIs are going to be same.
So is it good practice to keep separate UI classes (by duplicating GridBagConstraints and other stuffs) for each or move common code in to abstract layer and derive description of the UI elements in the implementation level?

There are some other things that you can try, so you can avoid duplicating UI code, I'll give you 2 examples:
You can use the tag to bring the UI code inside another layout file and show it in you current layout, at the end you will be able to call it directly from your current Activity or Fragment in the same way you do with the other elements at the root of your Fragment or Activity class.
Re-using layouts
First layout file named: include_example_layout.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/textView_includeExampleLayout_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="#+id/checkBox_includeExampleLayout_valid"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</merge>
Second layout file named: activity_main.xml
<LinearLayout>
<include layout="#layout/toolbar_search_algolia"/>
</LinearLayout>
And from the MainActivity file you will be able to call the ids of this file include_example_layout as if they were declared directly in your activity_main file, so you will be able to reuse it.
The second one is creating a View element, this has an added advantage to the first method but is a little more complex, the thing is that you will be able to move some UI logic to the class of the new View element, for example if you want to disable the checkbox when something is happening with the information you can move thtat logic to the new view class.
Custom View
I'll no write a complete tutorial about this because it is a extense topic but I'll left some examples in other places that will help you understand the most basic concepts, there are two way in wich you can build CustomViews the first one is extending the View class that will force you to create it from scratch, but you can also extend other Views like a LinearLayout and this will help you to get started with the concept of a CustomView (is not recommended in every case, it can slow down your UI if you don't use it wisely)
Example extending LinearLayout
Example extending View

Related

How would I go about using an OnClickListener for a GridView of ImageButtons?

I have this GridAdapter that shows my working GridView of at least 100+ buttons, but all of these will need to navigate to a page once clicked. I'm unsure of how to begin or if what I have is a substantial place to start. I know where I'd put an OnClick method and listener but I am unsure of how to ensure every button in the grid is heard separately since they don't have Ids. Guides online have been helpful but I'm still lost. If anyone has some advice as to how to proceed I'd be grateful. Both java files are attached but the array of images is not included in the Adapter screenshot.
Grid_Adapter
Grid_Fragment
You could add android:onClick="[method name here]" within the ImageButtons in your XML layout code, to call the code when each button is clicked. Note that this method has to be public and exist within the current activity.
As an example:
<?xml version="1.0" encoding="utf-8"?>
<!-- truncated -->
<Button android:id="#+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:onClick="buttonClickMethod1" />
<!-- truncated -->
Also note that this isn't the preferred method, and that using onClick listeners or data bindings is a better approach.
Finally, I would question your activity layout of having 100+ buttons from a user perspective - that seems very high.

Is it okay to define same ids in Android?

I know that if we define different ids it is easy for us to find it in future. I found many answers but they're not what i'm looking for. I've a java function which is bit lengthy and will be used in many java activities. So it won't be possible for me to change ids for every java and xml activity. So is it okay to have same ids in different XML and JAVA files? Or could they make app crash in any way? IDs example :
XML ACTIVITY ONE :
<TextView android:id="#+id/resultview" />
JAVA ACTIVITY ONE :
printresult = (TextView) findViewById(R.id.resultview);
XML ACTIVITY TWO :
<TextView android:id="#+id/resultview" />
JAVA ACTIVITY TWO :
printresult = (TextView) findViewById(R.id.resultview);
As you can see above there are different XML and JAVA but ids are same.
Yes it is okay to use same id in different in different XML files. The point to be considered here is that the activity inflates the particular layout and the findViewById method finds the view using the id in that particular layout that you inflated. Yes, this doesn't introduce bugs however it is a bad practice.
You can refactor the id's via android studio's refactor function. So if you change your id here android studio will search the usages of that particular id and change them for you.
Right click on the id you want to change.
Click on refactor
Click on rename
You will see the following dialog appear
Then you can change the way of refactor by changing the scope and other things at your comfort.
You will not have problem because the components are in different XMLs. But it's better to put different names for them, you will be more comfortable yourself. For example:
<TextView android:id="#+id/resultview1" />
<TextView android:id="#+id/resultview2" />
there is no problem to give one id to many views but:
the views should be in different layout files
it is not a good practice
There is no problem for duplicated IDs until they used in same file. But it is NOT a good practice.

How do I create custom GUI controls in Eclipse for use in Android app layouts?

OK the header says it.
I want to be able to create my own controls such as buttons, textviews, edittext boxes, spinners, and so on. I can create the images in CS5 but how do I turn those images into functional GUI components? Thanks
You mean you want to create a custom looking version of those controls right?
You could make an ImageButton component for any custom button you made, and use the attribute android:src, like
android:src="#drawable/your_image"
Similarly for EditText you can use the attribute android:background.
Edit:
The android controls are not stored as standard image files anywhere accessible. The standard designs that you mentioned are the default look of the elements.
To change it, you would have to create the background yourself and store it as a .png file in the drawable folder of your project. Make sure you name this file only in lower case and with no special characters like spaces etc.. Copy this file into all the drawable folders(drawable-hdpi,ldpi etc).
Then you have to refer to this file within your box as
<EditText
android:background="#drawable/your_edit_text_graphic_file_name"
android:text="#string/sample_text"
android:layout_width="match_parent"
-- all other attribute declarations --
</EditText>
Similarly, for other elements you would have to use android:src like is mentioned earlier to refer to your own graphic!
Hope this helps!
You just create your CustomView extend View or TextView.... and implement event handle. Android Developer had a training about this Custom Components . When you finish you can find your custom view in GraphicLayout => CustomView in Eclipse and add like normal components.
Images just provide nice appearance not event handle so you can not just make image to component
You don't have to use CSS in case of android you have to use XML file for UI designing.You can create UI in XML statically or dynamically(i.e. Programmatically). You can create button in android in the following manner.
<Button
android:id="#+id/buttonStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your btn Name"/>
You can access this button in your java file by getting id of button and use it.
Hope this helps.
For more info read this post http://developer.android.com/reference/android/widget/Button.html

How to create a large selection screen?

My app tracks how players at a pokertable play. I give my users the option to save up to 50 players stats in the form of a 'Player' object stored inside a JSON array within sharedPreferences. Allowing the user a way of selecting and loading one of these players has proven difficult.
An alertDialog seems to small, a new activity seems a bit overboard for what is essentially just a large menu and I fear passing my objects to it will prove difficult. A viewFlipper sounds interesting but I've no idea whether it's suitable.
How should I go about doing this? Filling the screen with dozens of buttons is really all I wish to do.
I agree that creating a new activity for this would be an overkill. One way to address this would be to have a GridView populated with your "buttons" - and the user would select one from the grid by clicking it. The question is where/how to display this grid.
ViewFlipper is a nice option in my opinion. The way you would go about it is place your existing layout inside the view flipper, then put the GridView into the ViewFlipper after your existing layout:
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/flipper">
<LinearLayout ...>
<!-- this is where your main layout goes -->
</LinearLayout>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/player_select"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ViewFlipper>
Then in your activity set up the Adapter for your grid view and the OnItemClickListener - to do something when one item is clicked. Finally somewhere (either in the main layout or in the options menu or elsewhere) provide something to display this grid view. In the handler code for that action you'd have something like
ViewFlipper flip = (ViewFlipper)flip.findViewById(R.id.flipper);
flip.showNext();
This would effectively hide your main layout and show the grid instead. And at the end of your OnItemClickListener you'll have
ViewFlipper flip = (ViewFlipper)flip.findViewById(R.id.flipper);
flip.showPrevious();
This would hide the grid view and navigate back to your main layout. Feel free to add any animation you like for the transition (e.g. 3G flip animation looks particularly nice).
Try an AutoCompleteTextView. Start typing one of the names, and a list of all possible matches pops up.
Screenshot from Google's tutorial:
I went with a ListView. It combines the filtering and tidyness of the autocomplete yet displays the names so users don't have to remember them like the ViewFlipper solution.
Thanks for both answers, they are both good solutions that helped me come to the solution I needed which combines the two.
The autocomplete had the advantage of searching so I could extend the number of players a user can save to my hearts content, it also looked neat but could not easily see the whole list which for poker players was necessary as the names you give when titling your notes are rarely your fellow players name but rather off the cuff nicknames based on their appearance/playing style. Hence using autocomplete the user will have to remember the nicknames they gave which over 50 players and potentially months apart would prove tricky.
Alek Gs solution would avoid that problem but would not be suited large numbers of names or long names for that matter as the buttons would be off fixed sizes.

Can multiple android views be stored in the same .xml file without belonging to the same parent?

I have an android project that has several small views which I need to instantiate at runtime. I haven't been able to figure out a way to store all of these related views in a single xml file and I now there are going to be many of these xml files. I was just wondering if there is any way to have them all in a single file, but not belonging to some parent ViewGroup.
The layout folder in android kinda sucks since there's no way to make subfolders, everything is just piled into the same place, ugh.
I hope someone can tell me of a better way of organizing these things.
If I understand you correctly you want several views meged onto one screen or merged into one xml file. You can include other xml's into one.
The articles showed you how to use the tag in XML layouts,
to reuse and share your layout code. This article explains the tag and how it complements the tag.
http://developer.android.com/resources/articles/layout-tricks-merge.html
Also, this video might help (about 19 minutes in). Shows you how to extract a current layout and be able to include it in others.
a couple things:
Yes, the layout folder is a pain. I use strict naming conventions to make it bearable, and in eclipse use the shortcut ctrl + shift + r to quickly find the layout I am looking for. Try naming your layouts after your activity: activity1_menu_overlay and activity1_main. With the above shortcut, just type out Activity1 and it will only show you the relevant layouts.
And if that doesn't work, you can try wrapping all your views in LinearLayouts and using view.setVisibility(View.Gone); or view.setVisibility(View.Visible); to show/hide the appropriate views.
Here is an example of that second one, because it's tough to explain.
one XML file:
<LinearLayout>
<LinearLayout ... android:visibility="visible">
<copy/paste of view 1>
</Linearlayout>
<Linearlayout ... android:visibility="gone">
<copy/paste of view 2>
</Linearlayout>
<Linearlayout ... android:visibility="gone">
<copy/paste of view 3>
</Linearlayout>
<Linearlayout ... android:visibility="gone">
<copy/paste of view etc.>
</Linearlayout>
</Linearlayout>
keep in mind this approach will require you to define a reference to each "child" LinearLayout view in your activity, so you can call setVisiblity appropriately.
This approach works well for animations, and I would only use it for 2 or 3 possible views in one xml file.

Categories