Adding a view on an event


Last Updated: 2011-07-12 02:53:24
  1. jeypeyy

    jeypeyy Member

    Hey android hackers, I need some help with a game I'm making. I'm just trying to learn android programming and improve my java skills, so it's not going to be anything big.

    To begin with there should be a screen (Activity) that lets the user decide how many players there will be. It should do this by having one text field and a button to begin with, and when the button (labeled "Add Player") is pressed there should be a new text field in between the old text field and the button. It should be possible to do this indefinitely (well, until the memory runs out).

    I don't care if this is a bad UI decision. It was the first idea I had in mind and now that I got this problem, I want to understand how to solve it. When I've solved it I want to hear about other UI ideas for this.

    I have written the XML file and there is one text field and one button to begin with, just as intended. The problem comes when I press the button and it should add a new text field. I just can't figure out how to do this.

    tl;dr: I want a new text field to appear when I click a button. How?

    [...]
    public class HurrDurr extends Activity {


    private ArrayList<Player> players;
    private Button button;
    private int defaultView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    defaultView = R.layout.main;
    setContentView(defaultView);

    button = (Button) findViewById(R.id.more_players);
    button.setOnClickListener(morePlayersListener);

    }

    private OnClickListener morePlayersListener = new OnClickListener() {
    public void onClick(View v) {
    //how do I add a text field from here?
    }
    };

    }


    //I believe this is all I need to post. If you want to see other files, tell me.

    Advertisement
  2. jeypeyy

    jeypeyy Member

    Help me please!
  3. jeypeyy

    jeypeyy Member

    Still no answer :(

    I must say the documentation isn't very clear on this. The Declaring Layout part of the Dev Guide is only reffering to using the XML file, and says that
    Then if I look at the ViewGroup and the View class references, I can't see anything about instanting them at runtime.

    I guess I have to create a new TextField object, but I have a hard time understanding the arguments in the methods. I basically want to create a text field and giving it the same properties as the button and then changing the properties of the button so it is below the new text field instead.

    First of all, what exactly is a context? I noticed Activity is an indirect subclass to Context, so I thought that maybe I could put "this" as an argument to tell the textview that it belongs to this activity. Is that correct?

    Then I want to choose the AttributeSet, and I guess I want to take them from the button, but I don't know how.

    Is there anyone around here who can help me? I really need this so I can move on.
  4. jeypeyy

    jeypeyy Member

    Is this really that hard? I got 3 replies directly on my "hey forum" message, but no answer on this yet :(
  5. alostpacket

    alostpacket Over Macho Grande? VIP Member

    sorry I dont drop by here as much as I should :(

    Anyways, this code should help some:
    Code (Text):
    1.  
    2.  
    3.  
    4.   private OnClickListener morePlayersListener = new OnClickListener() {
    5.     public void onClick(View v) {
    6.       addMytextField();
    7.     }
    8.   };
    9.  
    10. protected void addMytextField()
    11. {
    12. // first get a reference to the view group you want to add too
    13. // we can cast it to LinearLayout if that's what it is
    14. // LinearLayout is a subclass of ViewGroup
    15.  
    16. LinearLayout viewRoot =  (LinearLayout) findViewById( R.id.my_main_linear_layout_id);
    17.  
    18. // create the textview with a context
    19. // since our activity is a "component" of our app and
    20. // it extends context, we can use "this"
    21. TextView myTextView = new TextView(this);
    22.  
    23. //now construct some layout params
    24. LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
    25. myTextView.setLayoutParams(lp);
    26.  
    27. //now add the view
    28. viewRoot.addView( myTextView );
    29.  
    30.  
    31. }
    32.  
    Also, it can often be easier to toggle visibilty of view already defined in the XML by using something like:

    Code (Text):
    1. myTextField.setVisibility( View.VISIBLE );
    2. myTextField.setVisibility( View.INVISIBLE );
    3. myTextField.setVisibility( View.GONE );
  6. jeypeyy

    jeypeyy Member

    Thank you. I actually figured it out before you posted, but thanks anyways.

    I'm pretty much done with the first version of the app now, but now I just want to change the text color (for the whole app). I followed the guide here, but when I add android:theme="@style/black" to the application tag in AndroidManifest I can't run the app. It crashes at setContentView().

    AndroidManifest.xml:
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest
    3.     xmlns:android="http://schemas.android.com/apk/res/android"
    4.     package="jeypeyy.lol.game"
    5.     android:versionCode="1"
    6.     android:versionName="1.0"
    7. >
    8.     <uses-sdk
    9.         android:minSdkVersion="10" />
    10.  
    11.     <application
    12.         android:icon="@drawable/icon"
    13.         android:label="@string/app_name"
    14.         android:theme="@style/black"
    15.     >
    16.         <activity
    17.             android:name=".AddPlayers"
    18.             android:label="List of players"
    19.         >
    20.             <intent-filter>
    21.                 <action
    22.                     android:name="android.intent.action.MAIN" />
    23.                 <category
    24.                     android:name="android.intent.category.LAUNCHER" />
    25.             </intent-filter>
    26.         </activity>
    27.         <activity
    28.             android:name=".Game"
    29.             android:label="@string/app_name"
    30.  
    31.         >
    32.         </activity>
    33.         <activity
    34.             android:name=".Result"
    35.             android:label="Result"
    36.         >
    37.         </activity>
    38.  
    39.     </application>
    40. </manifest>
    defaultStyle.xml:
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3.     <style name="black" parent="@android:style/TextAppearance.Medium">
    4.         <item name="android:textColor">#000000</item>
    5.  
    6.     </style>
    7. </resources>
    8.  
  7. alostpacket

    alostpacket Over Macho Grande? VIP Member

    if you remove the styling does it stop crashing?

    That should give you an idea of where to hunt down the problem, also posting the stack trace helps :)

Share This Page