Best way to create a lot of new layouts - java

I'm currently making a local news app and the main layout has 10 image buttons (more to be added in the future) and I was wondering what would be the best way to get each of these to open a separate layout with an individual text view without making 10 seperate classes and maybe even without making 10 separate layouts. Right now my MainActivty class handles the first button from the layout main_activity which opens a new layout named issue.XML.
Thanks in advanced.

You can create a layout called "newsLayout" that has just one text view and sets it from intent and make the onClick listener of the buttons to start a new activity and passing the desired text (the news) to the intent
Here is some code to help:
newsLayout.java
TextView text = findViewById(R.id.text);
text.setText(getIntent().getCharArrayExtra("TEXT"));
mainLayout.java
public void openNews(View view)
{
Intent intent = new Intent(this, newsLayout.class);
intent.putExtra("TEXT", newsText);
startActivity(intent);
}
Where 'newsText' is the text you want to be shown
I may have wrote some lines wrong because im answering from mobile and i dont remember the exact words, if there's anything you dont understand tell me :)

Related

Button in one activity to do the same as button in another activity

I am new to Android. In my application, in ActivityOne I want to allow the user to take a picture using a button. The picture is then stored in the gallery and displayed in an ImageView in ActivityTwo.
My question is: How can I add a button to ActivityTwo that allows the user to retake the picture if they don't like it when displayed in the ImageView. Basically, the button will do the same as the one in ActivityTwo. Do I need to write the same code for the button?
I have tried to create a separate that implements View.OnClickListener, add the functionality in the onClick method, and in each activity create an instance of that class attached to the button:
onClick(View v) {
int id = v.getId();
if (id == R.id.myButton) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
myfile = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
v.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
However, this causes errors as the class is not an activity.
Any help is appreciated.
You can write the same logic for this second button, but as Naveen mentioned it would be easier to put the button on the first activity, leading to the camera. When the user takes a picture just add an imageview to the layout above the button using linearlayout on the first activity. Then the picture is displayed and the user has the option to retake it.
The best option is to let the user take the picture in the activity where it is needed. But if there is a need for you to use it in multiple places, you should extract the code triggered by the button in the first activity(The code that takes the picture) into a separate class. then you can call it from anywhere when needed.
#Subzero-273k Follow this.
In your activity make two different layouts. One, which shows your first button, remember to hide the other layout by setting the visibility.
Once user clicked the button1 which calls someOnClick(), hide first layout and enable another layout, which shows you the imageView and button2, onclick of button2 still calls your old function someOnClick().
How about this? As you dint mention what else you are doing in two different activities, I assume you just show activity one for single button.

Sequence of Activities - Interactive book

I'm trying to make an interactive book, where every page contains 2 images and 2 buttons.
One image is animated on user touch (that's working fine)
The buttons are "Back" and "Next", that's working fine too, but my problem is I have a sequence of 10 classes name one after another:
Class01
Class02
Class03...
(Every class has a different animation)
So in class01, next button is always calling the next activity
public void Next(View v) {
Intent next = new Intent(this, next_page);
startActivity(next);
finish();
and closing the current one, which is Force Closing the app quite regularly.
I'm new to android and I think my logic is pretty useless.
How can I implement this sequence?
you can make it using View Flipper to make it more interactive and add animations.
In your case, finish(); isn't necessary, remove it. And you will have:
Next button
public void Next(View v) {
Intent next = new Intent(this, next_page);
startActivity(next);
}
Back button
public void Back(View v) {
this.finish();
}
Don't forget, users can press "Back Button" on their device. This may interest you:
public void onBackPressed() {
// do something if the button back is pressed.
super.onBackPressed();
}
I think #MohammedSaleem and #mvnpavan are right when they say you should to use ViewFlipper, more adapted in your case. You will declare in your Manifest just one Activity for all your layouts. See this answer (https://stackoverflow.com/a/3545954/2668136) which says:
ViewFlipper can be used if you want to periodically change the views.
Say like an automated flipping book of some sort.
To make a ViewFlipper, you must to read this tutorial:
Creating Image Slideshow Using ViewFlipper
And to see a simple example in this SO answer:
How To Use ViewFlipper With Three Layouts?
Finally, the Google Reference Documentation:
Public Class ViewFlipper
Hope this help.

Android App Development, Dynamic Buttons & Fields

Let me preface my question by saying I have done a lot of research in creating dynamic buttons within an android app, and most are simply wrong or have a different view of dynamic than I do. If I missed something then just post the link and I'll check it out.
What I'm looking for is a way to create a button within my app based on information I gather from internet sources. For instance, when someone creates a post on a forums that I care to see, the app will find this, parse it for me, and return some info. Since I can't fit all this info on screen for each post that shows up, I want to create a button dynamically that previews this info. Such as name of user, date, and short description (as a preview that by clicking, will give all of the inforamtion in a separate activity). For the sake of this post, lets pretend I get this info from a text-entry location (not from an actual internt forum post).
First and foremost, how do I create the button dynamically? The other half of my question is less important to me. I would like to do this programmatically. Links to tutorials are great.
Secondly, and less important... Once I have created this button dynamically, how can I get custom views of the button based on a predictable format.
If anything is unclear, just ask and I'll try to clarify. Thanks for all your help!
In my opinion the right approach is to have an Adapter that will map the data to a certain view (a button in your case).
What adapter you choose will be depending on how you decide to fetch and store the data from internet.
When there are new posts you will be adding them to the data source (a database, a list, etc...) and you will call notifyDataSetChanged which will refresh the list, dynamically creating as many views as needed to display all the data.
I think this answers your question. The idea is to programatically create a button, and then add it to the current layout. Somethiug like this:
Button newButton = new Button(this);
newButton.setText("Click Me");
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
...
}
});
container.addView(newButton);
Where container is the layout that will hold the button (i.e. will become it's parent). You can also add layout settings to the button if desired.
Well, you can create a new Button, and set an onClickListener as so:
Button button = new Button(context);
button.setText("New Button");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
If you have anything else you need to set, such as an ID, you can call the method as you wish. You will need then add it to your layout as so:
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
layout.addView(button);
There is of course more you can do with it, but that should get you started, and anything else you need you should be able to find by perusing the docs.
If you need help with anything more specifically, then just comment and I will attempt to elaborate.

Android moving 'windows'

I'm new to android and I'm need a little help understanding how to move from one window to another. I know i use setContentView(R.layout.main) to load an xml layout file, but how do i swap to another layout file? I assume i would use an onClick method on a button and change setContentView(R.layout.other_layout), but would doing all this inside my main activity make my code cluttered? I could end up having 10000+ LOC easily. Can someone explain the correct way to do this please. Thanks
Intents allow us to call another activity from our present activity. For example our current activity is Act1 and we want to move to another activity, Act2. this can be done as:
Intent i = new Intent(Act1.this, Act2.class);
startactivity(i);
Refer http://developer.android.com/guide/topics/intents/intents-filters.html for more information on intents and activity.
Another option is to call setContentView() 2nd time to change the layout.
You use Intents to launch other Activities.
In your current Activity (i.e. window), you can do the following code to launch a new Activity
Intent i = new Intent(this, NewActivityName.class);
startActivity(i);
You should create Activities. Activity is equivalent to window/frame concept on desktop. Each activity should have a goal towards user interaction, ie. taking inputs and showing output. In your case create two activies, and both of them should have their own layour XML and a call to setContentView() within onCreate().
On button click use startActivity() to invoke a new one. Keep in mind that these activities are stacked top of one another.
A visible screen in Android is represented by an Activity. So instead of loading a different layout file into the same activity, you simple create a new activity with it's own layout and java file.
In order to call this second activity from the first one, or to communicate between activities in general android uses so called Intents.
Just look that chapter up in Android's Dev-Gui.

Re-load Android activity data

I am writing an Android app, part of which will be a survey involving multiple pages of checkbox question and answers. I have created an activity to display the question and options (from the DB) and what I want to do now is when i press the "Next" button it should just reload the current activity with the next question set from the database.
(the activity starts with survey.getNextQuestion() - so its just a case of refreshing the activity so it updates)
Im sure this is a simple thing to do -any ideas?
Thanks
Typically you would start a new instance of the activity for the new question. That way if the user hits back, it has the logical behavior of taking them back a page.
It is possible to update the same activity, but it is more complicated.
To open a new activity:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("nextQuestion", x);
startActivity(intent);
Then in your onCreate method, you can pull the next question from the database.
Im sure this is a simple thing to do -any ideas?
It is pretty straight forward, yes. Mayra's answer is perhaps the better one, however here is an approach that will achieve the functionality you specified.
You can use findViewByID(int) to identify the View objects in your layout that need to be updated, and assign the result to an attribute in your onCreate to allow you to access it later.
E.g.
aView = (View) findViewById(R.id.aview);
Your survey.getNextQuestion() can obviously be used to get the next question.
The question can then be placed into the UI by manipulating the Views you obtained in onCreate.
E.g.
aView.setText("your question/answer");
Depending on the number of answers you may need to programatically create/remove checkbox Views from your layout
E.g.
ViewGroup yourViewGroup = findViewById(R.id.yourviewgroup);
View yourView = new View();
//Configure yourView how you want
yourViewGroup.addView(yourView);
youViewGroup.removeView(yourView);
All of this functionality can be contained in a function that is called by the onCreate method and when the next button is pressed.
Don't forget to store the result of the previous question before refreshing ;)

Categories