• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Adding a view on an event

jeypeyy

Lurker
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.
 
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
If you're interested in instantiating View objects at runtime, refer to the ViewGroup and View class references.

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.
 
sorry I dont drop by here as much as I should :(

Anyways, this code should help some:
Code:
  private OnClickListener morePlayersListener = new OnClickListener() {
    public void onClick(View v) {
      addMytextField();
    }
  };

protected void addMytextField()
{
// first get a reference to the view group you want to add too
// we can cast it to LinearLayout if that's what it is
// LinearLayout is a subclass of ViewGroup

LinearLayout viewRoot =  (LinearLayout) findViewById( R.id.my_main_linear_layout_id);

// create the textview with a context
// since our activity is a "component" of our app and 
// it extends context, we can use "this"
TextView myTextView = new TextView(this);

//now construct some layout params
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
myTextView.setLayoutParams(lp);

//now add the view
viewRoot.addView( myTextView );


}
Also, it can often be easier to toggle visibilty of view already defined in the XML by using something like:

Code:
myTextField.setVisibility( View.VISIBLE );
myTextField.setVisibility( View.INVISIBLE );
myTextField.setVisibility( View.GONE );
 
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:
<?xml version="1.0" encoding="utf-8"?>
<manifest
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="jeypeyy.lol.game"
	android:versionCode="1"
	android:versionName="1.0"
>
	<uses-sdk
		android:minSdkVersion="10" />

	<application
		android:icon="@drawable/icon"
		android:label="@string/app_name"
		android:theme="@style/black"
	>
		<activity
			android:name=".AddPlayers"
			android:label="List of players"
		>
			<intent-filter>
				<action
					android:name="android.intent.action.MAIN" />
				<category
					android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity
			android:name=".Game"
			android:label="@string/app_name"

		>
		</activity>
		<activity
			android:name=".Result"
			android:label="Result"
		>
		</activity>

	</application>
</manifest>

defaultStyle.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="black" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">#000000</item>
 
    </style>
</resources>
 
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 :)
 
Back
Top Bottom