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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:34:57+00:00 2026-06-06T18:34:57+00:00

Loading class (View): public class Loading extends View { private long movieStart; private Movie

  • 0

Loading class (View):

public class Loading extends View {

    private long movieStart;
    private Movie movie;

    public Loading(Context context, InputStream inputStream) {
            super(context);
        movie = Movie.decodeStream(inputStream);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        super.onDraw(canvas);
        final long now = SystemClock.uptimeMillis();
        if(movieStart == 0)
            movieStart = now;
        final int relTime = (int)((now - movieStart) % movie.duration());
        movie.setTime(relTime);
        movie.draw(canvas, 100, 100);
        this.invalidate();
    }

}

Activity onCreate method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    InputStream inputStream = null;
    try {
       inputStream = getAssets().open("loading.gif");
    } catch(IOException e) {
      e.printStackTrace();
    }
    Loading loading = new Loading(this, inputStream);
    setContentView(loading);
}

I want to set the view in center of the device layout. Like in XML layout, that we can set android:layout_centerHorizontal=”true”. How can I do it?

  • 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-06T18:34:58+00:00Added an answer on June 6, 2026 at 6:34 pm

    You need to set LayoutParams for the View. What I would do is put your custom view in a container, for example a FrameLayout. Then, create your view and set the FrameLayout.LayoutParams for your View with gravity set to CENTER_HORIZONTAL. It’ll look something like this

    FrameLayout frame = new FrameLayout(context);
    
    FrameLayout.LayoutParams flParams = new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER_HORIZONTAL
    );
    
    Loading loading = new Loading(this, inputStream);
    loading.setLayoutParams(flParams);
    frame.addView(loading);
    setContentView(frame);
    

    You may want to configure the FrameLayout to get it to display how you want.

    Here’s the documentation for the LayoutParams constructor I’d use: http://developer.android.com/reference/android/widget/FrameLayout.LayoutParams.html#FrameLayout.LayoutParams%28int,%20int,%20int%29

    It’s important to remember that each of the Views that extend ViewParent have their own LayoutParams class, so, for example, if you wanted to use a LinearLayout instead of a FrameLayout, you’d use LinearLayout.LayoutParams. Different types of Params have different constructor options, so you can play around with it and find which is best for you. Good luck.

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

Sidebar

Related Questions

I have a custom view: public class Loading extends View { private long movieStart;
page.php (Example Controller Page) opened as http://page/view/ class page extends fwcontroller { public function
My code as follows: public class wall extends Activity { GridView webgridview; Button web;
I have a class that extends Activity and within it got a private class
I have read about dynamically loading your class files when needed in a function
I'm trying to optimize the startup time/class loading time of my Java web app
Please provide a good, thorough tutorial about Java class loading , focusing on how
I am dynamically loading a partial class with dropdownlists. The dropdownlists represent the many
Any re-sources or best-practices for implementing dynamic class re-loading feature into a J2EE WebApp
I am loading an external SWF using the Loader class, and adding it to

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.