Firstly, I'm new to Android (so I apologize if this question is ignorant) but have some experience in Java. I have been reading a book on Android and remain slightly confused about the basic components (activities, intent, content providers, and broadcast receivers).
I have a few java classes that I'm wanting to convert over to Android but I'm not sure what type of components they should be.
If a class does simple conversions, should that be an intent?
Or, if a class draws, should it be an activity?
I'm just looking for someone who can explain the components a bit better than the references at android and perhaps give some good examples of each component.
I think you're confused about some of the terminology. An activity is what the user interacts with (displays the content, contains the listeners for buttons, etc.) So when you launch an app and see something on the screen, an Activity is what draws all the buttons/components on screen and contains the code for user interaction. An intent is kind of a way to tell activity to launch something else. For example, if you were on your main activity, and wanted to change the Activity when the user clicked something, you would create and start an intent specifying that:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
Read this basic tutorial. It should be somewhat clearer than the official docs.
http://androiddevelopertips.com/activity/understanding-activities-in-android.html
Related
I would like to show a notification and play a sound when the user taps onto that notification. It works somehow when I use an activity to play the sound, as I have written in my own answer to this question: How can I create a notification that plays an audio file when being tapped? (in this question and answer there is also the source code showing how I create the notification and how my PlaySoundActivity looks like.
Yet, I have realized, that while the sound is playing, the appearance of my main application changes and it will not be restored without closing the application.
I have created my application from the "Tabbed Activity" project template.
This is how it looks after being started:
And this is how it looks when I have tapped onto the sound notification (the sections are gone):
Can anyone explain why this happens? Is it a wrong approach to play sound using an activity? But it does not work here when I use a service, I hear nothing! How to solve that?
According to the Android developer reference, "almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)".
I had not done this. Therefore a new window was displayed, but there was no content.
The better solution for playing a sound is to use a PlaySoundService (service!!!) instead of an activity. It contains almost the same code as an activity would do, but the pending intent is created with PendingIntent.getService(...) instead of PendingIntent.getActivity(...). Using the wrong method does not result in an obvious error message, but the service won't work as expected.
Although I do know how to open another activity quite easily, using intent like the below:
Intent intent = new Intent(first.this, second.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("selectedApp", ApplicationTitle.getText());
startActivity(intent);
When you use the android application "PlayStore News", when you select a news item rather than just being redirected to another activity. An animation occurs, where in the change between several small cards to one large occurs.
How can I accomplish this?
http://www.mysamplecode.com/2013/02/android-animation-switching-activity.html
There is a method
overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
You might want to check out https://developer.android.com/training/material/animations.html#Transitions
This explains how to define custom transitions between activities. In particular, you may be interested in shared element transitions. I'd be more specific, but its a fairly broad question
Check this sample code for animation while switching activities in android.
http://android-er.blogspot.in/2013/04/custom-animation-while-switching.html
http://www.mysamplecode.com/2013/02/android-animation-switching-activity.html
I am java programmer, but i never put my hand on android before. I just want to clear some basic stuff that the different between programming a window app and a android app.
I know how to programm a window app that has pop up windows inside the app.
for example:
MyWindowClass m= new MyWindowClass(new java.awt.Frame(), true);
m.setVisible(true);
But i do not know how to open a new view or layer at Android.
Can someone give me some hinds.
If you just want to display a pop-up window, just use a Dialog.
If you are looking to add an entirely new layer to your application, consider starting a new Activity (you can use either Context.startActivity or Context.startActivityForResult if you are interested in returning a value (placed in a Bundle) from that Activity.
You can also have another layer by starting a new Activity with a transparent background, but there are some limitations when it comes to that such as user input not being passed to the Activity behind it.
EDIT:
If you want to have "multiple windows" and not have them lose state (unless they are closed by the system), you can also use startActivity with an Intent on which you've added the FLAG_ACTIVITY_REORDER_TO_FRONT flag.
Just starting out developing some android apps. Coming from a web development background I'm wondering if the idea behind changing whats displayed on screen is similar to linking html files.
Say I had a button that once clicked would then display a completely new page, button gone and completely new content in its place. I had thought at first that this was done just by having conditional statements in the main activity class. But I don't see how this would work with the xml layout file.
So I have come to the conclusion that you have to define multiple xml files and switch between them using logic in the main class.
If this is correct whats the best way to go about this, and if not could some suggest how this is achieved normally?
Thanks.
I think it wise to follow the following tutorial: http://developer.android.com/training/basics/firstapp/index.html
Have you tried visiting Android developers' website?.The solution to your question can be obtained taking the Android training module in that website. You have said you want to go to a new page, you can use Activities here.
Let me explain you this in simple terms.
In Android for every page(Activity) you need to make a separate xml file. for example main_activity.xml.
And for each page(Activity) there is a java class. For ex MainActivity.java. This class may contain event handling and business logic part.
Now let's go to your question about switching between multiple pages.
Suppose you have 2 activities: MainActivity and SecondActivity.
Now in MainActivity you have a button then you set its onClick attribute to its event handling method. This can be done in xml file.
android:onClick="goToSecond"
Now in MainActivity.java you need to create a method which looks like this.
public void goToSecond(View v)
{
Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
}
This is a code snippet for switching to second activity.
And I also agree with other answers that you should check out developers.android.com
Hope it helps.
There is no need to switch between the XML files for portrait and landscape mode. Android does that for you. If you are writing an app that uses both orientation, then you have to write separate layout files and keep them in layout(for portrait), layout-land(for landscape) folders. This is not at all necessary if your design looks same in both orientation.
I have been looking all over the internet and i found some good guides for programing in android but i still dont fully understand what exactly is going on with this thing
i thought this would be like programing a java program but its very different.. there are lots of xml files and there is this thing called "activity" and an "intent" of which i dont understand how they work.. same with the way of displaying things..there is this xml file that designs the way the app looks but when you i used this code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(FirsttimeActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
the text box that was there before disappeared and a normal text appeared instead.. what just happand here? how do i replace the text with something else after that? how do i go back to the normal view?
so my qoustions are as follows:
what does an "activity" mean? whats the parallel in a normal java project?
whats an intent and how does it work?
how do i control the display? how come it changed when i entered that code?
thanx
An Activity is an essential part of Android development. Its basically a class which you can use to represent any number of UIs. The onCreate method is not a constructor, but is called as soon as the Activity is started, so initializing your information in there is a good idea. The program knows which Activity it is starting with within your android manifest file with the line of code
<category android:name="android.intent.category.LAUNCHER" />
There is no real parallel to a 'normal' java project. An intent is used to pass information from Activity to Activity. The best way to learn about Intents is to read about them here :
http://developer.android.com/reference/android/content/Intent.html
The actual visual part of what you see, is controlled in the XML files which are located in the /res/layout/ folder within your project.
This is all very basic information that you should know before coming here. A more detailed guide and tutorial can be found the Android developer's site. Go through the tutorials and guides and learn as much as you can before looking anywhere else or asking on Stack Exchange. I've provided some basic answers here, but you can find better details on the site I linked earlier.
In basest terms, an Activity is a specific screen and is the building block of the typical Android application. Each screen that you see in an Android application such as the settings screen or main application screen is a single Activity.
XML files are used to design the layouts of each Activity that requires a graphical interface. When you used
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
You created an entirely new TextView widget. To link to a widget that has been described in the XML file, you call the findViewById(int id) method.
An Intent is how an Android application passes information between Activities and launches new Activities.
If you visit http://developer.android.com you can find all the info you need.
Learn programming Android requires some studying. The official developer site is the best point to start.