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

  • SEARCH
  • Home
  • 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 7059391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:13:35+00:00 2026-05-28T04:13:35+00:00

How can I construct this scenario? I want to build a game where the

  • 0

How can I construct this scenario?

I want to build a game where the game board is a canvas controlled by a custom View but within a ScrollView. The game board is potentially much bigger than the screen and I want to scale, scroll around the game. However, I want all the advantages of the UI outside the game board to include Buttons, TextViews, ImageViews, etc.

Here’s the outline code of what I imagine should work. Am I wrong in thinking I can use both
my own onDraw and expect buttons and other widgets to be managed by Android? Must I implement
all my own UI ? Just trying to deal with a Button (see below) fails me Here’s the XML where I want my custom view surrounded by other UI elements.

I’ve looked at a lot of other examples and tutorials but have yet to see a custom view placed with an activity with UI elements around it.

My main activity

// Activity 
public class gameStartActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.game); 
    } 
} 

My custom view

// gameView.java 
public class gameView extends View { 
  public gameView(Context context) { 
    super(context); 
    setFocusable(true); //necessary for getting the touch events 
   InitBoardView(); 
 } 
private void InitBoardView(){ 
    Button myButton = (Button)findViewById(R.id.editname); 
-----> fails at this point as myButton is null 
    myButton.setOnClickListener(doSomething); 
} 
@Override 
protected void onDraw(Canvas canvas) { 
  super.onDraw(canvas); 
 canvas.drawBitmap(face, getPaddingLeft(), getPaddingTop(),paint); 
} 
@Override 
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) 
{ 
  super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
  final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
  setMeasuredDimension(xSize, ySize); 
} 

game.xml

 <ScrollView 
        android:layout_width="240dp" 
       android:layout_height="600dp" 
    <com.android.example.game.gameView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"/> 
  </ScrollView> 
  <TextView 
     android:id="@+id/sometext 
     android:layout_width="120dp" 
     android:layout_height= "wrap_content" 
     android:layout_weight="1" 
     android:visibility="gone"/> 
  <Button 
     android:id="@+id/editname" 
     android:text="DoIt" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"> 
 </Button> 
  • 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-28T04:13:36+00:00Added an answer on May 28, 2026 at 4:13 am

    findViewById() would work in a custom view only if:
    – You extend from ViewGroup (or another subclass of ViewGroup)
    – The Button is a child of your custom view

    A better way of doing this would be to find the button in the Activity and set its listener there. You could also have a setButton(Button view) method in your custom view and have the activity do the following:

    ((MyCustomView) findViewById(R.id.myCustomView)).setButton((Button) findViewById(R.id.myButton));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given some typical search form, I can't construct this form action when submitting a
Can you please tell me what kind of construct in C# is this. Code
How can I construct a query against an custom IUserType field in NHibernate? More
Can I use default arguments in a constructor like this maybe Soldier(int entyID, int
Can I do this without reference to the object in the constructor? In other
This was the question asked in interview. Can we call one constructor from another
Is there any way I can construct an new object from the given object
In python, I can construct my optparse instance such that it will automatically filter
How can I construct an object using a query if I also need to
how can i construct a artificial request to login to twitter or any site

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.