Android App Development : How do change content on screen depending on event - java

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.

Related

how to reuse single activity infinite times?

My app has an album style feature that users can pick/take photos from device and load it in every page in this album.
Now the number of this album pages is up to user and should be unlimited. On the Other hand the codes that is being used in activities as you could imagine are exactly same. I have wrote code once and I just want to reuse this activity for each page of album. how should I achieve this?
Since there are thousands of apps that are provide this kind of functions I know there is a good way but i'm unable to find that. Just a trick or a link to a tutorial or even a small explanation is good enough.
Thanks in Advance.
You can create a next() method, and change every layout view in the page with this function, so that you are in same activity but only views are being changed.
One solution is to add the data to be displayed to the "extras" of the Intent which you use to start the activity.
Alternatively, you might want to look at using a Fragment instead of an Activity. You might also want to look at using ViewPager to be able to swipe through the fragments which display the album data.

How to avoid titlebar of activity loading before actual activity loads and make it load with the activity load using java code?

.
I am doing reverse-engineering of applications.While reverse-engineering an app i needed to change the titles of each activities in an app that i got .I have a problem regarding activity titlebar.
Actually the developers of this app have set many of the titles from androidManifest.xml file using
<activity android:name="com.example.dmo.MainActivity" android:label="first"></Activity>
I changed the title of each activity by putting setTitle(" say abc"); in onCreate() of each activity and title is successfully changed, But problem is that while loading activity it shows the titlebar with the title from androidManifest.xml file i.e. android:label="first".
This title appears for some miliseconds (time increases if activity takes more tome to load), and then when my activity loads completely, title changes to the text i passed in setTitle() in onCreate().
This is somewhat similar to my problem.
I am not able to find solution since i have a strict restrictions that i can not make changes in any of the .xml fiels in an applicaion. I only allowed to change the java code and add some new java code if necessary.
I searched a lot but all solutions i found suggested changes in xml files; and some other solutions those suggested using code did not worked.
So is there any solution that does not require manual hard code xml changes directly. Changing xml using code is acceptable but not hard code changes like editing their original manifest file to :`<activity android:name="com.example.dmo.MainActivity" android:label="say abc"></Activity>`
Any AspectJ solution is also acceptable(i.e. some pointcut to some method that can do the work for me), if someone knows for the same.
I also want to know that, which class or api is responsible to load title bar before activity loads? ,so that if possible i can make changes in that .
Any help would be very appreciable . .Thank u in advance . .
Then the answer is you can't. This is the Application theme that is shown and it can be changed only in manifest. The screen you see is the Activity screen in state when the Application is not yet loaded and before any code in Activity onCrete() is executed.
Did you try apktool? You should be able to get editable manifest using it.

Android Program Modification

I'm currently working on my first android project, which is to modify an existing sample program ("Tic Tac Toe" game). I'm going through the tutorials on the official website, but looking at the sample code I don't think I'll be able to figure everything out on my own in the time I have.
The modifications include being able to select a custom background, setting up a scoring system, and implementing a timed "blitz" mode. My basic questions are:
In which subfolder would the code that sets the background color/image for the game be?
Is there a way to create an Intent function that opens up a file search window to allow a user to select this custom background image?
I'd like to start here, I'm sure I'll have more questions as I go along. As always, any help is appreciated. (By the way, the code from the game is from the standard sample problems that come installed with the Android SDK for Eclipse).
Update 1:
So far I found this in a class called GameView:
mDrawableBg = getResources().getDrawable(R.drawable.lib_bg);
setBackgroundDrawable(mDrawableBg);
mDrawableBg is a Drawable object, I'm not sure what this part is refrencing:
R.drawable.lib_bg
What would be the proper way to modify the background in this piece of the code?
Update 2:
Here's where I'm at:
I have the getDrawable function taking another function as an argument:
mDrawableBg = getResources().getDrawable(getImage());
getImage() is suppose to return a integer referencing the selected image, here is the code (so far) for that function:
public int getImage(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 10);
}
This is suppose to open the gallery and let the user select an image, I'm not sure how to return a reference ID to that selected image though. Also, the startActivityForResult function is not working properly, I don't think I'm using the Activity class properly.
The background is probably defined as either a drawable (in /res/drawable-*) or a color value (in /res/values/colors.xml or something like that). It will be referenced in one of the layout files in /res/layout. The layout file will be referenced by one of the activity classes in the Java source folders.
You can declare in code an array of drawable resource IDs and use that to dynamically generate a dialog and/or activity. The HorizontalScrollView widget might be useful for this. If you start a selection activity with an intent, use startActivityForResult instead of startActivity. You can then set the background of your top view using setBackgroundResource().

programming an android app - where to start

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.

Changing views/activity in Android

I am a newbie in Android and currently I am designing an application where I have faced a big problem with changing views. I have 3 classes, each one for a different screen. I use a button to change pages but it does not seem to work. Every time I move to the next screen, the variables methods etc of the 2nd screen are located in a different class. Can you please show me the simplest way to do this? Thank you.
Place each screen in different Activity. Start respective activities according to button presses. To pass data between activities use Intent.
Activity Reference
Intent Reference
Simple Example for Activities and Intents

Categories

Resources