Android Program Modification - java

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().

Related

How to get image from PhotoEditor SDK

I am trying to integrate my project with PESDK. I need to get image that is currently in the editor. I found that PESDK has R.id.editorImageView. This is object of EditorPreview class which isn't an extension of ImageView. So I didn't understand how to extract the image. Because of word Preview I suppose that PESDK do not apply immediately all changes to the image and in the end after pressing default AcceptButton of PESDK it runs final calculations and saves this image.
Is there any way that I can emulate this action? I just need to get Bitmap in the memory without saving it.
If there is no such way then I think the one possible solution is to call onClick of default AcceptButton and notify my code that it was pressed myButton, not AcceptButton. Then read saved image, process it and then delete file. I'm quite new in Android and if you know how to do this notification (that it was myButton pressed) then please share your knowledge.
Thank you for any help or advice!

Launch the same activity for different buttons with different resource

I am new to android and was starting out by making a list of button.i want when each button click launch a same activity for them with diffrent resource such image and raw file
what is big deal here?
call same intent twice :)
refer how to start new activity here
You can the same activity/intent multiple times, just if you want the code in said activity to do something else you might want give it some extra information by using intent.putInt(<Key>, <Value>); and others

Android window-layering in java

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.

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

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.

Change Linear layout background image on activity startup

I change Linear Layout background image via this code:
mainlayout.setBackgroundResource(R.drawable.back);
But i want do this on every time that activity starts, In first start and switching between activities. I put this code inside an IF statement in onCreate() but background not change! Of course when i set this code to a button works fine! How and where i have to put my code?
my complete code is:
//check theme id
if(myDbHelper.gettheme()==1)
{
mainlayout.setBackgroundResource(R.drawable.back);
}else if(myDbHelper.gettheme()==2)
{
mainlayout.setBackgroundResource(R.drawable.blueback);
}
You need to put your code into the onResume() method.
Here are details that will explain why
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You may want to refer to the Android activity lifecycle, but I'd recommend the onResume() method.
I'm assuming based off your code snippet you have the image you want as a background as a drawable resource already in project. If so you could just go into the XML and add into the linerayout:
android:background="#drawable/back"
This should just set the background within the XML layout avoiding having to have code set it within one of the activity life-cycle functions.
Though in the case you wanted different themes as I just saw in the original post (seemed to be edited since I was typing this up), you could try storing the constant int of the R.drawable that the user wants as the background or theme, and have your DB Helper's getTheme() return that reference to set the background without the if statements.
Such saying the user changes their preference to R.drawable.black or .blueback store the int within the DB so it get return by gettheme rather than a 1 or 2 enumeration. Not sure if this would be a best practice though.
EDIT: Are you sure theme is either 1 or 2?
If you want to do it everytime, why don't you just define it as mainLayout's background in your layout.xml? Otherwise, use onResume() rather than onCreate().

Categories

Resources