Search code examples
javaandroidandroid-widgetandroid-relativelayout

Adding TextViews from Java code


I'm trying to learn Android programming by learning Java and Android Studio and I make programs for applying what I learn. Anyway, trying to create a simple word game, I ran into problems. I expected my for loop in lines 27-35 to display 5 dashes on the screen, but it only displays one. I have previously used Visual Studio and well, it was simpler there. what am I missing? I'm not sure if the program creates five textviews and place them in one place or it only creates one.

Here's my MainActivity.java:

package com.mycompany.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.graphics.Color;
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //The GUI related codes and definitions are here
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainGame meGuess = new MainGame();
        final String dashForTextFields = "-";
        meGuess.setGuessingWord();
        RelativeLayout mainGameLayout = (RelativeLayout) findViewById(R.id.myLayout);
        RelativeLayout.LayoutParams myTextContainer = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        TextView[] myTextViews = new TextView[meGuess.getGuessingWordLength()];
        for(int i = 0; i < meGuess.getGuessingWordLength(); i++)
        {
            myTextViews[i] = new TextView(this);
            myTextViews[i].setText(dashForTextFields);
            myTextViews[i].setTextSize(100);
            myTextContainer.leftMargin = 10 * i;
            myTextContainer.topMargin = 50;
            mainGameLayout.addView(myTextViews[i], myTextContainer);
        }
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
 and here's MainGame class:
package com.mycompany.myapplication;
public class MainGame
{

        private String wordToBeGuessed;
        public String getGuessingWord()
        {
            return wordToBeGuessed;
        }
        public void setGuessingWord()
        {
            //IMPORTANT NOTE: This method should be updated//
            wordToBeGuessed = "apple";
        }
        public int getGuessingWordLength()
        {
            return wordToBeGuessed.length();
        }
}

Solution

  • I think your problem lies in the fact that you reuse the same myTextContainer for all text views. If you put these lines:

    RelativeLayout.LayoutParams myTextContainer = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    

    inside the for-loop, you should be fine.

    In any case, this is a typical example of where you should use a Linear layout (with horizontal orientation) instead of a Relative layout. And Android Studio also has a Layout Editor - it is typically easier to draw interfaces than to program them.