Multiple TextViews in a SurfaceView - java

I am creating an android game, I tried using the canvas.drawText() method to display my score and level but it caused errors. Now I am trying to use TextViews to display them, I am using a SurfaceVeiw and I wanted to know is it possible and would it be a good way of doing it.

You can't really put TextViews into a SurfaceView. What you can do instead, though, is add TextViews on top of the SurfaceView. Let me know if you need help on doing that if you don't know how.
// You can use a FrameLayout to hold the surface view
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(surfaceView);
// Then create a layout to hold everything, for example a RelativeLayout
RelativeLayout relativeLayout= new RelativeLayout(this);
relativeLayout.addView(frameLayout);
relativeLayout.addView(textview1);
relativeLayout.addView(textview2);
setContentView(relativeLayout);
When adding your views, it may be helpful to using LayoutParams to help organize things. Use this documentation for LayoutParams. Hope this helps!
If you're using an XML layout, I would use a RelativeLayout, as mentioned above, contain the SurfaceView inside a FrameLayout. Placing the TextView's on top of the FrameLayout is just a simple matter of configuring the RelativeLayout parameters for each view. You seem new to Android, perhaps this guide will help you with RelativeLayouts and XML layouts in general.

Related

Achieving smooth scroll with recyclerview android design library

I wish to achieve following smooth with android layout without having to implement custom linear layout manager. My question is following achievable with just combination of android design library and nestedscrollview.
<ParentLayout>
Textview
Imageview
Gallery(viewpager)
Recyclerview
Textarea
Textview
Textview
</ParentLayout>
I wish to achieve smooth scroll throughout the parentlayout. if i am at last element of the recyclview the scroll should continue to parentlayout. How can this be achieved and which parentlayout should be used?
From my understanding of your needs, you don't need a nested nestedscrollview. Just place your recyclerview inside of a parent scrollview and you should be good to go
If you provide a code sample or a wireframe of what you want to achieve, it'll be easier to help :)

How to dynamically load imagebuttons on android?

as the title says, I am trying to figure out how to dynamically load imagebuttons using Android Studio. I have already pre-loaded the drawable folder with all of the images I want to make into imagebuttons.
I am using a relativelayout for this app and the main screen will scroll down. I am trying to do this without using the XML file since it seems like it makes it harder to do things dynamically.
How can I create x number of imagebuttons with these conditions? If using the xml folder would make it easier, can someone help me understand it better?
Create a custom ScrollView which find that user reach to bottom. Then inflate your view which have ImageButton. Add that inflated view in your RelativeLayout or LinearLayout which will be inside ScrollView.
Refer this:- Detect end of ScrollView

Centering Object In LinearLayout (Android)

To StackOverFlow,
I am currently developing an Android App, and am facing a issue. The issue is that when I place two view items under a parent (LinearLayout) it will not center one view to use the entire width of the parent. The reason for this (I beleive) is because when the second view uses parent_fill it already takes the width of the previous view into account. So it centers the "empty space". The code for the layout is below.
http://i.stack.imgur.com/byqG3.png
As you can tell it is a simple layout. The problem is shown below.
http://puu.sh/jiKIV/4a30800776.png
The problem is that the Title Bar ("Mah App") is not centering on the entire Linear Layout Bar.
So my Question is how do I make the Title Bar center across the entire Linear Layout even though there are other views are on there as well?
Thanks,
Thomas.
Layout Weight is the concept you are missing here. Look at this:
http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
In your case, you need to define android:layout_weight=1 for the view you want to center. This will make that view more important than the rest on the parent.
But really the best way to center views is to use a RelativeLayout instead of LinearLayout. Aligning possibilities are much better

How to Add Rules to align views inside Cardview in Android

I have a cardview inside which I have several Views which I want to align one below another. For a relative layout I will use the following
layoutParams.addRule(RelativeLayout.BELOW, idofanotherview);
However I cannot find addRule() method for cardview, how do I achieve this?
As the documentation of the CardView states, this component inherits from FrameLayout. That is normal that the addRule() method is unrecognized.
If you really need to use a RelativeLayout to position your subviews then I suggest that you wrap your subviews with one.
This post gives you a good exemple of how to achieve what you need
Good luck!

Does a paint app on an android phone require an xml layout?

Do i need a layout for a view for when the user "paints" on the screen? And what type of layout should i have? Can it be virtually anything? Like a white screen? Thanks.
You need to use at least one layout which will act as your holder View.
It can be a white screen or whatever you want etc.Instead of programming creating all the views make a holder View for your paint stuff and do the painting on it.
No, XML layouts are optional. You can create it programmatically:
View view = new MyCustomPaintView(context);
setContentView(view);
But XML layouts are very usefull (and strictly recommended) when you need to place many Views on a screen.

Categories