-2

in this simple Application of my first program in Android i want to get username and password from User. but after click on button that return NULL and thats incorrect, username and password fields have string and are not NULL. Textusername and Textpassword in this code are NULL and can not get string from R.id.username AND R.id.password

My XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

        <TextView
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="@string/EnterUsername"
                />
        <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:id="@+id/username"
                />
    </LinearLayout>
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

        <TextView
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="@string/EnterPassword"
                />
        <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:id="@+id/password"
                />
    </LinearLayout>
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            />
    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/submitButton"
            android:text="@string/SubmitButton"
            android:gravity="center"
            android:background="@drawable/purple"/>
</LinearLayout>

My Code:

package com.example.AndroidMultiPage;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

        final EditText username = (EditText) findViewById(R.id.username);
        final EditText password = (EditText) findViewById(R.id.password);

        Button   submit   = (Button)   findViewById(R.id.submitButton);

        final String Textusername = username.getText().toString();
        final String Textpassword = password.getText().toString();

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(
                         MyActivity.this, Textusername, 
                         Toast.LENGTH_SHORT).show();

                //TOAST SHOW NULL
            }
        });
    }
}
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
DolDurma
  • 15,753
  • 51
  • 198
  • 377

6 Answers6

2

You should move

 final String Textusername = username.getText().toString();
 final String Textpassword = password.getText().toString();

under onClick(..) of submit Button and inside onClick(..) you should check String values are NULL or not. as @Raghunandan said...

M D
  • 47,665
  • 9
  • 93
  • 114
1

Move this

String Textusername = username.getText().toString();
String Textpassword = password.getText().toString();

inside onClick.

ALso check

if(!TextUtils.isEmpty(Textusername))
{
           // display Toast 
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

Try below code:

submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final String Textusername = username.getText().toString();
        final String Textpassword = password.getText().toString();

        Toast.makeText(MyActivity.this, Textusername, Toast.LENGTH_SHORT).show();    
    }
});
mbm29414
  • 11,558
  • 6
  • 56
  • 87
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
1

Please move this

final String Textusername = username.getText().toString(); final String Textpassword = password.getText().toString();

inside onClick().

Aniruddha
  • 4,477
  • 2
  • 21
  • 39
1
submit.setOnClickListener(new OnClickListener() {


     @Override
                public void onClick(View view) {

                   String Textusername = username.getText().toString();

                    Toast.makeText(MyActivity.this, Textusername, Toast.LENGTH_SHORT).show();

                    //TOAST SHOW NULL
                }
            });
DolDurma
  • 15,753
  • 51
  • 198
  • 377
Android Man
  • 319
  • 2
  • 6
  • 22
0
Use this:
 submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
String text_user=username.getText().toString();
                Toast.makeText(
                         MyActivity.this, text_user, 
                         Toast.LENGTH_SHORT).show();

                            }
        });