I am trying to make an android app in which you click a button and a new textview is created right next to an existing one with some preset text in the same style/size. The problem is, when the new textview is created the text appears smaller and in a different style (new text is grey, original is black). Moreover, the new textview appears misaligned in relation to the original. I tried the answer suggested here and there was a slight improvement, now the new text is aligned properly but the size is still smaller than the original. My end goal is to have it so you could keep clicking the button to add more textviews next to each other. Anyone know what the problem is?
Relevant code:
public class MainActivity extends Activity {
TextView textView;
TextView textView2;
LinearLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (LinearLayout) findViewById(R.id.linearLayout);
textView = (TextView) findViewById(R.id.textView);
}
View.OnClickListener handler2 = new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2 = new TextView(getApplicationContext());
textView2.setLayoutParams(textView.getLayoutParams());
container.addView(textView2);
}
};
}
Xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="49dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/linearLayout"
android:layout_alignParentTop="false"
android:layout_above="@+id/button">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/textView"
android:textSize="50dp"
android:layout_gravity="center_vertical" />
</LinearLayout>
</RelativeLayout>
Many thanks.