Im building an application that requires when you press the button I.E. (search) then it opens the search page, click the (home) button then it goes back to the home view. what I'm missing is the knowledge to make that connection between those two views. this is what the home page looks like in XML.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
//Title
//Search Student Button
<Button
android:id="#+id/button1"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Search Student" />
//New Student Button
<Button
android:id="#+id/button2"
android:layout_width="99dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="New Studetn " />
//Legal Button
<Button
android:id="#+id/button3"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Legal Info" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="Student Registration "
android:textAppearance="?android:attr/textAppearanceLarge" />
some pictures of the application.
http://imgur.com/aVpqUCZ
Button searchStudentButton;
Button newStudentButton;
Button legalButton;
searchStudentButton = (Button) findViewById(R.id.button1);
searchStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, searchStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
newStudentButton = (Button) findViewById(R.id.button2);
newStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, newStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
legalButton = (Button) findViewById(R.id.button3);
legalButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, legalActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
Don't forget to add your new activity in the AndroidManifest.xml:
<activity android:label="#string/app_name" android:name="searchStudentActivity"/>
<activity android:label="#string/app_name" android:name="newStudentActivity"/>
<activity android:label="#string/app_name" android:name="legalActivity"/>
When dealing with multiple views and/or buttons like you are, I usually prefer using only one instance of onClickListener for all the views, to keep the code cleaner.
public class MainActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button btnSearchStudent = (Button) findViewById(R.id.button1);
Button btnNewStudent = (Button) findViewById(R.id.button2);
Button btnLegalInfo = (Button) findViewById(R.id.button3);
btnSearchStudent.setOnClickListener(this);
btnNewStudent.setOnClickListener(this);
btnLegalInfo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1: {
Intent intent = new Intent(this, SearchStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button2: {
Intent intent = new Intent(this, NewStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button3: {
Intent intent = new Intent(this, LegalInfoActivity.class);
startActivity(intent);
break;
}
}
}
}
I would recommend that you change the android:id attribute of your buttons to a more meaningful name. This makes it easier to see what you're referencing in the code. I personally prefer prepending my views with an abreviation of the view class such as btn_ for Button and tv_ for TextView. Remember to update your calls to findViewById() and the id's used in the switch statement.
Finally, don't forget to add your activities to the AndroidManifest.xml file as Sagar Pilkhwal posted, if you haven't already.
Related
Only on 2nd click the button loads the new activity.
Usually this problem happens often when I define an onCLick() method in the .xml and also have it in the activity. This is here not the case.
<Button
android:id="#+id/settingsBtn"
android:layout_width="192dp"
android:layout_height="60dp"
android:layout_above="#+id/exitBtn"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_gravity="end|bottom"
android:layout_marginStart="-2dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="24dp"
android:background="#android:color/black"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="#string/settings"
android:textColor="#android:color/white"
android:textSize="24sp" />
private Button mSettingsButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
.
connectUiElements();
setUpUiListeners();
}
.
.
.
private void connectUiElements() {
mSettingsButton = (Button) findViewById(R.id.settingsBtn);
}
private void setUpUiListeners() {
mSettingsButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
SettingsActivity.class);
startActivity(intent);
}
}
);
}
Usually I would expect, that the buttons works when I click once.
i have the edit view and button in main activity. Here is the button code:
<Button
android:id="#+id/button"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="#{handler::onButtonClick}"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
how can i change activity in onButtonClick method?
<Button
android:id="#+id/button"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="onButtonClick"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
And add this function to your java code:
public void onButtonClick(View view) {
Intent intent = new Intent(context, YourActivityClass.class);
context.startActivity(intent);
}
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here you can edit the view and change activity
// Change Activity using Intent
startActivity(new Intent(CurrentActivity.this, SecondActivity.class));
}
});
}
Hope this answer helps you.
In your main Activity
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getContext(),YOURACTIVITYNAME.class);
startActivity(intent);
}
});
I was working on a different code site before Android studio, and how they made buttons in a second activity open was different than here. So far I have my second activity button and it opens..
My fifthactivity.java is below
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
I understand I need to make a new .java and a new layout to direct the button, I just need help with the code to put into my fifth activity.java
Below is my layout for the other button that i need to open.
<Button
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PlayStation"
android:drawableLeft="#drawable/playstation"
android:drawableStart="#drawable/playstation"
android:layout_weight="0.07"
android:textSize="35sp"
android:id="#+id/button5" />
Button button;
Button anotherButton; // the second button OP required
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
anotherButton = (Button)findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(FifthActivity.this, AnotherActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
}
}
Add new Button to your xml file and style it how do you like and add new id like
android:id="#+id/button6" :
<Button
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PlayStation"
android:drawableLeft="#drawable/playstation"
android:drawableStart="#drawable/playstation"
android:layout_weight="0.07"
android:textSize="35sp"
android:id="#+id/button5" />
<Button
android:id="#+id/button6"
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SomeText"
android:textSize="35sp" />
In your fifthactivity.java add new Button:
Button button, button2;
and, like your previous button, add click listener to that button. Create new Java class with its own layout and open it thru Intent with that button.
I am a completely new programmer and I am looking to make a sort of quiz app as my first app. Just Questions with the right answer that will send the user to the next activity. I figured out how to edit buttons but I am not sure what to add to the JAVA file or XML file that will allow the next activity (screen) to be opened up.
This is my Layout so far. I have decided the password input but the answer to the question will be stored within the app. Not sure if this is the right approach?
`
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="Answer" />
`
Thanks in advance for any help rendered!
#kanwaljit Sngh
I am getting multiple errors like "button cannot be resolved to a type" and "R cannot be resolved to a variable" What does these mean?
`import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Startscreen extends Activity {
EditText editText1 = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String answer = editText1.getText().toString().trim();
if (answer.equals("desired answer")) {
Intent i = new Intent(getApplicationContext(),
CorrectAnswerActivity.class);
startActivity(i);
} else {
Intent i = new Intent(getApplicationContext(),
WrongAnswerActivity.class);
startActivity(i);
}
}
});
`
Get the value of edittext
check it with desired answer
If true, redirect to next activity
Eg :
if(editText1.getText().toString().equalsIgnoreCase("desiredanswer")
{
startActivity(new Intent(this,nextActivity.class));
}
else
{
Toast.makeText(this,"Wrong answer",2000).show();
}
EditText editText1 = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String answer = editText1.getText().toString().trim();
if (answer.equals("desired answer")) {
Intent i = new Intent(getApplicationContext(),
CorrectAnswerActivity.class);
startActivity(i);
} else {
Intent i = new Intent(getApplicationContext(),
WrongAnswerActivity.class);
startActivity(i);
}
}
});
and write new two activity CorrectAnswerActivty and WrongAnswerActivity
and do actions what to display in these activties
and add the following line to the xml file in button tag-
android:id="#+id/button"
MainActivity
package com.Kevious.Kevin;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
import android.opengl.*;
import android.content.*;
import android.text.*;
import java.net.*;
public class MainActivity extends Activity
{
String passw;
String value = "Test";
int i = 1;
TextView textView;
Button button;
EditText editText;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View p1)
{
loadMethod();
}
}
);
editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher(){
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
}
public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
passw = editText.getText().toString();
textView.setText(passw);
passw = textView.getText().toString();
}
public void afterTextChanged(Editable p1)
{
}
}
);
}
public void loadMethod()
{
passwCheck();
}
public void passwCheck(){
passw = textView.getText().toString();
if (passw.equals("kev"))
{
Toast.makeText(this, "corrrect " + passw, Toast.LENGTH_SHORT).show();
setContentView(R.layout.nextactivity);
} else {
Toast.makeText(this, "incorrect " + passw, Toast.LENGTH_SHORT).show();
}
}
}
NextActivity
package com.Kevious.Kevin;
import android.os.*;
import android.content.*;
import android.app.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
public class NextActivity extends Activity
{
Button button2;
TextView textView2;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText("Welcome Kevin");
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener(){
public void onClick(View p1)
{
textView2.setText("button clicked");
buttonclick2();
}
}
);
}
public void buttonclick2(){
Toast.makeText(this, "logged out", Toast.LENGTH_SHORT).show();
setContentView(R.layout.main);
}
}
Im new to stackoverflow, so I hope I am creating this correctly. I apologize if I did it wrong.
I am learning Android Development and just experimenting and creating my first app and somehow I encountered a problem where one of my buttons do not work, even when I have added the button listeners correctly.
When I type "kev" as the password on the main layout and press the button, it will take me to NextActivity Layout via the "setContentView(.....)" this works fine.
The problem is, when I am in the NextActivity Screen, it will have a button. Somehow when I click on that button, it does nothing. I am sure the code is right. Can someone help me out here?
Thanks
setContentView() doesn't start a new Activity. To do that, run something like this:
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
And run setContentView in the new Activity, and set any onClickListeners there.
Other than the answers posted. You are trying to initialize views without setting the content to the activity in NextActivity. You can findviewbyid of the views of the current view hierarchy set to the activity.
If you need to navigate from one activity to another say on button click
startActivity(new Intent(ActivityName.this,NextActivity.class); or
startActiivty(new Intent("packagename.NextActivity);
setContentView. You have misunderstood the purpose of setContentView.
http://developer.android.com/reference/android/app/Activity.html#setContentView(int)
Set the activity content to an explicit view.
This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.
Parameters
view The desired content to display.
In your NextActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// this should be first not in buttonclick2()
textView2 = (TextView) findViewById(R.id.textView2);
// then initialize views.
...
}
Example:
public class MainActivity extends Activity
{
String passw;
TextView textView;
Button button;
EditText editText;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View p1)
{
passw = editText.getText().toString();
if (passw.equals("kev"))
{
Toast.makeText(MainActivity.this, "corrrect " + passw, Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,NextActivity.class));
} else {
Toast.makeText(MainActivity.this, "incorrect " + passw, Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.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"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_alignParentTop="true"
android:layout_marginLeft="83dp"
android:text="Password" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentBottom="true"
android:layout_marginLeft="39dp"
android:text="Button" />
</RelativeLayout>
NextActivity.java
public class NextActivity extends Activity
{
Button button2;
TextView textView2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
textView2 = (TextView) findViewById(R.id.textView1);
textView2.setText("Welcome Kevin");
button2 = (Button) findViewById(R.id.button1);
button2.setOnClickListener(new OnClickListener(){
public void onClick(View p1)
{
textView2.setText("button clicked");
}
});
}
}
next.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentBottom="true"
android:text="Button" />
</RelativeLayout>
Manifest.xml
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.NextActivity"
android:label="#string/app_name" >
</activity>
Actually you are not navigating to the NextActivity. You are in the MainActivity itself, you have just changed the layout dynamically. For navigating to the NextActivity use,
startActivity(new Intent(MainActivity.this,NextActivity.class));
And in the onCreate() method of the NextActivity set your layout and try what you were trying to do.
Calling setContentView() multiple times in the same Activity is not recommended. See: Android: switching screens with new activity or just changing content view.
Also, you need to start NextActivity by something like:
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
Right now, you're just changing the UI without adding the functionality of the Activity.
if (passw.equals("kev"))
{
Toast.makeText(this, "corrrect " + passw, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
} else {
Toast.makeText(this, "incorrect " + passw, Toast.LENGTH_SHORT).show();
}
}
Would you like to put something towards ur next activity use
Intent intent = new Intent(getApplicationContext(), NextActivity.class);
intent.putExtra("your_key", your_value);
intent.putExtra("your_next_key", your_next_value);
startActivity(intent);
AND DONT FORGET TO REGISTER BOTH YOUR ACTIVITIES IN YOUR MANIFEST! Make sure it is spelled like ur .java classes (case sensitive!)
AndroidManifest.xlm
<application
android:icon="#drawable/logo"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NextActivity" />
</application>