Null pointer exception on button send [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Please find below the main class
package com.example.myfirstapp;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public void addListenerOnSpinnerItemSelection() {
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
Button buttonSend;
EditText textTo;
EditText textSubject;
EditText textMessageContact;
EditText textMessageEmail;
EditText textMessageAmount;
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextSendTo);
textSubject = (EditText) findViewById(R.id.editTextName);
textMessageContact = (EditText) findViewById(R.id.editTextContact);
textMessageEmail = (EditText) findViewById(R.id.editTextEmail);
textMessageAmount = (EditText) findViewById(R.id.editTextAmount);
spinner = (Spinner) findViewById(R.id.spinner);
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email,
"Choose an Email client :"));
}
});
}
}
Also please find below the xml file
<?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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- android:background="#drawable/mmm_bg" -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dip" >
<TableRow>
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dip"
android:text="Name:"
android:textColor="#FF0000"
android:textStyle="bold" />
<EditText
android:id="#+id/editTextName"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_weight="3"
android:ems="10"
android:hint="enter the name"
android:textColor="#000000"
android:textStyle="bold" >
<requestFocus />
</EditText>
</TableRow>
<TableRow>
<TextView
android:id="#+id/textViewContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dip"
android:text="Contact:"
android:textColor="#FF0000"
android:textStyle="bold" />
<EditText
android:id="#+id/editTextContact"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_weight="3"
android:ems="10"
android:hint="enter the contact no."
android:textStyle="bold" >
<requestFocus />
</EditText>
</TableRow>
<TableRow>
<TextView
android:id="#+id/textViewEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dip"
android:text="Email: "
android:textColor="#FF0000"
android:textStyle="bold" />
<EditText
android:id="#+id/editTextEmail"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_weight="3"
android:ems="10"
android:hint="enter the email address"
android:textColorHint="#008080" >
<requestFocus />
</EditText>
</TableRow>
<TableRow>
<TextView
android:id="#+id/textViewProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dip"
android:text="Product:"
android:textColor="#FF0000"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/product_array" />
<requestFocus />
<EditText>
</EditText>
</TableRow>
<TableRow>
<TextView
android:id="#+id/textViewAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dip"
android:text="Amount:"
android:textColor="#FF0000"
android:textStyle="bold" />
<EditText
android:id="#+id/editTextAmount"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_weight="3"
android:ems="10"
android:hint="enter the amount"
android:textStyle="bold" >
<requestFocus />
</EditText>
</TableRow>
<TableRow>
<TextView
android:id="#+id/textViewSendTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dip"
android:text="Send To:"
android:textColor="#FF0000"
android:textStyle="bold" />
<EditText
android:id="#+id/editTextSendTo"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_weight="3"
android:ems="10"
android:hint="reciever&apos;s email address"
android:textStyle="bold" >
<requestFocus />
</EditText>
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/buttonSend"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Send" />
</LinearLayout>
</LinearLayout>
please find below the log cat
05-13 09:03:49.935: E/AndroidRuntime(14062): Caused by: java.lang.NullPointerException
05-13 09:03:49.935: E/AndroidRuntime(14062): at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:89)
05-13 09:03:49.935: E/AndroidRuntime(14062): at android.app.Activity.performCreate(Activity.java:5231)
05-13 09:03:49.935: E/AndroidRuntime(14062): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-13 09:03:49.935: E/AndroidRuntime(14062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-13 09:03:49.935: E/AndroidRuntime(14062): ... 11 more
05-13 09:08:50.395: I/Process(14062): Sending signal. PID: 14062 SIG: 9
I am trying to create an app which has 4 to 5 textbox and spinner and send button.
On clicking the send button the email should go to the recipient with the details entered in text boxes. but there occurs null pointer exception.
I am unable to solve the problem. Why is the NPE happening and how to resolve it .
Thanks in advance. Please help me to get through this.

A null pointer exception is very common and means you're trying to access a method or property on an object that is null. Obviously it would not be possible to call such a method (as it doesn't exist).
I suspect that either textTo or textSubject is null, because those are user defined objects. If you set a breakpoint on the line where you set the variable textTo (for example), you should notice that it is null.
When you find the value that is null, then you can figure out how to remedy the situation. If it turns out to be a variable like 'textTo', then check that the resource actually exists, or that you are casting it to the correct object

Just write your if loop after declaration of your all the views in your onCreate() as below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextSendTo);
textSubject = (EditText) findViewById(R.id.editTextName);
textMessageContact = (EditText) findViewById(R.id.editTextContact);
textMessageEmail = (EditText) findViewById(R.id.editTextEmail);
textMessageAmount = (EditText) findViewById(R.id.editTextAmount);
spinner = (Spinner) findViewById(R.id.spinner);
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email,
"Choose an Email client :"));
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

Related

OnEditorActionListener() working but cant get the text from EditText

I want to get the username from edit text and display it in the second Activity in TextView id-textView2. When I press enter after writing the name and click start button it goes to the second activity but the text is not being displayed . I tried executing other actions like startActivity() inside onEditorAction method it works after i press enter, but this code is not working
String name = nameText.getText().toString();
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
Is it because i declared two times intent, inside onEditorAction() and inside starQuizz method in Activity A. i did this because of scope issues.
Activity A
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.view.KeyEvent;
import android.widget.TextView.OnEditorActionListener;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText nameText = findViewById(R.id.nameText);
nameText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView nameText, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
String name = nameText.getText().toString();
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
handled = true;
}
return handled;
}
});
}
//OnClick of a Start button
public void startQuizz(View view){
Intent p1 = new Intent(this, qPage1.class);
startActivity(p1);
}
}
Activity A xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/beautifulcolorgradientsbackgrounds091eternalconstance"
tools:context=".MainActivity">
<EditText
android:id="#+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="181dp"
android:width="300dp"
android:hint="#string/name_input"
android:inputType="text"
android:imeOptions="actionSend"
android:imeActionId="10"/>
<Button
android:id="#+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="182dp"
android:onClick="startQuizz"
android:text="START" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:fontFamily="#font/changa_one"
android:text="Are you up for the challenge?"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="30sp" />
</RelativeLayout>
Activity 2
package com.guesstasif.guesswhat;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.view.View;
import android.widget.TextView;
public class qPage1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_q_page1);
final ProgressBar p1progress = findViewById(R.id.pg1progressBar);
Runnable r = new Runnable() {
#Override
public void run() {
int progressStatus = 0;
while (progressStatus<200){
p1progress.incrementProgressBy(1);
android.os.SystemClock.sleep(50);
progressStatus++;
}
RadioButton q1radiobutton3 = findViewById(R.id.q1radioButton3);
RadioButton q2radiobutton1 = findViewById(R.id.q2radioButton1);
Intent p2 = new Intent(qPage1.this, qPage2.class);
//name==============================================================
String name = getIntent().getStringExtra("user_name");
TextView textView2= findViewById(R.id.textView2);
textView2.setText(name);
//==================================================================
if(q1radiobutton3.isChecked() && q2radiobutton1.isChecked())
{
p2.putExtra("intVariableName", 2);
}
else if (q1radiobutton3.isChecked() || q2radiobutton1.isChecked())
{
p2.putExtra("intVariableName", 1);
}
startActivity(p2);
}
};
Thread progressThread =new Thread(r);
progressThread.start();
}
}
Activity 2 xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".qPage1">
<TextView
android:id="#+id/q1textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Q1.What is the name of the 7th planet of ou Solar system?"
android:textAlignment="center"
android:textSize="24sp" />
<RadioGroup
android:id="#+id/q1radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1textView"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<RadioButton
android:id="#+id/q1radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Jupiter" />
<RadioButton
android:id="#+id/q1radioButton2"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Saturn" />
<RadioButton
android:id="#+id/q1radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Uranus" />
<RadioButton
android:id="#+id/q1radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Neptune" />
</RadioGroup>
//second Question//
<TextView
android:id="#+id/q2textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="173dp"
android:text="Q2.What is the name of the largest tree in the world?"
android:textAlignment="center"
android:textSize="24sp" />
<RadioGroup
android:id="#+id/q2radioGroup"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/q2textView">
<RadioButton
android:id="#+id/q2radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="General Sherman" />
<RadioButton
android:id="#+id/q2radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sequoia sempervirens" />
<RadioButton
android:id="#+id/q2radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Centurion" />
<RadioButton
android:id="#+id/q2radioButton4"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_weight="1"
android:text="Coast redwood" />
</RadioGroup>
<ProgressBar
android:id="#+id/pg1progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="33dp"
android:max="200" />
<TextView
android:id="#+id/scoreView"
android:layout_width="170dp"
android:layout_height="41dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="89dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:textAlignment="center" />
</RelativeLayout>
The reason is you start a new Intent in here:
public void startQuizz(View view){
Intent p1 = new Intent(this, qPage1.class);
startActivity(p1);
}
Which does not pass the string and just starts a new Intent-Activity.
Also here:
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
You missed to start Intent so:
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
startActivity(p1);
And in the quiz method, start another Intent:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
And finally, in the second Activity:
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("user_name");
Also, you were using Thread instead of runOnUIThread(); and that might cause some UI effects.
Look at the startQuizz method - seems like you are not setting the name as an extra to the intent!
You should use a single function that starts the next activity both in the editor action and the button click listener.
It seems you are setting text of a TextView in a background thread.
The UI cannot be altered in a background thread.
Try wrapping the setText() part in runOnUiThread().
Check this out: How do we use runOnUiThread in Android?
I'll answer the most obvious question here. It seems there's a lot of code work being done, here also.
EditText edittext = (EditText) findViewById(R.id.something);
You must, and I cannot stress this enough, as of Level 30, still, you have to cast it as an EditText. In fact, you have to with everything, essentially. Java doesn't like structural propagating without being handed the stalk, not the beans (seeds). So, please one last thing:
String string = edittext.getText().toString()
That works every time in this combination!

onClickListener for a button text to change based on edit text

I have the following code but it changes the button text to empty. I'm basically trying to change the the button's text when it clicked to whatever the user types into the edit text field I have. Everything seems to work, however, when I click on either buttons I have the button's text changes to empty/null and since i'm using wrap content it shrinks the size of the button too.
activity_main.xml
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:gravity="center"
android:padding="10dp"
android:text="Name: Balkar Rana"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:textColor="#FFFF00"
android:textSize="20dp"
android:textStyle="bold"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textColor="#FF69B4"
android:text="Username: "
android:textSize="20dp"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#000000"
android:textSize="20dp"
android:id="#+id/username"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textColor="#63ea1f"
android:text="Password: "
android:textSize="20dp"/>
<EditText
android:inputType="textPassword"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#000000"
android:textSize="20dp"
android:id="#+id/password"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="Username"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#FF69B4"
android:id="#+id/userButton"
android:onClick="onClick"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:text="Password"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#63ea1f"
android:id="#+id/passButton"
android:onClick="onClick"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:text="USER-PASS"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#FF69B4"
android:id="#+id/userPassButton"
android:onClick="onClick"/>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.balkarrana.lab2;
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 MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText usernameInput = (EditText)findViewById(R.id.username);
final String userText = usernameInput.getText().toString();
final Button userButtonVariable = (Button)findViewById(R.id.userButton);
EditText passwordInput = (EditText)findViewById(R.id.username);
final String passText = passwordInput.getText().toString();
final Button passButtonVariable = (Button)findViewById(R.id.passButton);
final String userPassText = userText + passText;
final Button userPassButtonVariable = (Button)findViewById(R.id.userPassButton);
userButtonVariable.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
userButtonVariable.setText(userText);
}
});
passButtonVariable.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
passButtonVariable.setText(passText);
}
});
userPassButtonVariable.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
userPassButtonVariable.setText(userPassText);
}
});
}
}
You are getting the value of the EditText within your onCreate when the app is first started and they will be blank. You need to get the current value within the onClick and then set it. Here's how I'd do it for the userButton.
...
public class MainActivity extends AppCompatActivity{
EditText usernameInput;
Button userButtonVariable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameInput = (EditText)findViewById(R.id.username);
userButtonVariable = (Button)findViewById(R.id.userButton);
userButtonVariable.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String userText = usernameInput.getText().toString();
userButtonVariable.setText(userText);
}
});
To stop a button changing size don't set the width to be wrap_content.
Try match_parent and then use margins.
<Button
android:layout_width="match_parent"
...
</Button>

(Android)Trying to understand why my method will not compute/display the correct value in the textview

My method that is called (calculate BMI) inside of my activity is supposed to compute and display BMI (based on the formula inside the code), and display its value inside of a textview. The application runs correctly, however the value displayed is always zero. I am struggling to figure out if the problem lies in my formula (unlikely) or if the way that I am passing the value is incorrect. Any help would be appreciated. Code below
.Java file
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
public class Health extends ActionBarActivity {
EditText bloodPressure;
EditText cholesterol;
int weight;
int height;
EditText heightString;
EditText age;
TextView bmiText;
EditText weightString;
MyDBHandler dbHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_health);
weightString = (EditText) findViewById(R.id.weightTextBox);
heightString =(EditText) findViewById(R.id.heightTextBox);
bmiText = (TextView) findViewById(R.id.bmiTextView);
bloodPressure = (EditText) findViewById(R.id.bloodPressureTextBox);
cholesterol = (EditText) findViewById(R.id.cholesterolTextBox);
age = (EditText) findViewById(R.id.ageTextBox);
dbHandler = new MyDBHandler(this, null, null, 1);
}
public void calculateBMI(View view)
{
weight = Integer.parseInt(weightString.getText().toString());
height = Integer.parseInt(heightString.getText().toString());
int BMI = 703 * (weight/(height*height));
bmiText.setText("Your calculated BMI is: " + Integer.toString(BMI));
Toast.makeText(Health.this, " Info Saved ", Toast.LENGTH_SHORT).show();
HealthInfo healthInfo = new HealthInfo(bloodPressure.getText().toString(),cholesterol.getText().toString(),heightString.getText().toString(),weightString.getText().toString(),age.getText().toString());
dbHandler.addHealth(healthInfo);
}
public void calorieClick(View view){
Intent i = new Intent(this, CalorieTracker.class);
startActivity(i);
}
public void openTips(View view){
Intent i = new Intent(this, HealthTipsActivity.class);
startActivity(i);
}
public void openPlans(View view){
Intent i = new Intent(this, DietPlansActivity.class);
startActivity(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_health, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
.XML file
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="seniorproject.broncos.broncowellnessapp.Health"
android:background="#006699">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/blood_pressure_text"
android:id="#+id/bloodPressureTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/cholesterol_text"
android:id="#+id/cholesterolTextView"
android:layout_below="#+id/bloodPressureTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/weight_text"
android:id="#+id/weightTextView"
android:layout_below="#+id/cholesterolTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/height_text"
android:id="#+id/heightTextView"
android:layout_below="#+id/weightTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/age_text"
android:id="#+id/ageTextView"
android:layout_below="#+id/heightTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:width="150dp"
android:id="#+id/bloodPressureTextBox"
android:layout_below="#+id/bloodPressureTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:width="150dp"
android:id="#+id/cholesterolTextBox"
android:layout_below="#+id/cholesterolTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:width="150dp"
android:id="#+id/weightTextBox"
android:layout_below="#+id/weightTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:width="150dp"
android:id="#+id/heightTextBox"
android:layout_below="#+id/heightTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:width="150dp"
android:id="#+id/ageTextBox"
android:layout_below="#+id/ageTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/diet_plans_button_text"
android:onClick="openPlans"
android:id="#+id/dietPlansButton"
android:layout_below="#+id/ageTextBox"
android:layout_marginTop="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/calorie_tracker_button"
android:id="#+id/calorieAccessButton"
android:layout_below="#+id/dietPlansButton"
android:onClick ="calorieClick"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/tips_button_text"
android:id="#+id/tipsButton"
android:onClick="openTips"
android:layout_below="#+id/calorieAccessButton"
android:layout_alignRight="#+id/calorieAccessButton"
android:layout_alignEnd="#+id/calorieAccessButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BMI_button_text"
android:id="#+id/bmiButton"
android:onClick="calculateBMI"
android:layout_alignBottom="#+id/cholesterolTextBox"
android:layout_alignRight="#+id/dietPlansButton"
android:layout_alignEnd="#+id/dietPlansButton"
android:layout_marginRight="36dp"
android:layout_marginEnd="36dp"
android:background="#ff00b812" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Push Button to Compute BMI"
android:id="#+id/bmiTextView"
android:layout_alignBottom="#+id/weightTextBox"
android:layout_alignLeft="#+id/bmiButton"
android:layout_alignStart="#+id/bmiButton" />
</RelativeLayout>
Your weight and height variables are int. That is your problem. Change them to float or double
Currently, you are doing integer division which is causing the result to be truncated as follows: 1 / 2 gives 0
You are doing a division for integer which results mostly to zero.
That is why the result is always zero.
Change your weight and height variables to double like this
and just cast the result back to int.
double weight = 0.0;
double height = 0.0;
int BMI = (int)(703 * (weight/(height*height)));
bmiText.setText("Your calculated BMI is: " + Integer.toString(BMI));
Place this on the end of the onCreate() method:
findViewById(R.id.bmiButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
weight = Integer.parseInt(weightString.getText().toString());
height = Integer.parseInt(heightString.getText().toString());
int BMI = 703 * (weight/(height*height));
bmiText.setText("Your calculated BMI is: " + Integer.toString(BMI));
Toast.makeText(Health.this, " Info Saved ", Toast.LENGTH_SHORT).show();
HealthInfo healthInfo = new HealthInfo(bloodPressure.getText().toString(),cholesterol.getText().toString(),heightString.getText().toString(),weightString.getText().toString(),age.getText().toString());
dbHandler.addHealth(healthInfo);
}
});
Some android version does not support onClick attribute for Button tag in XAML.

Making android EditText vertical scrollable

I am new to Android development and I have an issue with making Android EditText vertically scroll able. The vertical scrolling of the EditText does not work for me. I followed several posts and resources but none of them worked for me.
I am using the following code sample to enable the vertical scroll ability.
this.detailsEditText.setScroller(new Scroller(this));
this.detailsEditText.setMaxLines(1);
this.detailsEditText.setVerticalScrollBarEnabled(true);
this.detailsEditText.setMovementMethod(new ScrollingMovementMethod());
The following are the activity class and layout resource file, respectively I am using in my app.
CreateAppointmentActivity.java
package lk.iit.appointmentmanagerapp.activities;
import java.sql.Date;
import java.sql.Time;
import lk.iit.appointmentmanagerapp.data.Appointment;
import lk.iit.appointmentmanagerapp.data.DatabaseHandler;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Scroller;
public class CreateAppointmentActivity extends Activity {
private EditText titleEditText;
private EditText timeEditText;
private EditText detailsEditText;
private EditText dateEditText;
private Button saveButton;
private Button resetButton;
private final DatabaseHandler handler = new DatabaseHandler(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_appointment);
this.titleEditText = (EditText)(this.findViewById(R.id.edit_title));
this.timeEditText = (EditText)(this.findViewById(R.id.edit_time));
this.detailsEditText = (EditText)(this.findViewById(R.id.edit_details));
this.dateEditText = (EditText)(this.findViewById(R.id.edit_date));
this.detailsEditText.setScroller(new Scroller(this));
this.detailsEditText.setMaxLines(1);
this.detailsEditText.setVerticalScrollBarEnabled(true);
this.detailsEditText.setMovementMethod(new ScrollingMovementMethod());
SharedPreferences preferences = getSharedPreferences("date_variables", MODE_PRIVATE);
this.dateEditText.setText(preferences.getInt("Year", 0) + "-" + preferences.getInt("Month", 0) + "-" + preferences.getInt("Day", 0));
this.dateEditText.setEnabled(false);
this.saveButton = (Button)(this.findViewById(R.id.save_new_appointment_button));
this.saveButton.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("deprecation")
#Override
public void onClick(View v) {
String[] dateComponents = dateEditText.getText().toString().split("-");
String[] timeComponents = timeEditText.getText().toString().split(":");
Appointment appointment = new Appointment();
appointment.setAppointment_title(titleEditText.getText().toString());
appointment.setAppointment_date(new Date(Integer.parseInt(dateComponents[0]), Integer.parseInt(dateComponents[1]), Integer.parseInt(dateComponents[2])));
appointment.setAppointment_time(new Time(Integer.parseInt(timeComponents[0]), Integer.parseInt(timeComponents[1]), Integer.parseInt(timeComponents[2])));
appointment.setAppointment_details(detailsEditText.getText().toString());
boolean inserted = handler.addAppointment(appointment);
//
//
}
});
this.resetButton = (Button)(this.findViewById(R.id.reset_button));
this.resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
titleEditText.setText("");
timeEditText.setText("");
detailsEditText.setText("");
dateEditText.setText("");
}
});
}
}
activity_create_appointment.xml
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/background"
tools:context="lk.iit.appointmentmanagerapp.activities.CreateAppointmentActivity" >
<TextView
android:id="#+id/create_activity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome_create"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dip"
android:layout_marginBottom="25dip"
android:textSize="18.5sp"
android:textColor="#ffffff" />
<TextView
android:id="#+id/title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/create_activity_title"
android:layout_marginTop="15dip"
android:text="#string/title_textview_text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/edit_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/create_activity_title"
android:layout_marginTop="15dip"
android:layout_toRightOf="#+id/title_textview"
android:layout_marginLeft="30dip"
android:background="#ffffff"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/time_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_textview"
android:layout_marginTop="25dip"
android:text="#string/time_textview_text"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText
android:id="#+id/edit_time"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/time_textview"
android:layout_below="#+id/edit_title"
android:layout_marginLeft="25dip"
android:layout_marginTop="25dip"
android:ems="10"
android:inputType="time" />
<TextView
android:id="#+id/details_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/time_textview"
android:text="#string/details_textview_text"
android:layout_marginTop="25dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/edit_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/details_textview"
android:layout_alignBottom="#+id/details_textview"
android:layout_alignLeft="#+id/edit_title"
android:background="#ffffff"
android:ems="10"
android:inputType="text" >
</EditText>
<TextView
android:id="#+id/date_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/details_textview"
android:text="Date"
android:layout_marginTop="25dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/edit_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/date_textview"
android:layout_below="#+id/edit_details"
android:background="#ffffff"
android:layout_marginLeft="25dip"
android:layout_marginTop="25dip"
android:ems="10"
android:inputType="date" >
</EditText>
<Button
android:id="#+id/save_new_appointment_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/date_textview"
android:layout_marginTop="15dip"
android:layout_marginBottom="10dip"
android:layout_centerHorizontal="true"
android:text="#string/save_button_text"
android:textAllCaps="false" />
<Button
android:id="#+id/reset_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/save_new_appointment_button"
android:layout_marginTop="2dip"
android:layout_centerHorizontal="true"
android:text="#string/reset_button_text"
android:textAllCaps="false" />
</RelativeLayout>
Please bear with me if I have done any mistakes as I am relatively new to this area of development.
I would be grateful if someone help me out with this issue.
Try this in java code:
EditText dwEdit = (EditText) findViewById(R.id.DwEdit);
dwEdit.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if (view.getId() ==R.id.DwEdit) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
And in your xml:
<EditText
android:id="#+id/DwEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="10"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:overScrollMode="always"
android:inputType="textCapSentences">
</EditText>
If you wanna scroll editText only in detailEditText then you specify height of editText 50dp or your need and remove inputType="text" from your editText ....it fulfill your need..
and you don't need any code in java relative to that editText..

When user logs in it navigates to home page

I'm just wondering if anyone can help me. I've created a login page which requests a user to enter their username and password. Once the details are correct it should move to my home page? I'm just wondering how I would do this. Thank you in advance
Here is my login xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#808080"
android:orientation="vertical" >
<!-- Login Title -->
<TextView
android:layout_width="222dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="#string/login_title"
android:textColor="#FFFF00"
android:textSize="24sp" />
<!-- Login Image -->
<!-- layout for Username -->
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="-75dp"
android:src="#drawable/logov2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="60dp"
android:gravity="center_horizontal"
android:text="#string/pleaselogin"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:layout_weight="0.83"
android:text="#string/username"
android:textColor="#FFFF00"
android:textSize="24sp" />
<EditText
android:id="#+id/username1"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="#FFFFFF"
android:ems="10"
android:inputType="text"
android:textSize="24sp" >
<requestFocus />
</EditText>
</LinearLayout>
<!-- layout for Password -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="67dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_weight="1.0"
android:text="#string/password"
android:textColor="#FFFF00"
android:textSize="24sp" />
<EditText
android:id="#+id/password1"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="#FFFFFF"
android:inputType="textPassword" />
</LinearLayout>
<Button
android:id="#+id/login1"
android:layout_width="150sp"
android:layout_height="50dp"
android:layout_marginLeft="200dp"
android:background="#FF0000"
android:text="#string/login"
android:textColor="#FFFFFF"
android:textSize="28sp" />
</LinearLayout>
Login class
package com.example.workplease;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.workplease_v2.R;
public class LoginActivity extends Activity {
String userName, passWord;
EditText username, password;
Button login;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// UI elements gets bind in form of Java Objects
username = (EditText)findViewById(R.id.username1);
password = (EditText)findViewById(R.id.password1);
login = (Button)findViewById(R.id.login1);
// now we have got the handle over the UI widgets
// setting listener on Login Button
// i.e. OnClick Event
login.setOnClickListener(loginListener);
}
private OnClickListener loginListener = new OnClickListener() {
public void onClick(View v) {
//getting inputs from user and performing data operations
if(username.getText().toString().equals("test") &&
password.getText().toString().equals("test")){
// responding to the User inputs
Toast.makeText(getApplicationContext(), "Login Successfully !!!", Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "Login Not Successful !!!", Toast.LENGTH_LONG).show();
}
};
}
Home page XML
<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:background="#808080"
android:orientation="vertical"
tools:context=".Home" >
<TextView
android:id="#+id/title"
android:layout_width="220dp"
android:layout_height="100dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="#string/description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFF00"
android:textSize="28sp" />
<ImageButton
android:id="#+id/exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="40dp"
android:src="#drawable/exercisesv2"
android:background="#null" />
<ImageButton
android:id="#+id/routines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="22dp"
android:src="#drawable/routines"
android:background="#null" />
<ImageButton
android:id="#+id/logbook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="22dp"
android:src="#drawable/logbook"
android:background="#null" />
<ImageButton
android:id="#+id/qrscanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="22dp"
android:src="#drawable/qrscanner1"
android:background="#null" />
<TextView
android:id="#+id/Copyright"
android:layout_width="206dp"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="70dp"
android:text="#string/copyright"
android:textColor="#FFFF00"
android:textSize="14sp"
android:background="#null" />
<ImageView
android:id="#+id/Logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="-550dp"
android:src="#drawable/logov2" />
</LinearLayout>
Home class
package com.example.workplease;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import com.example.workplease_v2.R;
public class Home extends Activity {
ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
AddListenerOnButton();
AddListenerOnButton1();
}
public void AddListenerOnButton(){
final Context context = this;
imageButton = (ImageButton) findViewById(R.id.exercise);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Exercises.class);
startActivity(intent);
}
});
}
public void AddListenerOnButton1(){
final Context context = this;
imageButton = (ImageButton) findViewById(R.id.routines);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Routines.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
To start a new Activity you call startActivity(Intent intent)
Intent intent = new Intent(this, Home.class);
startActivity(intent);
So your loginListener button OnClickListener would perform this action:
public void onClick(View v) {
if(username.getText().toString().equals("test") &&
password.getText().toString().equals("test")){
// responding to the User inputs
Toast.makeText(getApplicationContext(), "Login Successfully !!!",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this, Home.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "Login Not Successful !!!",
Toast.LENGTH_LONG).show();
}
}

Categories