Android: is using setContentView multiple times bad while changing layouts? - android

is using setContentView multiple times bad while changing layouts?
Some people say that it's bad and they never say why.
and is there some other thing to change layout using button?

Let's take a look at the Android Documents:
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.
So, setContentView will overwrite the layout, and replace it with a new one. Usually, you only want to do this once in onCreate. Theoretically, you could do it more, but it involves re-drawing the entire layout, and this could take some time. There are a few alternatives, depending on exactly what you want:
ViewAnimator: This is useful for showing a quick animation, if you want to change the view multiple times in quick succession.
Fragments- Instead of re-drawing the entire view, you can switch out fragments. Each fragment is a kind of mini activity, and overall this will contain the code much better.
Pass Intent Arguments- Pass information to an activity to help it set up. The first activity passes information to a common second activity, which knows how to set itself up based off of the information it receives from the first activity.
As for your specific application, here's what I would do:
Each band follows a specific layout. There is only 1, or maybe a few, possible layouts.
When the Band activity starts, the appropriate layout is chosen, and populated, knowing what's in there.
The Android SDK shows how to pass data from one activity to another. Just pass the data that the second activity needs from the first, using something like this:
Intent intent=new Intent(...);
intent.putExtra("Album","Some Album")
startActivity(intent);
The second activity will do this:
Intent intent=getIntent();
String albumName=intent.getExtraString("Album");
//Does something with albumName, maybe get a TextView and .setText()

Yes this is bad, because it inflates your activity with your layout, and if your layout has a lot of views, it may take time.
To avoid that you should use a ViewAnimator, where you put all your layouts and you switch by showNext() and showPrevious(), i.e:
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewAnimator"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout> </RelativeLayout>
<RelativeLayout> </RelativeLayout>
</ViewAnimator>
And in your code:
// Don't forget the setContentView
//
// Load the ViewAnimator and display the first layout
ViewAnimator va = (ViewAnimator) findViewById(R.id.ViewAnimator);
// Switch to the second layout
va.showNext();
// Add another layout at the third position
LinearLayout fooLayout = new LinearLayout(this);
va.addView(fooLayout, 3, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));

i cant show example because i'm waiting for answer to do it. okay i
have the app of lyrics(it will show lyrics of band) and albums are new
activities but i dont want many activities and thats why i want to
make songs only layouts and change views with button press
It sounds like you're going about this the wrong way. If you want to change the UI an Activity contains, then Fragments would be the better approach. There's a bit of a learning curve there, but it's good android design, and well documented.
Further, you seem to be confusing formatting and content. If you're displaying song lyrics, you don't need a new layout for each song. You just need to change the lyrics and keep them in the same activity. What you're doing is akin to creating a new web-browser for each web-page you intend to visit. Instead, find a way to store the lyrics and display them on a single activity (or in a fragment) to display those lyrics. The same would apply to each album: One activity would display the album cover in the corner (or as background), the title, release date, etc, as text, and then a list of songs below. The actual content of the TextViews can change, but the layout ought to be the same.

Related

Android Studio Single activity acting as many

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,

Are Spinner recommended for layout change

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.

Fragment vs Activity with LinearLayout in Android

I have a dynamic UI that I need to generate and I would like to know what the best approach would be to do this and why? The application that I need to make will either way only have a single activity in which to display various different views which are generated by code, so not in an XML file.
So basically I want to draw one set of views and have a user interact with them (textview, button, radio button, edittexts etc). Then save the data he generated, clear the canvas or screen and within the same activity generate the next set of views for the user to interact with.
I have done extensive research and I know that I can use either a Fragment or an Activity with a LinearLayout to achieve this, but I am not sure which would be better and why?
Thanks,
Wihan

How to structure an app with many similar views/activities?

Background:
I'm creating an Android app (my second) and I'm not really sure how to structure it.
The section of the app I'm having issues with is basically a sort of simple catalogue; a user clicks a button which opens a layout containing a title (TextView) and an image (ImageView).
Image for clarity:
What is the best way to structure this? Since I'm pretty new to Android development, my approach would have been to create an activity for the first view (no.1), create a seperate activity for each of the "Products"-pages, "Products 1", "Products 2" etc. and a third activity for the single item view.
I know how to reuse the "single item"-layout (no.3) but I'm not really sure what the best approach is for no.2.
Question:
Should I create six new activities for "Products 1", "Products 2",..,"Products 6" and corresponding layouts for each? Or should I try to keep the number of activities to a minimum?
Any help is highly appriciated.
Thanks!
Your app is like a tree-structure app, so three activities/views are enough for your application.
Products Category List(Contains Product1, Product2, etc.)
Product List(Contains Product1-1, Product1-2, etc.)
Product(Contains image for a product)
You should not create an extra activity for every product because it will consume too many system resources.
For coding, you can make a base class since these three activities are similar, and put common elements into the base class.
If you use listviews the first two screens can be just one activity, with different data passed in to display screen 2. And screen 3 will be common for all, another activity.
If you are planning on keeping the screens as you displayed above, with buttons, then you can just create one layout with button, use it as a row layout within a listview. The activity logic would be such that, depending on data, it would populate one or more rows. With this option too you can do screens 1 and 2 using one activity and one set of layouts.
You make one layout:-
1. layout one you can visible(visibility) first 6 textview and other will b gone(visibility).
2. Layout two you can visible(visibility) textview what you want and other will b gone(visibility).
3. Layout three you can visible(visibility) only image view and other will b gone(visibility).
note:- change textview value through setText.

How to load class at runtime in android app?

In my android project I've two layouts in that first layout having one button if i click that button I need to display the second layout in the same layout for this I created a view by using LayoutInflater and attached it to "Table Layout" which is present in first layout.
Everything should be fine but the corresponding class file for the second layout is not loading. Without loading I'm not able to call events like click and some other loader events so any one help me how can I load the corresponding class file when i click button in first layout?
It's difficult to understand what you mean by class not loading.
If you want some layout objects to be hidden at a particular time, look into the visibility parameter in xml or setVisibility(true/false) in code.
If you want to display a whole different screen, create a second activity and call it:
Intent i = new Intent(CallingActivity.this, ActivityToStart.class);
startActivity(i);
Your question is difficult to understand, but you appear to be trying to achieve an effect like embedding one activity inside another. You may want to look at the android developer docs about the "fragments" API, which is the currently-recommended way of achieving this kind of effect.

Categories

Resources