Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6253377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:58:58+00:00 2026-05-24T13:58:58+00:00

android newbie here. My first game involves a custom view, which is going to

  • 0

android newbie here.

My first game involves a custom view, which is going to draw a game board and some scoreboards on the screen. I need to know how many players there are in order to get the number of scoreboards up, and I need this to be in known in the constructor of the custom view, so that appropriate variables are initialised on time.

My current implementation is like this, is this the correct way to get variables into the custom view constructor?…

I instantiate the custom view from my activity like this:

numPlayers=2;
setContentView(R.layout.gamescreen);
mBoardView = (BoardView) findViewById(R.id.board_view);

And in the custom view constructor:

public BoardView(Context context, AttributeSet attrs){
            super(context, attrs);
vNumPlayers = ((GuappsXOMainGame)getContext()).getNumP(); 

That’s how I have it now and it seems to work well enough, but is it better to be doing something along the lines of the answer to this question:?

android:how to instantiate my custom view with attributeset constructor

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:58:58+00:00Added an answer on May 24, 2026 at 1:58 pm

    When a custom view (or SurfaceView) is the only view, often in games, then it is very easy to pass parameters like level, players, sound on etc by creating an instance of this view before setContentView(). An example:

    MyView myView = new MyView(level, players, soundOn);
    setContentView(myView);
    

    In your case, your custom view is instantiated when the layout is created, hence you have to communicate back to the activity object to get the values. This is also possible. I have done this by setting static variables in the activity before calling setContentView(my_layout) then in the constructor for the custom view just say level = MyActivity.level

    Or perhaps the way you are doing already where you obtain the instance of the activity and call a public method or variable.

    In the link you provided an attribute is “hard-wired” into the XML layout. I don’t see the advantage of that, when one can simply enter the value into the custom view class itself as a “final” variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Extreme Android developer newbie here...well, new to Android development, not development in general. I
I am newbie to android game development. I've been trying an example of game
I'm an Android newbie, and working on an app which needs an Options menu.
I'm a completely newbie to android programming, having done some java for my computing
Android developer newbie here. I am trying to develop a GUI wrapper in Android
I am a Android newbie. I guess I might be doing something stupid here.
Java newbie here. I am using a third party library which gives me a
I am newbie in android so i am not famalier to all errors here
I'm a newbie into Android programming... Here's what I'm trying to do for a
Well, i am an Android newbie. I am trying to write an application which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.