0

Similar to a to do app. I have a top bar that is set by the xml file and at run time I would like to create some textviews which go directly under it. Also there will be a button that adds new text views when clicked.

When I create a new textview from the button how do I make sure that the new Textview stays under the static textviews and/or layouts that I have created. Right now it's not and it's just overlapping.

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

    Button btn = (Button) findViewById(R.id.Add_new);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddNewTask(v);
        }
    });
}

I tried both options to add a new textview and both of them just overalpp and I can't find any documentation on a method such as under/below, most I can find are just alight top and align bottom which wouldn't be useful in this case

public void AddNewTask(View v) {
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.overallview);
    TextView tv= new TextView(this);
    tv.setText("TEST");
    rl.addView(tv);
}

My XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/overallview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.robertli.hustle.MainActivity"
tools:showIn="@layout/activity_main"
android:background="#00095e">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:id="@+id/topbar"
    >

    <TextView
        android:id="@+id/app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:text="Hustle"
        android:textSize="25dp" />
</LinearLayout>

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"

    android:layout_below="@id/topbar"
    android:id="@+id/Add_new"
     android:background="#00095e"
     android:textColor="#ffffff"
     android:clickable="true"
     android:textSize="50dp" />

Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
Robert Li
  • 107
  • 1
  • 11
  • check this link - https://stackoverflow.com/questions/2305395/how-to-lay-out-views-in-relativelayout-programmatically?rq=1 – i_A_mok Aug 13 '16 at 03:12

1 Answers1

0

Suggestion : You should take a look at ListView to do this !!! ListView help you dynamically add the textview as well.

if you still want to adding directly to Relativelayout refer this example:

        RelativeLayout relativeLayout = new RelativeLayout(context);
        View view = new View(context);
        relativeLayout.addView(view);

code to dynamically add alignment in RelativeLayout

        RelativeLayout.LayoutParams viewLayoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        viewLayoutParams.addRule(RelativeLayout.BELOW, R.id.your_view);

take a look at documentation for more https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

jknair0
  • 1,194
  • 13
  • 24