Android: uses setContentView several times poorly when changing layouts?

uses setContentView several times badly when changing layouts?

Some people say that is bad and they never say why.

and is there something else to change the layout with a button?

+8
android android-layout android-activity setcontentview
source share
3 answers

Take a look at the Android Documentation :

Set the activity contents to an explicit view. This view is placed directly in the hierarchy of activities.

So, setContentView overwrite the layout and replace it with a new one. Usually you want to do this only once in onCreate. Theoretically, you could do this more, but it requires re-drawing the entire layout, and this may take some time. There are several alternatives, depending on what you want:

  • ViewAnimator : This is useful for showing quick animations if you want to quickly change views several times in a row.
  • Fragments - Instead of re-drawing the entire view, you can turn off fragments. Each fragment is a kind of mini-activity, and in general it will contain code much better.
  • Pass Intent Arguments - Transfer information to an action that will help him set up. The first action transfers information to a common second activity, which knows how to configure itself based on the information that it receives from the first action.

As for your specific application, here is what I will do:

  • Each group follows a specific pattern. There are only 1, or possibly several possible layouts.
  • When the action of the group begins, the corresponding layout is selected and filled, knowing what is there.

The Android SDK shows how to transfer data from one activity to another. Just pass the data that is required for the second action from the first, using something like this:

 Intent intent=new Intent(...); intent.putExtra("Album","Some Album") startActivity(intent); 

The second action will do the following:

 Intent intent=getIntent(); String albumName=intent.getExtraString("Album"); //Does something with albumName, maybe get a TextView and .setText() 
+8
source share

I cannot set an example because I am waiting for an answer to do this. Well, I have an application for text (this will show the group’s lyrics), and albums are new but I don’t want a lot of events, and that’s why I want to make compositions only layouts and change the views using the button

You seem to be mistaken. If you want to change the user interface containing the Activity , then Fragment would be a better approach. There's a bit of a learning curve there, but it's a good design for Android, and well documented .

Also, you seem to be confusing formatting and content. If you show the 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 do is akin to creating a new web browser for every web page you are about to visit. Instead, find a way to save the text and display them in a single exercise (or fragment) to display these texts. The same applies to each album: one action displays the album cover in the corner (or as a background), title, release date, etc. As text and then a list of songs below. The actual contents of the TextView may change, but the layout should be the same.

0
source share

Yes, this is bad because it inflates your work with your layout, and if there are a lot of views in your layout, it can take some time.

To avoid this, you should use ViewAnimator where you put all your layouts and you switch 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)); 
0
source share

All Articles