0

Progress bar not centre alignI have made a customView for a .gif image support in android. I got success in that, but currently that view is aligned to the top left corner of my activity. I want it to be aligned to the center of my activity. Please tell me how can I do this.

My code is as below:

GifwebView  view = new GifwebView(this, "file:///android_asset/p3.gif");
view.setBackgroundColor(Color.parseColor("#00000000"));
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rl_pics);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel.setBackgroundColor(Color.parseColor("#00000000"));
view.setLayoutParams(p);
rel.addView(view);

CustomView

public class GifwebView extends WebView {

    public GifwebView(Context context, String path) {
        super(context);
        // TODO Auto-generated constructor stub
        loadUrl(path);
    }

}
user3820044
  • 177
  • 1
  • 3
  • 20

2 Answers2

1

Your activity main layout (RelativeLayout), it's height and width should be match_parent, so it will take the whole screen, and it's gravity should be set to center, so that the elements in it will be positioned to center of the screen.

XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_pics"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
</RelativeLayout>

Activity:

RelativeLayout rel = (RelativeLayout) findViewById(R.id.rl_pics);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

GifwebView  view = new GifwebView(this, "file:///android_asset/p3.gif");
view.setBackgroundColor(Color.parseColor("#00000000"));
view.setLayoutParams(p);

rel.addView(view);
Marko
  • 20,385
  • 13
  • 48
  • 64
  • I have edited my code as per yours,But no changes are there in this.Its not working..! – user3820044 Sep 08 '14 at 06:49
  • I have tested the code with a TextView and it is working. Be sure to set your RelativeLayout's height and width to match_parent in XML layout, set gravity to center and your custom views width and height to wrap content, just add it to the relative layout and it should be positioned in the center. If not, there has to be a problem in your custom view. Provide code for custom view to see what is wrong. – Marko Sep 08 '14 at 07:04
  • Marko..But my view is a customView not a textview.! – user3820044 Sep 08 '14 at 07:10
  • I will try it when I get home later in the evening and will get back to you. There may be a problem with the WebView. Although I would rather display the original Android's progress dialog / bar for loading, would make things easier and would look nicer. – Marko Sep 08 '14 at 07:47
  • but can it possible to put a gif image to preogressDialog?Gif file format not supported in anroid,I want to make a progress Dialog like "500px" app.. – user3820044 Sep 08 '14 at 07:54
  • As I can see you want to display .gif to indicate loading. When using Android's progress dialog/bar you do not need an gif/image, it is already taken care of. You just initialize progress dialog/bar, show it when load starts, and hide it when the loading part is done. Check this SO question for how to show a progress dialog http://stackoverflow.com/questions/9814821/show-progressdialog-android – Marko Sep 08 '14 at 08:03
  • I know it very well friend,Thing is that i want to put a specific image in progress dialog. – user3820044 Sep 08 '14 at 09:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60812/discussion-between-marko-and-user3820044). – Marko Sep 08 '14 at 10:01
0
p.addRule(RelativeLayout.CENTER_HORIZONTAL);
view.setGravity(Gravity.CENTER_VERTICAL);

This should work. Also if you want the activity to take the whole screen instead of the requiered for filling your content you should use match_parent instead of wrap_content.

edit: if your custom view doesn't implement the setGravity(int) method, you can do this instead:

p.addRule(RelativeLayout.CENTER_HORIZONTAL|RelativeLayout.CENTER_IN_PARENT, view.getId());
Ssr1369
  • 348
  • 3
  • 16