Hello__ Hello__ - 5 months ago 27
Java Question

Android studio link

I am trying to create a text view link when the user clicks it they go into the register page but it doesn't work . I don't get and error or my app doesn't stop it just doesn't open up the link for me .

Login.java :

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends AppCompatActivity implements View.OnClickListener {

Button bLogin;
EditText etUsername, etPassword;
TextView tvRegisterLink;

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

etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);
bLogin = (Button) findViewById(R.id.bLogin);

bLogin.setOnClickListener(this);

}

@Override
public void onClick(View v) {

switch (v.getId()){
case R.id.bLogin:

break;

case R.id.tvRegisterLink:

startActivity(new Intent(this, Register.class));

break;
}
}
}


activity_login.xml :

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:id="@+id/etUsername"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/etPassword"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/bLogin"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Here"
android:id="@+id/tvRegisterLink"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />


Register.java :

public class Register extends AppCompatActivity implements View.OnClickListener {

Button bRegister;
EditText etUsername, etPassword;

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

etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
bRegister = (Button) findViewById(R.id.bRegister);

bRegister.setOnClickListener(this);

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bRegister:
break;
}
}
}


activity_register.xml :

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/hash"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="426dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#Hash"
android:id="@+id/textView"
android:layout_alignTop="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="135dp"
android:textSize="40sp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:id="@+id/etUsername"
android:layout_below="@+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/etPassword"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/bRegister"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

Answer

You need to define a click listener for the TextView that you want to be clicked.

In your Login activity do:

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

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink); 
    bLogin = (Button) findViewById(R.id.bLogin);

    bLogin.setOnClickListener(this);
    tvRegisterLink.setOnClickListener(this);
}
Comments