0

I am trying to make a customisation app for wooden signs. When the user clicks say on a 10 x10 size sign i want the edit text box to change size to demonstrate what they have done. Can this be done on android studio? This is my code so far, I think what i have done is changed the size of the text but it crashes when the button is clicked so i don't know. Java File:

public class Letters extends AppCompatActivity {
    int txtSize = 14;
    EditText Wood;
    Button bSize, bSize1, bSize2, bSize3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_letters);

        Button bSize = (Button) findViewById(R.id.bSize);
        Button bSize1 = (Button) findViewById(R.id.bSize1);
        Button bSize2 = (Button) findViewById(R.id.bSize2);
        Button bSize3 = (Button) findViewById(R.id.bSize3);

        bSize.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (Wood.getTextSize() == 15) {
                    txtSize--;
                    Wood.setTextSize(txtSize);
                } else {
                    txtSize += 0;
                    Wood.setTextSize(txtSize);
                }
            }
        });
        bSize1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (Wood.getTextSize() == 16) {
                    txtSize--;
                    Wood.setTextSize(txtSize);
                } else {
                    txtSize += 0;
                    Wood.setTextSize(txtSize);
                }
            }
        });

        bSize2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (Wood.getTextSize() == 17) {
                    txtSize--;
                    Wood.setTextSize(txtSize);
                } else {
                    txtSize += 0;
                    Wood.setTextSize(txtSize);
                }
            }
        });


        bSize3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (Wood.getTextSize() == 18) {
                    txtSize--;
                    Wood.setTextSize(txtSize);
                } else {
                    txtSize += 0;
                    Wood.setTextSize(txtSize);
                }
            }
        });

    }
}

xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:padding="60dp"
    android:layout_width="match_parent"
    android:weightSum="1">

    <Button
        android:id="@+id/bSize"
        android:text="15 x 10 cm "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="bSize" />

    <Button
        android:id="@+id/bSize1"
        android:text="20 x 15 cm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="bSize1" />

    <Button
        android:id="@+id/bSize2"
        android:text="30 x 7 cm"
        android:layout_width="103dp"
        android:layout_height="61dp"
        android:onClick="bSize2" />

    <Button
        android:id="@+id/bSize3"
        android:text="30 x 20 cm"
        android:layout_width="0dp"
        android:layout_marginBottom="20dp"
        android:layout_height="160dp"
        android:onClick="bSize3"
        android:layout_gravity="center_vertical" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/Wood"
        android:textSize="40dp"
        android:layout_alignTop="@+id/button3"
        android:layout_centerHorizontal="true"
        android:background="#795548"
        android:hint="Choose Letters.."/>

</LinearLayout>
09Emi
  • 25
  • 2
  • 8
  • As an aside, you should follow standard naming conventions for variables, so the variable "Wood" should begin with a lowercase letter, "wood". – chRyNaN Feb 27 '16 at 16:33

2 Answers2

1

Your application crashes probably because of a NullPointerException. This is because you have not initialized your Wood variable in the onCreate method.

What you should do is this:

Wood = (EditText) findViewById(R.id.Wood);
Button bSize = (Button) findViewById(R.id.bSize);
Button bSize1 = (Button) findViewById(R.id.bSize1);
Button bSize2 = (Button) findViewById(R.id.bSize2);
Button bSize3 = (Button) findViewById(R.id.bSize3);

When your app crashes, there is a logcat error stacktrace in Android Studio. The stacktrace shows you what exception have caused the crash. You can easily solve problems by looking at the logcat logs.

Krasimir Stoev
  • 1,734
  • 11
  • 9
  • thanks. Do you know how to change the size of the actual editText box not the text inside it, when the button is pressed. Thanks – 09Emi Feb 27 '16 at 16:42
  • try these suggestions: http://stackoverflow.com/questions/4257843/in-android-how-to-create-edittext-of-fixed-width – Krasimir Stoev Feb 27 '16 at 16:44
  • By size do you mean the width and height of the editText? – Krasimir Stoev Feb 27 '16 at 16:44
  • There are methods setWidth and setHeight if you need to change the dimensions of the EditText itself – Krasimir Stoev Feb 27 '16 at 16:47
  • @Kraimir Stoev yeah I'm wanting to change the dimensions of the setWidth and setHeight when certain buttons are pressed. – 09Emi Feb 27 '16 at 16:50
  • Well, Woods.setWidth(x) and Woods.setHeight(y) is what you can try. x and y should be determined by the button you have clicked. You can see the setWidth and setHeight methods documentation. – Krasimir Stoev Feb 27 '16 at 17:04
  • @Kraimir Stoev thank you so much it works now. Sorry but do you know when the user types into the edit text how do i show that edit text onto a new activity? Thanks – 09Emi Feb 27 '16 at 18:24
  • Maybe this would help: http://stackoverflow.com/questions/1489852/android-handle-enter-in-an-edittext :) gl gl :) – Krasimir Stoev Feb 27 '16 at 18:53
  • @Kraimir Stoev Thanks, that didn't work do you have any other ideas. thanks – 09Emi Feb 28 '16 at 17:25
  • @Kraimir Stoev I have written my code like this but it doesn't seem to work bBasket.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Wood.setImeActionLabel("Custom text", KeyEvent.KEYCODE_ENTER); Intent intent = new Intent(Bedroom.this, Basket.class); String message = Wood.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } }); – 09Emi Feb 28 '16 at 17:28
0

You never initialized your EditText which is causing NullPointerException

Add this

Wood = (EditText) findViewById(R.id.Wood);

inside onCreate method.

Update: This is how you can change the height and width of EditText on button Click

final LayoutParams lparams = new LayoutParams(150,50); // First param is Width and second is height
Wood.setLayoutParams(lparams);
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • thanks. Do you know how to change the size of the actual editText box not the text inside it, when the button is pressed. Thanks – 09Emi Feb 27 '16 at 16:48
  • Do you know how to save an edit text as an image so I can then call it onto the next page like an online basket? Thanks – 09Emi Feb 27 '16 at 18:37