Why can't I set the gravity for my view

I want to set the gravity of a view (in my .java file) but for some reason it can't be done this time (it states that there is no such method). But normally setGravity(Gravity.CENTER); will just be considered valid code.

So why can't it be done all the time? What should I do when I can't set the gravity of a view but I still want to align it to mid( like setGravity(Gravity.CENTER);)?

AScrollView middle = new AScrollView(context);

RelativeLayout.LayoutParams formid = 
new RelativeLayout.LayoutParams(width,LayoutParams.WRAP_CONTENT);

middle.setLayoutParams(formid);

middle.setGravity(Gravity.CENTER); //This can't be done..

Upvotes: 1

Views: 129

Answers (4)

Veerababu Medisetti
Veerababu Medisetti

Reputation: 2755

try this code. 

        ScrollView middle = new ScrollView(this);
        middle.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.CENTER));

let me know the status.

Upvotes: 0

Ridcully
Ridcully

Reputation: 23665

Layout Gravity is not supported by RelativeLayout. It is supported e.g. by LinearLayout where you can set it via the public property gravity of the LinearLayout.LayoutParams class.

Upvotes: 0

Sunil Kumar
Sunil Kumar

Reputation: 7082

use this way..First add the view middle then after try to set gravity

formid.addView(middle) ;
middle.setGravity(Gravity.CENTER);

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

I can't say exactly why you can't set Gravity on ScrollView but I'm pretty sure you can't. However, if you have a ScrollView then you should have some Layout or content inside of it to scroll. Simply set the Gravity on that Layout and it should give you the effect you want

Upvotes: 1

Related Questions