skip to Main Content

i tried to make my first converter in android studio with java but 2 functions setText and getText are not working

package com.example.unitconvertor;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity<editTextTextPersonName> extends AppCompatActivity {
    private Button button;
    private editTextTextPersonName editTextTextPersonName;
    private View editText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);

        

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "value saved", Toast.LENGTH_SHORT).show();
                String s = editText.getText().toString();
                int inch = Integer.parseInt(s);
                double cm = 2.54 * inch;
                editText.setText( "the value is" + cm);
            }
        });
    }
}

i hope that i am clear
thankyou

2

Answers


  1. The error is that your "edittext" is declared as view, correct it like this : private EditText edittext;
    then, retrieve it by id like this in your onCreate method edittext = findViewById(R.id.your_id);

    Login or Signup to reply.
  2. Try this-

    public class MainActivity extends AppCompatActivity  {
        private Button button;
        private EditText editText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
            button = findViewById(R.id.button);
            editText = findViewById(R.id.your_id);//ADD YOUR EDIT TEXT ID HERE
    
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, "value saved", Toast.LENGTH_SHORT).show();
                    String s = editText.getText().toString();
                    int inch = Integer.parseInt(s);
                    double cm = 2.54 * inch;
                    editText.setText( "the value is" + cm);
                }
            });
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search