Reputation: 6522
I have a Spinner, EditText and a Button. When I enter something in EditText and then click on a Button, I want it to create a new TextView according to which item on spinner is selected.
Button click method:
public void submitScore(View v){
final int position = playerList.getSelectedItemPosition();
EditText input = (EditText) findViewById(R.id.editText1);
createNewTextView (players.get(position) + input);
}
createNewTextView():
private TextView createNewTextView (String text){
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
newTextView.setLayoutParams(lparams);
newTextView.setText(text);
return newTextView;
}
Whole XML code:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Igra" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:orientation="vertical" >
</LinearLayout>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_toLeftOf="@+id/button1"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:text="Enter"
android:onClick="submitScore" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="Score:" />
As you can see; I have both Relative and Linear Layout inside this activity and they are separated (LinearLayout is under the RelativeLayout).
I want this TextView to be added into LinearLayout but it isn't added anywhere, I can't see it when I press a button.
My code now:
public void submitScore(View v){
LinearLayout lLayout = (LinearLayout) findViewById (R.id.linearLayout);
final int position = playerList.getSelectedItemPosition();
EditText input = (EditText) findViewById(R.id.editText1);
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
newTextView.setLayoutParams(lparams);
newTextView.setText(players.get(position) + input);
//createNewTextView (players.get(position) + input);
lLayout.addView(newTextView);
}
Result:
Upvotes: 0
Views: 845
Reputation: 15533
You don't see any textView that you have created because you didn't add any of them to your layout. (As far as I see from information yo gave)
Give an id to your linearLayout and in your submitScore() method get your layout by id then add your editText to your layout that you've created by createNewTextView() method.
<LinearLayout
android:id="@+id/myTextViewContainer"
...
>
</LinearLayout>
public void submitScore(View v){
....
TextView newTextView = createNewTextView (players.get(position) + input);
LinearLayout myTextViewContainer=(LinearLayout)findViewById(R.id.myTextViewContainer);
myTextViewContainer.addView(newTextView);
}
Upvotes: 0
Reputation: 4651
Why don't you make a textview from xml and make it invisible
textView.setVisibility(View.INVISIBLE);
then when you select something it becomes visible with the text wanted ..
private TextView createNewTextView (String text){
textView.setVisibility(View.VISIBLE);
textview.setText(text);
}
Upvotes: 0
Reputation: 221
On submitScore()
you are creating a TextView
but isn't adding it to the LinearLayout
.
Give you LinearLayout
an android:id
value then, on submitScore()
, get its reference and call the add()
method of the LinearLayout
to add the value returned from createNewTextView ()
Upvotes: 1