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 8025943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:19:45+00:00 2026-06-04T23:19:45+00:00

I have a little problem here: I want to make a button in my

  • 0

I have a little problem here:
I want to make a button in my timer … But I want to put in my view with random position ..
NOTE: I work at Android 2.2 API 8 ..
thanks guys!

Here is my code:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final int i=1;
    Button a = (Button) findViewById(R.id.button1);
    a.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        new CountDownTimer(20000,1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                //how to create a button here!
                LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mainan);
                Button btn = new Button(NyaActivity.this);
                btn.setId(i+1);                    
                btn.setText("Button"+(i+1));    
                // i want to set position of button randomly ..                
                linearLayout.addView(btn);                                  
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub

            }
        }.start();
    }
});
}
}
  • 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-06-04T23:19:47+00:00Added an answer on June 4, 2026 at 11:19 pm
    Math.random()*width
    

    where width is the maxValue of the range will give you a random x position, and you can do the same for the height of course

    then you set your LinearLayout.LayoutParams

    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(marginLeft, marginTop, 0, 0);
    btn.setLayoutParams(params);
    

    Voila, random position inside your layout

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

Sidebar

Related Questions

I have a litte problem here. I want to join twice from the same
i have a little problem here. I am working on a little application wich
I am having a little problem here. I have a <script language =JavaScript runat=server>
I have a little problem in coding in OO way. Here is my code:
I have a little problem with javascript form submit issue, here is the script
I have run into a problem that is a little irritating. Here is my
I need a little push in the right direction. Here's my problem: I have
I using dropkick js to style dropdown forms but have one little problem. What
I'm new around here and i have a little problems with a C# application.
I have 'little' problem with javascript numbers and NaN . I'm retrieving one single

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.