Animated backround for my android activity using Eclipse and ADT - android

I want to create a background that covers the whole screen of my Activity.
Just like:
android:background="#drawable/background"
But I want this background to be animated (60 frames), looping and target as many device resolutions as possible.
I tried to do a drawble .jpg sequence, but the problem is that I use 1920x1080, and the logcat while trying to test it on the emulator, the application crashes, and LogCat gives me en error saying, not enough memory
Then I tried to import a video of my sequence and set it to play on the background. But I don't know where to put the video in my res folder and call it from there with SetVideoPath(), in order to play it.
What is the best approach for an animated background, that covers multiple devices with different resolutions?
#Arun C Thomas
I still get an error in logcat,
OutOfMemoryError
my code is this:
package combiolab.biolab;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.view.WindowManager;
public class MainActivity extends Activity {
private Renderer graphicsRenderer;
//public static String gl;
AnimationDrawable anim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
GLSurfaceView myGLSurfaceView = (GLSurfaceView) findViewById(R.id.gl);
myGLSurfaceView.setEGLConfigChooser(true);
myGLSurfaceView.setRenderer(graphicsRenderer);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.opengl.GLSurfaceView
android:id="#+id/gl"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</android.opengl.GLSurfaceView>
</RelativeLayout>

You need to create an OpenGL view,
then add it like
<android.opengl.GLSurfaceView
android:id="#+id/gl"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</android.opengl.GLSurfaceView>
In onCreate() implement Renderer like
GLSurfaceView myGLSurfaceView = (GLSurfaceView) findViewById(R.id.gl);
myGLSurfaceView.setEGLConfigChooser(true);
myGLSurfaceView.setRenderer(graphicsRenderer);

Related

Trouble with ID in AIDE

I'm writing a simple Android App using AIDE (Android IDE). I gave one of my layout elements an ID, but when I try to access the element using findViewById(), I get an error tht says: "Unknown member 'id' of 'com.mycompany.mailscomunes.R'. I haven't seen this error outside of AIDE.
This is the Java code:
package com.mycompany.mailscomunes;
import android.app.*;
import android.os.*;
import android.content.Intent;
import android.provider.ContactsContract;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.one);
}
}
And this is the relevant XML:
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/one"/>
Remove these lines:
import android.app.*;
import android.os.*;
You're literally importing the entire Android framework, which includes layout files, which has ID's in them.
And you're not including your own ID file (called "R.java").
So remove those two lines, and include this one:
import com.mycompany.mailscomunes.R;
The root of an activity xml should be of Layout type.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Text"/>
</LinearLayout>
Goto your project and delete the build folder and do a rebuild. I had the same problem and is fixed

choosing resource type during xml file creating in Eclipse

When I want to create a XML file in Android SDK installed ECLIPSE for the purpose of SharedPreferences Activity layout... I do not see that dialog box to appear where I can choose from.
Following are my files.
MainActivity.java
package com.preferences_activity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
}
}
Preferences.java
package com.preferences_activity;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:text="#string/button1" />
</RelativeLayout>
preferences_activity.xml
<?xml version="1.0" encoding="UTF-8"?>
In the preferences_activity.xml, this is what I get when I create a xml file.
But I wanted to choose a type called preferences while creating a xml file. preferences layout is one of the type of resource available in the dialog box. Others are like: Layout, Values, Menu, AppWidget Provider Searchable, Animation.
If I had a option to choose a preferences layout option while creating a xml file I would be getting the following code:
<?xml version="1.0" encoding="UTF-8"?>
<PreferencesScreen
xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>
My Question is again: Where can I get that dialog box from while creating xml file where I can choose the resource type?
I am sorry if this question sounds funny. But I am struggling with it for few days in a row.
Thank you all.
For future Visitors:
I was struggling because I was using eclipse app instead of the one that was under adt-bundle. If you use the app under the adt-bundle, that has a logo with {} and light green background than you will have an options to choose a layout or any sorts of resources while creating an xml file. Pls, one more thing to note, I was having hard time to find a wizard for creating a xml file ( I could do it manually with right clicking the project and other and choosing xml file).. but I m talking about the wizard on the menu bar, you should be able to see it if you use this app under adt-bundle instead of regular eclipse. My answer may not be that likable, but sharing for future android beginners.

Coding advice on Buttons and MediaPlayer (avoid choreographer skipped frames)

I've been making a soundboard app of my infant son's sounds. The app simply opens to MainActivity class where it displays a scrollable list of buttons that, when pressed, will playback a pre-recorded MP3 of my son screaming something.
Once it's debugging or running on my Nexus 7 there is a Choreographer error and somewhere between 50 and 90 frames are skipped. The warning is that the 'application may be doing too much work on its main thread'. On my device three random buttons refuse to play sound until the app is closed and re-opened but then another three random buttons refuse to work. This problem didn't occur when my app only had eight buttons in a simple LinearLayout (without ScrollView).
I'm a beginner to both java and Android programming but, from what I guess, my laughably poor code is taking too much memory to properly function. My question is either, how would you code a scrollable list of 33 buttons that play their own sound, or could someone teach me how to better my code, please?
MainActivity.java (showing only the first three buttons/sounds for this question):
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer ahh = MediaPlayer.create(this, R.raw.ahh);
final MediaPlayer dededeh = MediaPlayer.create(this, R.raw.dededeh);
final MediaPlayer neganegabunbunbug = MediaPlayer.create(this, R.raw.naganagbunbun);
Button bahh = (Button) this.findViewById(R.id.Ahhhbutton);
bahh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ahh.start();
}
});
}
}
And the activity_main.xml (again with only the first three buttons for this question):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/Ahhhbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#layout/button_shape"
android:text="#string/button1a"
android:textColor="#d1d1d1"
android:textSize="20sp" />
<Button
android:id="#+id/dededehbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#layout/button_shape"
android:text="#string/button1b"
android:textColor="#d1d1d1"
android:textSize="20sp" />
<Button
android:id="#+id/neganegabunbunbugbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#layout/button_shape"
android:text="#string/button1c"
android:textColor="#d1d1d1"
android:textSize="20sp" />
Button bdededeh = (Button) this.findViewById(R.id.dededehbutton);
bdededeh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dededeh.start();
}
});
Button bnaganagbunbun = (Button) this.findViewById(R.id.naganagabunbutton);
bnaganagbunbun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
neganegabunbunbug.start();
}
});
</LinearLayout>
</ScrollView>
</LinearLayout>
It's not the layout. As your error said, you shouldn't do any long-term work on the Main-Thread.
The main-thread (or on Android the UI-Thread) handles interaction with the user. For the UI to not freeze and feel fast, you should execute long-term actions off the UI-Thread.
Also, the problem is not the actual playback, but you loading all media-files:
MediaPlayer.create(this, R.raw.ahh);
As the docs for the create()-method suggest:
[...] When done with the MediaPlayer, you should call release(), to
free the resources. If not released, too many MediaPlayer instances
will result in an exception.
For the "off the UI-Thread"-thing, you have three options:
Directly can create a Runnable-implementing class that plays your sound and submit it to a ScheduledThreadPoolExecuter (maximum control, more code).
Use an AsyncTask (Android exclusive) to play the song (fairly simple).
If you want the sound-playback to continue even when the app is not currently in the foreground, use a Service (most work)
So, what you should do is:
Store a List of your raw-file references (the R.raw.ahh) and don't load them directly.
When a song is selected for playback, call create() on that songs ID and start playback off the UI-Thread.
When done, call release() to release the resources.

Get code string in Android

Ex: I have a project with UI contains a button..
Activity:
package android.tuanshaker.myproject.com;
import android.app.Activity;
import android.os.Bundle;
public class MyProjectActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
And file main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
I want to display code and xml of my project by textview..How do i can to get them?
Copy the xml, js code files to res/raw. Use Context.getResources().openRawResource() to read them.
In addition, you can use jHighlight to format those source codes (in a web view).

Unable to use example class posted here in my project

I have been trying everything to get this to work, but to no avail. I tried to use the (AutoResizeTextView) posted here: Auto Scale TextView Text to Fit within Bounds
I created a new class file in my project with the name "AutoResizeTextView" and pasted the above code.
Then i opened the main.xml file and put n the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.mn.rl.AutoResizeTextView android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
in the mainActivity.java file, i have the following code:
package com.mn.rl;
import android.app.Activity;
import android.os.Bundle;
public class rlActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
com.mn.rl.AutoResizeTextView txt = (com.mn.rl.AutoResizeTextView) findViewById(R.id.tv);
txt.setText("Hello");
}
}
I have no errors in the code, and it runs but there is no autoresizing. the text remains the same size. in the xml i have the autoresize textview layout_width and layout_height set to fill parent, yet the font remains small. also tried txt.resize().
What am i doing wrong? Please Help.
there is a slight possibility that that class is only meant for resizing down. not up.

Categories

Resources