0

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.

Community
  • 1
  • 1

3 Answers3

0

Try this one:

public void onClick(View v) {
    textView2 = new TextView(getApplicationContext());
    textView2.setLayoutParams(textView.getLayoutParams());
    textView2.setTextSize(textView.getTextSize());
    textView2.setTextAppearance(this, android.R.style.TextAppearance_Large);
    container.addView(textView2);
}
Gordak
  • 2,060
  • 22
  • 32
0

When you create you TextView in the OnClick method, you're not applying any of the attributes that you did in the xml layout. The new TextView will have default values for textAppearance and textSize, which is why it looks different.

SirGregg
  • 100
  • 9
0

A good practice is to define view styles in xml, for this very reason - it allows you to reuse them in different places. So I'd define the style for your TextView in res/values/style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="textViewStyle" parent="@android:style/TextAppearance.Large">
    <item name="android:textSize">50dp</item>
</style>

</resources>

Then you can change the TextView in your xml to

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:style="@style/textViewStyle"
        android:text="0"
        android:id="@+id/textView"
        android:layout_gravity="center_vertical" />

and when you add TextView programatically you can use

textView.setTextAppearance(context, R.style.textViewStyle);

That way you don't need to hold onto the old TextView. You'll be doing it the proper Android way!

roarster
  • 4,058
  • 2
  • 25
  • 38
  • Thanks this was great. Just a small note, it doesn't work if you put 'android:style' in the xml, instead you just have to put 'style'. – Harry Stanley Jun 01 '15 at 12:14