2

I have this view

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView10"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView11"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView12"
    android:layout_below="@+id/textView11"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="55dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView13"
    android:layout_below="@+id/textView11"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

I want to create a custom subclass of RelativeLayout that every added item is this view, adding them would be like this: myCustomRelativeLayout.addItem("text1", "text2", "text3", "text4", marginTop, height), how do I do this?

Here's what it would look like if I added 3 views like this:

myCustomRelativeLayout.addItem("left1", "left one", "right1","right one", o, 200);
myCustomRelativeLayout.addItem("left2", "left two", "right2","right two", 150, 200);
myCustomRelativeLayout.addItem("left3", "left three", "right3","right three", 350, 200);

I'm not an expert, detailed explanations are welcomed

7
  • could you provide more explanation ? like adding representative images. Commented May 9, 2016 at 0:13
  • Note that a frame layout can have only one child Commented May 9, 2016 at 0:17
  • @kaanyılmaz please see edited question. Commented May 9, 2016 at 0:34
  • @F43nd1r frame layout can host more than one child, although it may not be easy to organize them Commented May 9, 2016 at 0:34
  • 1
    A RelativeLayout looks good based on what I can see from your code. It allows overlays and provides easier ways to position children. Commented May 9, 2016 at 0:44

1 Answer 1

1

You can do it by creating a custom view with those texts and just add that custom view into another layout:

public class CustomView extends RelativeLayout implements View.OnClickListener{

public TextView ClassName, Room, StartingTime, EndingTime;
public float Size;
public RelativeLayout mContainer;

// constructor
public CustomView (Context context, String subject, String room, String startingTime, String endingTime, int background, double size) {
    super(context);
    init(context);
    ClassName.setText(subject );
    Room.setText(room );
    StartingTime.setText(startingTime);
    EndingTime.setText(endingTime);
    mContainer.setBackgroundColor(background);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)size);
    params.setMargins(3,0,3,0);
    mContainer.setLayoutParams(params2); 
}


private void init(Context context) {
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mInflater.inflate(R.layout.my_view, this, true);

    this.mContainer = (RelativeLayout)findViewById(R.id.ClassContainer);
    this.ClassName = (TextView)findViewById(R.id.theClassName);
    this.Room = (TextView)findViewById(R.id.theClassRoom);
    this.StartingTime = (TextView)findViewById(R.id.theClassStartingTime);
    this.EndingTime = (TextView)findViewById(R.id.theClassEndingTime);
}

Create one like this:

CustomView mClass = new Schedule_Class_mini(getActivity(),
                                "Math",
                                "Room 8",
                                "10:30",
                                "12:00",
                                Color.parseColor("#508ACCA4"),
                                150);

Container.addView(mClass, params);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.