I was thinking about using spinners (as it is like a dropdown feature) to change the type of text on my activity. For example let us say we choose the car from the spinner, it would change/replace the layout completely and show the car attributes e.g. car make. Another example is if the user click "Mobile" the spinners will change the layout on the activity.
Is this a good approach to take? Or is it best to create a activity for each product? The only reason I asked this as one of the answers here recommended its not a good idea.
You are the only one that can decide whether to do that or not which mainly depends on the amount of changes that you will make to your layout events handling, and about spinner it has nothing to do with this manner, you will have to do an action after choosing an item from the spinner either creating a new intent() and launching an activity or changing the layout.
So, if you have minor changes in your "layout events handling", you have two options:
if the layout changes mainly in sources, backgrounds, and
visibility of the layout-elements but the structure remains as it is, your best choice is to just make your changes to the views(elements)
itself without changing the whole layout.
if the layout structure has to be changed, you are advised to change
the whole layout by using the method:
setContentView(R.layout.new_layout);
and after that you have to
declare the layout elements again.
BUT, if you have a complete new layout for every element in the spinner, you have two options:
create an activity for each element of the spinner, and include the spinner in each activity of them.
create a fragment activity, and include the spinner in the main
layout of the activity, and create a fragment for each element in
the spinner, and with each element change, navigate to it's
corresponding fragment.
Related
I have looked everywhere but don't seem to be able to find what I am looking for. I have an activity with multiple buttons, each button opens a new activity with an identical recyclerview layout, but different data. I am wondering if it is possible to use one activity and layout instead of multiple? this way instead of having 10+ activities (one for each button) I only have to manage one when a button is clicked and simply pass the necessary list data to it.
I believe you could set the intents for each button to call the same activity but with an integer such as 1-10, in the activity it takes the value and decides which list should be presented? If anyone thinks of how this could be done or a simpler way I would greatly appreciate it!
Yes, there are various ways to do that.
You could use multiple fragments on the same activity. Then add/remove fragments on each button click.
You can have multiple layouts within your activity. Say you have two buttons, and you have three layouts layout1, layout2, layout3, sequentially one after the another. So if initially, layout1 is visible and the rest are gone using layout.setVisiblity(View.GONE), if you click button1, ypu can do layout1.setVisiblity(View.GONE); layout3.setVisiblity(View.GONE); layout2.setVisiblity(View.VISIBLE) and vice-versa for pressing button2.
Are all the activities opened by the buttons similar? If so, you could only take care of the changes in the elements of the layout & specify conditions.
For instance, if you click a button, instead of changing the whole thing, you only go into the buttons & change their texts with btn.setText("..."). You could define different conditional statements inside the onClickListener of that button.
It could be something like:
if(btn.getText().equals("a certain text that you set to the button")){
doSomething();
else if(btn.getText().equals("another option")){
doSomethingElse();
Following this logic, you could continually update the elements in your layout & your code will decide what to do depending on what's stored in these elements.
The second option that comes to my mind would be creating different xml layout files & simply changing the layout of your MainActivity to the appropriate one depending on what stage of your process you are at.
I hope this helps,
my problem goes like this-
this is a commercial application in which users can purchase different products they see within app..
there is one view cart button available on the main screen
there are 10 different products available in an activity and there are 3 such activities( each of them containing various different products)
these products are arranged in a linear fashion (linear layout).
every product is having an add to cart image button.
what i want is, when the user presses that button, 3 things should happen..
a new activity will be created containing that product information (when the user presses another "add to cart" button, this new product will come below the previous product and so on).
there is one cart button in the menu bar, that cart should show a number equal to the nos of product in the cart.
its simple, a small flash notification that the product has been added to the cart.
any help will be deeply appreciated.
You would see some benefit by using Fragments instead. Use a main Activity as a "switchboard" and multiple Fragment instances to show the per item details.
You don't need to dynamically create a new Activity. Taking into consideration that you call yourself a newbie in android development, I can only tell you what to study to accomplish your plan.
First, learn to make an Activity that will contain a ListView. Make an XML layout for your ListView items, containing TextViews, Buttons, whatever you want. Then, make an adapter that will fill items with corresponding values (texts, button names, images, etc).
And second, make them all clickable (onItemClickListener or an onClickListener for every button) and make your Activity get inflated with a Fragment containing TextViews, ImageViews, etc... whatever you need to represent information about the item selected.
The best practical way is to make an empty Activity with an empty Fragment. The last one will be either inflated (populated) or even replaced with whatever things you need.
Sorry, but teaching you how to make all these things technically is beyond the scope of any sensible answer that someone can post here. It's all the matter of study and practice. But I hope the vector of study I gave you is transparent and affordable.
Let's say i have the Android XML file home_page.xml.
on this home_page.xml i have some variations that i want to show at different activities, and i'd like to reuse the same main layout home_page.xml.
For example, imagine variations on the page such as:
there's 2 more buttons if the user is in state A
there's 1 more editText field if the user is in state B (same activity as state A)
there's a different arrangement of layout on the Z-axis in a frame layout if the user is in state C (same activity as state A)
i know it's possible to programmatically say hide views and set views as visible. but is there a better way to do this via xml or something?
Android recommends using 2 Tags for re-using the layouts across different screens.
Include
When to Use ?
If you already know the layout that you want to re-use, create a new XML file and define the layout. Use tag to re-use it.
Merge
When to Use ?
To eliminate redundant view groups in your view hierarchy when including one layout within another, we can use tag.
Refer to this link - http://developer.android.com/training/improving-layouts/reusing-layouts.html for code sample and more details.
You can hide views but using the Visibility flag.
View v = findViewById(R.Id.my_view);
v.setVisiblity(View.GONE); //etc.
I've tried stuff like this before. I had mixed results. This is fine if you are doing things, like asking the user for a name, then showing an address input or something. But if you find yourself with like 3 or 4 conditions for one editText and then different ones for a button in the same class you might want to just use different layouts. A lot easier to manage.
If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...
The main menu of my app is a list of items that has a very specific look. It has a custom divider and every list element has a custom colour and height. To achieve this is have built a custom ArrayAdapter, but I wonder whether this is really necessary. The buttons in the main menu are always the same, so I wonder what's the better design pattern here. Pure XML or overriding the ArrayAdapter?
You cannot create a "Custom ListView for main menu purely in XML not using a programatically defined custom adapter". ListView requires a ListAdapter, whether you like it or not.
That being said, I would not put "buttons" in a ListView in the first place. Ideally, you would not even have a "main menu of [your] app", but rather would take the user someplace useful when they launch it. If you are sure that you need to have an activity that is a "main menu", use the dashboard pattern: Android Dashboard Pattern