i want to dispaly text views n other stuff on 1 screen and when ny1 of those is selected i want to go another screen which already has textviews and radiobuttons on it???
how do i do that???
Read more about using Intents and startActivity.
You pass data from one activity with the other using Extras. putString
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,
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.
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.
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.
I just have a question regarding changing an Activities layout.
Basicly my problem is:
I have a list view populated with a series of Strings.
I have an OnItemClick event assigned to the ListView,
When the user clicks an item in the list view, I want the current layout to disapear and an image to take its place.
From reading other posts, I understand that the recommended way is to set a separate activity for each item in the ListView, however, seeming as all the activity is doing is displaying an image I think it would be a waste of effort to set up a separate activity for each item in the ListView...
Could anyone give me some help on this?
thanksin advance.
Shaw
You really should create a new activity (ar a new fragment if you have space, but this is another question) which displays the image, for at least the following reasons
it is very, very simple to code and mantain such a solution. What if tomorrow you want to add 2 buttons and some text and maybe a menu for that image? you have your brand new activity to edit and upgrade without risking to damage your list activity.
it is more user friendly. If the user presses back when the image is shown, with 2 activities he will be back to the list, with your solution he will go back BEFORE the list, and this is not what he'd espect
remember this piece of advice: 1 activity = 1 simple task or interaction with user.
list + image display = 2 activities (or fragments)
PS: you do not need to define a different activity for each list item, just pass with the intent to the "ImageActivity" and specify there which image to show!
EDIT 2: to pass to the next activity your current selection, just use the putExtra(String key, T value) (T may vary, check documentation) method of Intent class.
example: intent.putExtra("imgCodeSelected", index) where index is fetched by the onItemClick event. You can put as many extras as you want as long as they have different keys.