1

I need to use a .java file to perform actions for a .xml layout. I have used this layout for a dialog box. I have linked the .java file to that layout but it doesn't seem to work at all.

This is the MainActivity

package com.example.ayush.projectfive;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button btn;
AlertDialog.Builder alrt;

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

    alrt = new AlertDialog.Builder(MainActivity.this);
    alrt.setIcon(R.mipmap.ic_launcher);
    alrt.setTitle("Login");
    alrt.setCancelable(false);
    alrt.setView(R.layout.mylayout);
    alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alrt.show();
            Intent i = new Intent(MainActivity.this,Second.class);
            startActivity(i);
        }
    });
}
}

This is the activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ayush.projectfive.MainActivity">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="EXIT"
    android:id="@+id/button"
    android:layout_gravity="center_horizontal" />

This is the dialog box layout

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="LOGIN"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Username"
    android:layout_marginTop="20dp"
    android:id="@+id/editText" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Password"
    android:layout_marginTop="15dp"
    android:inputType="textPassword"
    android:ems="10"
    android:id="@+id/editText2" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login"
    android:layout_marginTop="20dp"
    android:id="@+id/button3" />

This is the .java file I'd like to link with.

package com.example.ayush.projectfive;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Second extends AppCompatActivity{

EditText et, et2;
Button btn2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);
    et = (EditText) findViewById(R.id.editText);
    et2 = (EditText) findViewById(R.id.editText2);
    btn2 = (Button) findViewById(R.id.button3);
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String s1 = et.getText().toString();
            String s2 = et.getText().toString();
            if(s1.equals(s2)){
                Toast.makeText(Second.this, "Login Successful",      Toast.LENGTH_SHORT).show();
                Intent i = new Intent(Second.this,Third.class);
                startActivity(i);
            }
            else{

            }
        }
    });

}
}
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
Ayush Garg
  • 11
  • 2

2 Answers2

0

just add alrt.show(); in your MainActivity.java e.g. here:

public class MainActivity extends AppCompatActivity {

Button btn;
AlertDialog.Builder alrt;

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

    alrt = new AlertDialog.Builder(MainActivity.this);
    alrt.setIcon(R.mipmap.ic_launcher);
    alrt.setTitle("Login");
    alrt.setCancelable(false);
    alrt.setView(R.layout.mylayout);

    alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });

alrt.show();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this,Second.class);
            startActivity(i);
        }
    });
}
}
0

This is the .java file I'd like to link with [...]

public class Second extends AppCompatActivity{

Okay... That's what this code does

Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);

If you are trying to use the views that are contained within the dialog box and respond to the buttons there to login, then you need to remove the button from the activity, add alrt.show() in the onCreate instead of onClick and move the Activity start code into the dialog's onClick handlers.

It's also worth mentioning that the object is a builder, so you can do this

View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.mylayout, null);
final EditText editUsername = dialogView.findViewById(R.id.editText);
// TODO: Get other views from mylayout.xml

new AlertDialog.Builder(MainActivity.this)
    .setIcon(R.mipmap.ic_launcher)
    .setTitle("Login")
    .setCancelable(false)
    .setView(dialogView)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String username = editUsername.getText().toString();
            // TODO: Get more variables
            // TODO: Verify credentials
            Intent i = new Intent(MainActivity.this,Second.class);
            startActivity(i);
        }
    })
    .setNegativeButton("CANCEL", null)
    .show();

And, as stated earlier, remove btn.setOnClickListener

Community
  • 1
  • 1
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185