7
\$\begingroup\$

I have 2 buttons for Terms and Conditions in my project, that I need to implement in multiple activities. I created the following class:

public class TermsAndConditionsView extends LinearLayout implements View.OnClickListener {
 private Context context;
 public TermsAndConditionsView(Context context) {
 super(context);
 this.context = context;
 }
 public TermsAndConditionsView(Context context, AttributeSet attrs) {
 super(context, attrs);
 this.context = context;
 }
 @Override
 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
 super.onLayout(changed, left, top, right, bottom);
 setUpLayout();
 }
 private void setUpLayout() {
 LinearLayout container = (LinearLayout) this.getChildAt(1);
 Button terms1 = (Button) container.findViewById(R.id.terms1);
 Button terms2 = (Button) container.findViewById(R.id.terms2);
 terms1.setOnClickListener(this);
 terms2.setOnClickListener(this);
 }
 @Override
 public void onClick(View v) {
 if (v.getId() == R.id.terms1) {
 String url = "myUrl1";
 ActivityUtil.openBrowser((Activity) context, url);
 } else if (v.getId() == R.id.terms2) {
 String url = "myUrl2";
 ActivityUtil.openBrowser((Activity) context, url);
 }
 }
}

And the terms.xml file:

 <?xml version="1.0" encoding="utf-8"?>
<com.example.TermsAndConditionsView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
 android:id="@+id/hosted_by_text_view"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:textColor="@color/title_grey"
 style="@style/text1"/>
<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:orientation="horizontal">
 <Button
 android:id="@+id/terms1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Terms 1"
 style="@style/Link2"/>
 <Button
 android:id="@+id/terms2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="5dp"
 android:text="Terms 2"
 style="@style/Link2"/>
</LinearLayout>
</com.example.TermsAndConditionsView>

Is this a good way of reusing a group of views?

My colleagues say that it's not ok to use an XML which can be modified easily because I should modify in the code also (for example changing the order of the views) and if I am not careful the app might crash and it might take some time to figure out from where the problem comes from.

So, what's the best way of reusing this group of Terms and Conditions buttons?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 11, 2015 at 14:19
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Usually I use this approach when I want to create custom views but I don't implement actions inside the view, instead I am using a callback like the click listener :).

Also another minor thing is that there is no need to do getChildAt() so you can find a specific view from a child view if you don't happen to have id's with the same names. Instead you can call findViewById directly on this(the root view).

answered Feb 11, 2015 at 14:47
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.