0

I have more than 1 relative Layout within single Layout and I want to Assign Different Color border for the Specified Relative layout. I am able to provide the border in the RelativeLayout with the Specific Color for One.Can i Assign Different Color Border within one xml Drawable File?

custom_alert_dialog_loancollection

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:orientation="vertical">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#F8F8F8"
            android:paddingLeft="10dp">

            <TextView
                android:id="@+id/dialog_text_Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Bimala Rai"
                android:textColor="#0F6B7D" />

            <View
                android:id="@+id/dialog_view"
                android:layout_width="1dp"
                android:layout_height="18dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/dialog_text_Name"
                android:background="#0F6B7D" />


            <TextView
                android:id="@+id/dialog_text_Member_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@id/dialog_view"
                android:text="Mem No:1"
                android:textColor="#0F6B7D" />


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#FFEEDC"
            android:paddingLeft="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Remaining Principal"
                android:textSize="12sp"

                />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/border_alert_dialog_loancollection"
            android:paddingLeft="10dp">

            <TextView
                android:id="@+id/custom_alert_amountprincipal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Amount:"
                android:textSize="12sp"

                />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:layout_toRightOf="@+id/custom_alert_amountprincipal"
                android:background="@drawable/border_loan_collection"

                />


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#C7DCEF"
            android:paddingLeft="10dp">

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Interest Amount(Interest)"
                android:textSize="12sp"

                />


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"

            android:paddingLeft="10dp">

            <TextView
                android:id="@+id/custom_alert_amountinterest"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Amount:"
                android:textSize="12sp"

                />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:layout_toRightOf="@+id/custom_alert_amountinterest"
                android:background="@drawable/border_loan_collection"

                />


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#D7EDD6"
            android:paddingLeft="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Interest Amount(Principal)"
                android:textSize="12sp" />


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:paddingLeft="10dp">

            <TextView
                android:id="@+id/custom_alert_amountinterestprincipal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Amount:"
                android:textSize="12sp"

                />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:layout_toRightOf="@+id/custom_alert_amountinterestprincipal"
                android:background="@drawable/border_loan_collection"

                />


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginRight="10dp"
            android:background="#FFFFFF">

            <Button
                android:id="@+id/alert_dialog_button_Save"
                android:layout_width="50dp"
                android:layout_height="35dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:background="#59B0C1"
                android:text="Save"
                android:textColor="#FFFFFF"
                android:textSize="12sp" />


            <Button
                android:id="@+id/alert_dialog_button_reset"
                android:layout_width="50dp"
                android:layout_height="35dp"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/alert_dialog_button_Save"
                android:background="#F26D7E"
                android:text="Reset"
                android:textColor="#FFFFFF"
                android:textSize="12sp" />


        </RelativeLayout>


    </LinearLayout>

border_alert_dialog_loancollection

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="2dp"
        android:color="#FFEEDC" />
    <corners android:radius="0dp" />
    <padding
        android:bottom="0dip"
        android:left="0dip"
        android:right="0dip"
        android:top="0dip" />
</shape>

LoanCollection

public class LoanCollection extends AppCompatActivity implements View.OnClickListener {

    ImageView searchImage, filterImage;
    TextView searchText, filterText;
    LinearLayout linearLayoutsearchloancollection, linearLayoutcentergrouploancollection, loancollectionalertdialog;
    RelativeLayout rl1, rl2, rl3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loan_collection_layout2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(false);
        getSupportActionBar().setLogo(R.mipmap.uranus_drawr);
        searchImage = (ImageView) findViewById(R.id.image_search_loancollection);
        filterImage = (ImageView) findViewById(R.id.image_filter_loancollection);
        searchText = (TextView) findViewById(R.id.textsearchloancollection);
        filterText = (TextView) findViewById(R.id.loancollection_filtertext);
        linearLayoutsearchloancollection = (LinearLayout) findViewById(R.id.linearLayoutsearchloancollection);
        linearLayoutcentergrouploancollection = (LinearLayout) findViewById(R.id.linearLayoutcentergrouploancollection);
        loancollectionalertdialog = (LinearLayout) findViewById(R.id.loan_collection_alertdialog);


        rl1 = (RelativeLayout) findViewById(R.id.rl_1);
        rl2 = (RelativeLayout) findViewById(R.id.rl_2);
        rl3 = (RelativeLayout) findViewById(R.id.rl_3);


        searchImage.setOnClickListener(this);
        searchText.setOnClickListener(this);
        filterText.setOnClickListener(this);
        filterImage.setOnClickListener(this);
        loancollectionalertdialog.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {


        switch (v.getId()) {


            case R.id.image_search_loancollection:
                linearLayoutsearchloancollection.setVisibility(View.VISIBLE);
                linearLayoutcentergrouploancollection.setVisibility(View.GONE);
                break;


            case R.id.textsearchloancollection:
                linearLayoutsearchloancollection.setVisibility(View.VISIBLE);
                linearLayoutcentergrouploancollection.setVisibility(View.GONE);
                break;

            case R.id.image_filter_loancollection:
                linearLayoutsearchloancollection.setVisibility(View.GONE);
                linearLayoutcentergrouploancollection.setVisibility(View.VISIBLE);
                break;

            case R.id.loancollection_filtertext:
                linearLayoutsearchloancollection.setVisibility(View.GONE);
                linearLayoutcentergrouploancollection.setVisibility(View.VISIBLE);
                break;

            case R.id.loan_collection_alertdialog:

                Dialog dialog = new Dialog(LoanCollection.this);
                dialog.setContentView(R.layout.custom_alert_dialog_loancollection);
                dialog.show();


                ((GradientDrawable) rl1.getBackground()).setStroke(10, Color.parseColor("#FFEEDC"));
                ((GradientDrawable) rl2.getBackground()).setStroke(10, Color.parseColor("#C7DCEF"));
                ((GradientDrawable) rl3.getBackground()).setStroke(10, Color.parseColor("#D7EDD6"));


        }


    }
}

logcat

05-08 16:31:47.352 1972-1972/com.example.user.uranustech E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.user.uranustech, PID: 1972
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.view.View.getBackground()' on a null object reference
                                                                               at com.example.user.uranustech.LoanCollection.onClick(LoanCollection.java:97)
                                                                               at android.view.View.performClick(View.java:5610)
                                                                               at android.view.View$PerformClick.run(View.java:22265)
                                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

This can be Achieved with Different Drawable xml file but i want to achieve within the Single Drawable xml file.Is this possible within the Same Drawable file?

Ghimire
  • 948
  • 1
  • 9
  • 28

2 Answers2

1

change your custom_alert_dialog_loancollection. xml with below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#F8F8F8"
        android:paddingLeft="10dp">

        <TextView
            android:id="@+id/dialog_text_Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Bimala Rai"
            android:textColor="#0F6B7D" />

        <View
            android:id="@+id/dialog_view"
            android:layout_width="1dp"
            android:layout_height="18dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/dialog_text_Name"
            android:background="#0F6B7D" />


        <TextView
            android:id="@+id/dialog_text_Member_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/dialog_view"
            android:text="Mem No:1"
            android:textColor="#0F6B7D" />


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#FFEEDC"
        android:paddingLeft="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Remaining Principal"
            android:textSize="12sp"

            />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/border_alert_dialog_loancollection"
        android:paddingLeft="10dp">

        <TextView
            android:id="@+id/custom_alert_amountprincipal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Amount:"
            android:textSize="12sp"

            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_toRightOf="@+id/custom_alert_amountprincipal"

            />


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#C7DCEF"
        android:paddingLeft="10dp">

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Interest Amount(Interest)"
            android:textSize="12sp"

            />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/border_alert_dialog_loancollection"
        android:paddingLeft="10dp">

        <TextView
            android:id="@+id/custom_alert_amountinterest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Amount:"
            android:textSize="12sp"

            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_toRightOf="@+id/custom_alert_amountinterest"

            />


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#D7EDD6"
        android:paddingLeft="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Interest Amount(Principal)"
            android:textSize="12sp" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_3"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:paddingLeft="10dp"
        android:background="@drawable/border_alert_dialog_loancollection">

        <TextView
            android:id="@+id/custom_alert_amountinterestprincipal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Amount:"
            android:textSize="12sp"

            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_toRightOf="@+id/custom_alert_amountinterestprincipal"

            />


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginRight="10dp"
        android:background="#FFFFFF">

        <Button
            android:id="@+id/alert_dialog_button_Save"
            android:layout_width="50dp"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:background="#59B0C1"
            android:text="Save"
            android:textColor="#FFFFFF"
            android:textSize="12sp" />


        <Button
            android:id="@+id/alert_dialog_button_reset"
            android:layout_width="50dp"
            android:layout_height="35dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/alert_dialog_button_Save"
            android:background="#F26D7E"
            android:text="Reset"
            android:textColor="#FFFFFF"
            android:textSize="12sp" />


    </RelativeLayout>


</LinearLayout>

and in java class use the below code

rl1 = (RelativeLayout) findViewById(R.id.rl_1);
        rl2 = (RelativeLayout) findViewById(R.id.rl_2);
        rl3 = (RelativeLayout) findViewById(R.id.rl_3);

        ((GradientDrawable)rl1.getBackground()).setStroke(10,Color.parseColor("#FFEEDC"));


        ((GradientDrawable)rl2.getBackground()).setStroke(10,Color.parseColor("#C7DCEF"));


        ((GradientDrawable)rl3.getBackground()).setStroke(10,Color.parseColor("#D7EDD6"));

Output shown in the image

Zakir hussain
  • 392
  • 1
  • 10
  • I have checked it and I have shared my full code and output . please use the whole code that I have shared. – Zakir hussain May 08 '17 at 10:57
  • I have updated mine java class also.It seems that i have error in the Java class .Please check it once.I have used the Whole Code as you provided – Ghimire May 08 '17 at 10:58
0

Yes, you can achieve this by using the below code.

<RelativeLayout
     android:id="@+id/rl"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/your drawble" />

    ((GradientDrawable)rl.getBackground()).setColor(Color.parseColor("#F8981D"));
Zakir hussain
  • 392
  • 1
  • 10