how to setup multiple background on one activity randomly - java

i need to know how to setup background randomly on one activity,
'android:background="#drawable/backG"' only show one image
the background come randomly with the launch of the application, and it keep on showing until the user quit the app, and when he reopen it, it will show a new background
note : i have only one activity in my project
thanks in advance

I think, you should use Java code to make this.
For example, you have RelativeLayout is the main layout in your activity
You should use
RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout);
Resources res = getResources(); //resource handle
Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder
rLayout.setBackground(drawable);
For random background: you create an array that contains all you images background
and use the code below for random position
Random randomGenerator = new Random();
int randomPositionInt = randomGenerator.nextInt(10); // it will generate position in 0 - 10
Hope this help

Related

How to add hints in a game?

I am trying to create a word quiz game. I have a list of random words which are going to appear, and the player must arrange them correctly. Now I want to add hints. For example, if word is "School" appears, I want the hint to be "photo of school"
These are the words in the code's layout
<string-array name="words">
<item>SCHOOL</item>
<item>ABETS.BEATS.BEAST</item>
<item>ANGEL.ANGLE.GLEAN</item>
<item>COATS.COAST.TACOS</item>
</string-array>
In android studios go to res/drawable and add the photos you want to use there.
Here's how to do that : How to add an image to the "drawable" folder in Android Studio?
Then you initialize the photos with :
private ImageView schoolPhoto;
schoolPhoto = (ImageView) findViewById(R.id.imageViewId);
schoolPhoto.setImageResource(R.drawable.imageFileId);
Then you can create a method with toast which is often used for hints:
public static displayHint( ImageView imageHint) {
Toast toast = new Toast(this);
toast.setView(imageHint);
toast.show();}
And run that method as displayHint(schoolPhoto) when you show the string.

Activity as Dialog Just On Larger Screen

I'm developing an Android app that needs to support 2.3+. I'm using HoloEverywhere as a layout library. My app runs perfectly on smartphones and tablets, but for now I'm using the same layout for all devices, so, in tablets there is a lot of blank space in a lot of layouts. So, I thinking if is possible to show this layout as a popup (AlertDialog) just on large screens.
I searched the internet for a response but every response I look isn't applying to my case. I don't want to change my layouts files (and, of course, I don't want to create new layout files).
Can anyone give a direction to solve my problem, or the unique way is to create new layouts for large screens?
You can use your own layout file as the content view of a dialog. With a custom view you can create other/bigger dialogs for your tablets. It is also possible to create a dialog from an activity. See http://developer.android.com/guide/topics/ui/dialogs.html#ActivityAsDialog or http://developer.android.com/guide/topics/ui/dialogs.html#FullscreenDialog
You can change the theme of activity to dialog in Manifest file. See:
<activity
android:name="Activity"
android:label="Home"
android:theme="#android:style/Theme.Holo.Dialog">
</activity>
You can use this code to change the height and width of my activity in onCreate()..It provides better flexibility than specifying dialog theme
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
requestWindowFeature(Window.FEATURE_ACTION_BAR);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
android.view.WindowManager.LayoutParams params =
getWindow().getAttributes();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
params.height = LayoutParams.WRAP_CONTENT;
params.width = (int) (width / 1.2); // fixed width
params.alpha = 1.0f;
params.dimAmount = 0.5f;
getWindow().setAttributes((android.view.WindowManager.LayoutParams)
params);

Android is not matching closest size drawable when I use getResources().getIdentifier()

Here is my code:
int id;
String[] imageList = {"pic_1_480dpi", "pic_2_480dpi",
"pic_3_480dpi", "pic_4_480dpi",
"pic_5_480dpi", "pic_6_480dpi"};
// Get reference to image on screen
ImageView image = (ImageView) findViewById(R.id.image_View_2);
// Increment image list counter
counter++;
if (counter > 5) counter = 0;
id = getResources().getIdentifier(imageList[counter], "drawable", getPackageName());
image.setImageResource(id);
I only get the images in drawable-hdpi folder even though it is running on a 10.1 tablet. I have same-named images in the drawable-xhdpi and drawable-xxhdpi folders but they do not get selected for display. Is there a better (i.e. more correct) way to look for drawable IDs when selecting from a string array.
The screen size doesn't indicate the dpi. Which device is it? The dpi is a combination of the screen physical size and resolution.
I guess you're already familiar with the following link, if not, I suggest reading:
Android: Supporting Multiple Screens

Change Background Image of RelativeLayout from within Java Class (Android App)

So I am working on an Android application using Android 4.0 Library.
One of the activities of this application is made up of a RelativeLayout that has an image background and a toggle button.
The background image of the layout must change when the user toggles the button.
So it must be changed from inside the activity.java class:
if (toggleButton.isChecked()){
// Change the background of the activity to image 2 (for example)
}
else{ // when toggle button is off
// Change it back to image 1
}
Please help me with this. Thank you :)
You use the method, setBackground in the View class:
if (toggleButton.isChecked()){
// Change the background of the activity to image 2 (for example)
View myView = this.findViewById(yourViewId);
myView.setBackgroundResource(yourImage);
}
else{ // when toggle button is off
// Change it back to image 1
// Change the background of the activity to image 2 (for example)
View myView = this.findViewById(yourViewId);
myView.setBackgroundResource(yourOtherImage);
}

Having application running above other app

I want to make an activity that can be opened above ANY app.
Normally, even when the activity is set as dialog, when you switch to my app, you see my app, and in the background you see the launcher:
BUT, I want the app will go above any app like this: (made in photoshop):
I did see this question Creating a system overlay window (always on top), but in ICS there is no functionallity to the layout.
Furthermore, I want to give a dialog box from my app without minimizing the other app...
there are plenty of apps that show a floating view on top of everything like : airbrowser , LilyPad , Stick it , AirTerm , Smart Taskbar , aircalc ...
anyway , in order to achieve this feature , you must have a special permission called "android.permission.SYSTEM_ALERT_WINDOW" , and use something like that:
final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
final View view=findViewById(R.id.my_floating_view);
final ViewGroup parent=(ViewGroup)view.getParent();
if(parent!=null)
parent.removeView(view);
param.format=PixelFormat.RGBA_8888;
param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
param.gravity=Gravity.TOP|Gravity.LEFT;
param.width=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().width;
param.height=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().height;
final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wmgr.addView(view,param);
// TODO handle overlapping title bar and/or action bar
// TODO you must add logic to remove the view
// TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW
// TODO if you wish to let the view stay when leaving the app, make sure you have a foreground service running.
I'm one of the developers of the Tooleap SDK, and we also dealt with this issue.
Basically, you don't need to use the SYSTEM_ALERT_WINDOW to display an activity on top of another one. You can just display a regular "shrinked" Activity with a transparent background.
To make a "shrinked Activity, change the activity window layout params of height and width:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = ...;
params.y = ...;
params.width = ...;
params.height = ...;
this.getWindow().setAttributes(params);
To make a transparent background add to your activity definition in the manifest file:
android:theme="#android:style/Theme.Translucent"
That way, you can create the illusion of a floating activity:
Note that only the foreground activity will be resumed, while the background one is paused. But for most apps this shouldn't be an issue.
Now all that remains is when to launch the floating activity.
Here is an example of a "floating" calculator app using a regular activity. Note that the activity below the calculator belongs to another app.

Categories