This is my first Android project, so bear with me. I created a custom view ("GameBoard" extends View) for drawing my game board and pieces by overriding the OnDraw. It all works fine. However, I'd like to add a tween-animated "You win" graphic at the end of the game. However, I can't figure out how to add an ImageView to my GameBoard view.
I read that I could use an XML file and add my custom view to it, but it didn't work. Here's my XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#001133" >
<view Class = "com.myname.firstgame.GameBoard"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</view>
</RelativeLayout>
I also tried it without the "view Class =" in the view tag. I also tried it without the ".GameBoard" in the view tag and specifying it in the "android:id=" tag. (All these attempts came from Google searches.)
If I can get it to work, I figure I can add an ImageView with my bitmap as the background, place it in the right spot, and animate it. I do a similar thing for my title screen and it works fine, but it uses an XML natively, without a custom view. (In hindsight, I'm wondering if I should have used XML rather than a custom view for the game board, but that's another matter.)
Can anyone help, please?
This seems to answer your question.
http://sankarganesh-info-exchange.blogspot.com/2011/04/performing-animation-in-android.html
just call the target.startAnimation() method when your user wins / loses / whatevers your game
Related
I created an Activity, which I load from a fragment within my "main" activity.
Within this new activity I placed an ImageView with an image from my drawable directory (I have about 10 other drawables I present the exact same way in the main activity).
The problem is that although in the IDE I see the image, it is not displayed in run time (via the simulator).
-- to clarify, the image is static hence the difference compared to other questions (I'm not loading it by code).
Any ideas on how to solve it?
My hunch is that it relates to the fact it's a new activity, but I'm new to android development, so I can't base it on any knowledge...
I did not add any code to the java section of the activity (didn't touch any "on.." nor added functionality to this specific file)
P.S. I tried cleaning the project, tried presenting an image that is presented on the main activity, and restarting anything that I can... nothing helped :(
My Activity xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="reducted">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/manageSubscriptionScreenLogo"
android:layout_width="match_parent"
android:layout_height="110dp"
app:srcCompat="#drawable/managesubscription_header" />
</LinearLayout>
</RelativeLayout>
It doesn't matter I take the ImageView and place it outside of the LinearLayout (and remove it), or change the size to wrap_content or match_parent (I tried all combinations), the image is not presented.
How it looks in the IDE:
And how it looks in run time:
Try using android:src="#drawable/managesubscrip_header" instead of app:srcCompat="#drawable/managesubscription_header" in ImageView to avoid automatic scaling of the drawable.
Let me know if it helps
Just to be clear, we're talking about an XML rotated view here, not the effects of rotating the device. I have a SlidingDrawer that contains a ListFragment. The ListFragment implements the Filterable interface so that users can search its contents by providing a string input through an EditText.
The relevant layout is included below. Because the SlidingDrawer class was deprecated in API 17, the source code was copied over an accessed via a local class. That's why the name of that view looks like a custom class when really it's not.
<com.example.echo.views.SlidingDrawer
android:id="#+id/left_sliding_drawer"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:handle="#+id/left_handle"
android:orientation="horizontal"
android:content="#+id/people_fragment_container"
android:rotation="180">
<LinearLayout
android:id="#id/left_handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/people_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/people_map_tab_grey"
android:rotation="180"
android:contentDescription="#string/people_tab"/>
</LinearLayout>
<FrameLayout
android:id="#+id/people_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.example.echo.views.SlidingDrawer>
What happens is, when the users provides input and filters the list such that there are no matching results, i.e., the ListView is empty and size is 0, the entire SlidingDrawer disappears.
Some things I've noticed in trying to fix this:
I am pretty sure this is related to displaying the empty view and/or whatever layout change occurs when it is displayed. If I simply do not set an empty view for the ListFragment the issue does not occur.
I am also pretty sure the effects are related to the fact that the SlidingDrawer is being rotated by 180 degrees since, if I remove the rotation attribute, the issue also does not occur. However, because SlidingDrawer in its default state only opened right to left, this drawer applies the XML attribute android:rotation="180" to flip the view so that it can be opened left to right. This must remain since there is other stuff on the right side of the screen that cannot be moved.
I'm not sure what is making the view disappear or where to start fixing it. I've trying fixing the child views' sizes by overriding onMeasure, onSizeChanged, and onLayout but cannot find anything that solves the issue.
Any ideas are appreciated.
I'm trying to port an app from iOS to Android. In my iOS app I have an image view which displays a map, and on top of it I want to display multiple button views on certain specific positions.
None of the standard views in Android seem to fit my needs, but I am surely missing something. How could I achieve this?
Thanks
The easiest thing to do would be to use any layout that's best suited for your needs (LinearLayout, FrameLayout, RelativeLayout, etc.) and set its background to any image you need. I believe the image will automatically scale to fill the corresponding Layout. Put the image into the appropriate drawable folders - and format your XML similar to this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_image">
...
</RelativeLayout>
I recently updated my phone to gingerbread and to my horror one of my apps stopped working!
The app consists of a SurfaceView and a layout (view) that shows up as an overlay over this SurfaceView.
THe xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.mycompany.MySurfaceView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<include layout="#layout/settings"
android:layout_alignTop="#id/myview"
android:visibility="invisible"
android:id="#+id/settings"
/>
</RelativeLayout>
When the user press the menu button I use this code to show the view:
View settings = (View)findViewById(R.id.settings);
settings.setVisibility(View.VISIBLE);
This worked flawlessy on versions earlier than 2.3 but now the behaviour is very random.
Observations
Very rarely everything works.
Sometimes the view shows up after hiding / unhiding it.
Sometimes you can press the buttons on the view (even though it's invisible) this makes the view show up for the rest of the application lifetime.
The getVisibility method always report the correct value.
What I've tried
Rebuilding draw cache
Using BringToFront method
Post invalidate
Setting the initial value of the view to "visible", this makes everything work, but I really want it to be initially invisible!
If anyone wants to check the problem you can try it for yourself in my app (it's on market, search for "chainparticles").
Thanks!
Solved it by changing the views initial state to "gone". WTF?!
I am designing a simple Android game using a surfaceview similar to the Lunar Lander sample provided by Google. I want to have various things such as highscores, messages, menus etc pop up on screen with the surface view still in the background.
The problem is that I want the "pop ups" to contain many design elements such as images, textboxes etc. Is there a way that I can create a layout in XML to design the pop up I want and then display that over the surfaceview as one object rather than having to construct each individual element one by one in code and paint them all to the canvas?
I want to animate the pop ups in from the side of the screen, so it would be good if I design the layout in XML so that all the object know their relation to each other and then animate that layout as one object.
It strikes me as something quite simple but I just cant seem to find how to achieve it.
You can overlay elements on top of SurfaceView either in code or in your XML layouts.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<android.view.SurfaceView
android:id="#+id/mysurface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</android.view.SurfaceView>
</FrameLayout>
And in your Actiivty:
TextView myText = new TextView(this);
myText.setText("Something");
addContentView(myText, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Don't know about the performance issues though.
A simple way of doing this is creating your popup interface as a different Activity. You would then give a transparent background theme to it, like the Dialog theme, in your manifest file.
Look at the CustomDialog, Translucent and TranslucientBlur samples in API Demos. They create such activites. The fact that the underlying activity is in a surface shouldn't change anything.