-1

I have following view I want to set gravity programmatically to ImageView but unfortunately I am not able to do it, I have read lot of SO questions and answers but still nothing working

   <?xml version="1.0" encoding="utf-8"?>
   <com.project.widgets.CustomRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frontRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listview_click"
    android:padding="@dimen/watchlist_front_row_padding">
   <LinearLayout
    android:id="@+id/linearLayoutMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imgDeleteStocks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingBottom="@dimen/watchlist_delete_img_selector_padding"
        android:paddingRight="@dimen/watchlist_delete_img_selector_padding"
        android:paddingTop="@dimen/watchlist_delete_img_selector_padding"
        android:src="@drawable/img_watchlist_delete_checked"
        android:visibility="visible" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0">

            <com.project.widgets.TextViewRobotoLight
                android:id="@+id/textViewCompanyName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="0.6"
                android:singleLine="true"
                android:text=""
                android:textColor="@color/color_primary_text_color_fivep"
                android:textSize="@dimen/watchlist_company_Name" />


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                android:clickable="false"
                android:gravity="right">

                <com.project.widgets.TextViewRobotoLight
                    android:id="@+id/textViewSecondColumn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="@dimen/watchlist_company_last_rate_left_padding"
                    android:text=""
                    android:textColor="@color/color_primary_text_color_fivep"
                    android:textSize="@dimen/watchlist_price" />
            </LinearLayout>


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:orientation="horizontal">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5">

                <com.project.widgets.TextViewRobotoLight
                    android:id="@+id/textViewExchange"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:singleLine="true"
                    android:text=""
                    android:textColor="@color/color_time_background"
                    android:textSize="@dimen/watchlist_symbol" />

                <com.project.widgets.TextViewRobotoLight
                    android:id="@+id/textViewFullName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/textViewExchange"
                    android:text=""
                    android:textColor="@color/color_time_background"
                    android:textSize="@dimen/watchlist_symbol"
                    android:visibility="visible" />

                <ImageView
                    android:id="@+id/imgfilter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/textViewExchange"
                    android:layout_alignTop="@+id/textViewExchange"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@+id/textViewExchange"
                    android:src="@drawable/icon_edit_watchlist"
                    android:textColor="@android:color/white" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toLeftOf="@+id/txtOpeningBracket"
                    android:gravity="center_vertical"
                    android:orientation="horizontal"
                    android:paddingRight="3dp">

                    <ImageView
                        android:id="@+id/imgChangeIndicator"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="@dimen/nifty_sensex_img_change_indicator_padding"
                        android:scaleType="fitEnd"
                        android:src="@drawable/img_negative_per_change" />

                    <com.project.widgets.TextViewRobotoLight
                        android:id="@+id/textViewThirdColumn"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        android:textColor="@color/color_time_background"
                        android:textSize="@dimen/watchlist_symbol" />
                </LinearLayout>


                <com.project.widgets.TextViewRobotoLight
                    android:id="@+id/txtOpeningBracket"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toLeftOf="@+id/textViewFourthColumn"
                    android:text="("
                    android:textColor="@color/color_time_background"
                    android:textSize="@dimen/watchlist_symbol" />

                <com.project.widgets.TextViewRobotoLight
                    android:id="@+id/textViewFourthColumn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toLeftOf="@+id/txtClosingBracket"
                    android:clickable="false"
                    android:text=""
                    android:textColor="@color/color_time_background"
                    android:textSize="@dimen/watchlist_symbol" />

                <com.project.widgets.TextViewRobotoLight
                    android:id="@+id/txtClosingBracket"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text=")"
                    android:textColor="@color/color_time_background"
                    android:textSize="@dimen/watchlist_symbol" />

                <com.project.widgets.TextViewRobotoLight
                    android:id="@+id/txtVolume"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/txtClosingBracket"
                    android:text=""
                    android:textColor="@color/color_time_background"
                    android:textSize="@dimen/watchlist_symbol" />

            </RelativeLayout>

        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <CheckBox
                android:id="@+id/nsebase_switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:button="@drawable/selector_nse_bse_switch"
                android:checked="true" />

            <com.project.widgets.TextViewRobotoLight
                android:id="@+id/textEquityCategory"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_margin="5dp"
                android:layout_toRightOf="@+id/nsebase_switch"
                android:background="@color/color_market_depth_header"
                android:paddingBottom="@dimen/watchlist_equity_category_top_bottom_padding"
                android:paddingLeft="@dimen/watchlist_equity_category_left_right_padding"
                android:paddingRight="@dimen/watchlist_equity_category_left_right_padding"
                android:paddingTop="@dimen/watchlist_equity_category_top_bottom_padding"
                android:text=""
                android:textColor="@android:color/white" />

            <com.project.widgets.TextViewRobotoLight
                android:id="@+id/txtBuy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/buy_sell_inbetween_margin"
                android:layout_toLeftOf="@+id/txtSell"
                android:background="@drawable/selector_watchlist_buy_btn"
                android:paddingBottom="@dimen/buy_sell_top_bottom_padding"
                android:paddingLeft="@dimen/buy_left_right_padding"
                android:paddingRight="@dimen/buy_left_right_padding"
                android:paddingTop="@dimen/buy_sell_top_bottom_padding"
                android:text="@string/string_buy"
                android:textColor="@color/color_primary_text_color_fivep" />

            <com.project.widgets.TextViewRobotoLight
                android:id="@+id/txtSell"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:background="@drawable/selector_watchlist_sell_btn"
                android:paddingBottom="@dimen/buy_sell_top_bottom_padding"
                android:paddingLeft="@dimen/sell_left_right_padding"
                android:paddingRight="@dimen/sell_left_right_padding"
                android:paddingTop="@dimen/buy_sell_top_bottom_padding"
                android:text="@string/string_sell"
                android:textColor="@color/color_primary_text_color_fivep" />
        </RelativeLayout>

    </LinearLayout>

</LinearLayout>
</com.project.widgets.CustomRelativeLayout>

so there is imgDeleteStocks which is inside LinearLayout which is inside my CustomRelativeLayout so I want to set imgDeleteStocks gravity to TOP pls help me

eLemEnt
  • 1,741
  • 14
  • 21

2 Answers2

1
  1. Wrap your ImageView with a relativeLayout

     <RelativeLayout
        android:id="@+id/imgDeleteStocks_rl"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" >
    
    <ImageView
        android:id="@+id/imgDeleteStocks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/ic_launcher"
        android:visibility="visible" />
    </RelativeLayout>
    
  2. Add program code

    ImageView iv = (ImageView)findViewById(R.id.imgDeleteStocks);
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)iv.getLayoutParams();
    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    iv.setLayoutParams(lp);
    
S.W.
  • 139
  • 8
  • To be aware, the relative layout's layout_height must be match_parent – S.W. Aug 08 '16 at 06:32
  • In your xml code, you have not set weightsum, therefore, simply modify android:layout_width="0dp" to android:layout_width="wrap_content" and remove android:layout_weight="1" – S.W. Aug 08 '16 at 06:34
0

Try this:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.TOP;
imgDeleteStocks.setLayoutParams(layoutParams);
Hello World
  • 2,764
  • 1
  • 11
  • 23
  • Just suggestion, I will recommend using LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams ) imgDeleteStocks.getLayoutParams(); instead of LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); – S.W. Aug 08 '16 at 05:58
  • @Hello its not working..@S.W. dude please give me proper answer man – eLemEnt Aug 08 '16 at 06:20