1
 LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
 View overView = mInflater.inflate(R.layout.settings, null); 
 addContentView(overView,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

Above shown is my code for overlaying a view on another view. But my overlayed layout appears on left side. But I have set android:layout_gravity="right" in my xml.

And I tried adding overView.gravity =Gravity.RIGHT. Here Gravity is imported but yet it gives error for gravity. Error: gravity cannot be resolved or it is not a valid field. That is what is shown . Some explanation for why that happens? . Do I have to write a field call gravity?.

Update : as requested by user Amr

The below code is written for my settings.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
    android:layout_width="200sp"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:layout_gravity="right"
    android:gravity="right"

 xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#138DD4">

        <Button
            android:id="@+id/back"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:background="#12A5F4"
            android:drawableLeft="@drawable/icon"/>

        <TextView
            android:id="@+id/title_settings"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:background="#138DD4"
            android:textSize="20sp"
            android:text="@string/title_settings"
            android:textColor="@android:color/white"
            android:gravity="center"/>

        </LinearLayout>


    <ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center"

            >



        <Button
            android:id="@+id/acc_settings"
            android:layout_width="200sp"
            android:text="@string/title_account"
            android:background="@drawable/button_border"
            android:textSize="15sp"
            android:layout_height="40sp"
            android:layout_gravity="center"
            android:gravity="center"
         />



        <Button
            android:id="@+id/profile_settings"
            android:layout_width="fill_parent"
            android:layout_height="40sp"
            android:text="@string/title_profile"
            android:background="@drawable/button_border"
            android:textSize="15sp"
            android:layout_gravity="center"
         />


        <Button
            android:id="@+id/parent_settings"
            android:layout_width="fill_parent"
            android:layout_height="40sp"
            android:text="@string/title_parent_details"
            android:background="@drawable/button_border"
            android:textSize="15sp"
            android:layout_gravity="center"
         />



        <Button
            android:id="@+id/help_info"
            android:layout_width="fill_parent"
            android:layout_height="40sp"
            android:text="@string/title_help"
            android:background="@drawable/button_border"
            android:textSize="15sp"

            android:layout_gravity="center" 

         />

        <Button
            android:id="@+id/about_info"
            android:layout_width="fill_parent"
            android:layout_height="40sp"
            android:text="@string/title_about"
            android:background="@drawable/button_border"
            android:textSize="15sp"
            android:layout_gravity="center" 
         />



        <Button
            android:id="@+id/logout"
            android:layout_width="fill_parent"
            android:text="@string/logout"
            android:background="@drawable/button_border"
            android:textSize="15sp"
            android:layout_height="40sp"
            android:layout_gravity="center"

          />



        </LinearLayout>
        </ScrollView>

  </LinearLayout>

And when I check with my graphical layout tab . this layout shown as I expected. Only thing after I run the project. It is still on left side.

SMK
  • 324
  • 3
  • 14
  • `View` class does not have a variable called `gravity` that you can use directly. – Apoorv May 27 '14 at 09:26
  • So what can I do to set the gravity I need?. Any other suggestions? – SMK May 27 '14 at 09:32
  • `FrameLayout.LayoutParams layoutParams_webview = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layoutParams_webview.gravity= Gravity.CENTER_VERTICAL |Gravity.CENTER_HORIZONTAL;`and applying this to the required element will give center but it will be shifted to the left. – pallavi Jun 17 '15 at 05:53

3 Answers3

0

actually View object dos not have setGravity so case the View object to what ever it's, like TextView or button or LinearLayout, i recommended you to do something like that

LinearLayout overView = (LinearLayout) mInflater.inflate(R.layout.settings, null);
overView.setGravity(Gravity.RIGHT);
NoXSaeeD
  • 924
  • 1
  • 7
  • 13
  • I tried this. Now no error but after running it is still there where it was before not where I have set to be – SMK May 27 '14 at 09:46
  • I think it's better to change the gravity of your desired layout using "android:layout_gravity=..." from the xml file itself – Amrmsmb May 27 '14 at 10:33
  • can you post the layout "settings" the one you want to change its gravity – Amrmsmb May 27 '14 at 10:33
  • @Amr My code is there with the question now. Have a look at it. – SMK May 27 '14 at 11:41
0

I think, in such case you need to modify the margins of the views contained in the layout that should inflates your main layout.And as far as i know, it is better to wrap your views within a relativelayout. please have a look at the example posted below, it should work

Activity_Code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LayoutInflater li = LayoutInflater.from(getApplicationContext());
    View v = li.inflate(R.layout.inflater, null);
    addContentView(v, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.layoutinflatertest00.LayoutInflaterTest00"
tools:ignore="MergeRootFrame"
android:orientation="vertical" />

inflater.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="200sp"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:layout_gravity="right"
    android:gravity="right|center_vertical">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#138DD4"
        android:layout_gravity="right|center_vertical">
        <Button
            android:id="@+id/back"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:layout_marginLeft="100dp"
            android:text="button"/>
       <TextView
            android:id="@+id/title_settings"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:background="#138DD4"
            android:textSize="20sp"
            android:text="text text text"
            android:textColor="@android:color/white"
            android:gravity="center"
            android:layout_toRightOf="@+id/back"/>
       </RelativeLayout>

Amrmsmb
  • 1
  • 27
  • 104
  • 226
0

wrapping the views with linearlayout helps . referred this link setting gravity for inflated layout

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/playoverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:padding="10dp">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <WebView
        android:id="@+id/webView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:scrollbars="none" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>
</LinearLayout>


</RelativeLayout>
pallavi
  • 432
  • 3
  • 14