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>
Related
I am developing a simple app that has two activities, MainActivity and SecondActivity and a transparent CustomActivity that extends Dialog. The
MainActivity has two buttons (Yes_button and No_button).
When user clicks Yes_button, the SecondActivity is called via Intent and the
CustomActivity will be in front of it.
When user clicks No_button the SecondActivity will also be called but the CustomActivity will not be called alongside with it.
The calling of the CustomActivity is based on if-else statement expressions. The CustomActivity has a skip button, when clicked the CustomActivity will close only then can the SecondActivity be accessible to the user. The SecondActivity has just one button that calls the MainActivity and the cycle continues.
Problem
When the app launches and a user clicks on the No_button, the SecondActivity will be called without the CustomActivity (as expected!), but ONCE a user
clicks on the Yes_button, the SecondActivity will keep on displaying alongside the CustomActivity EVEN when the No_button is clicked.
Expectation
I want the SecondActivity to be called alongside the CustomActivity each time the Yes_button is clicked and also let only the SecondActivity be called when ever the No_button is click.
activity_main.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"
xml:toos="http://schemas.android.com/tools">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends BaseActivity implements View.OnClickListener {
private static int getNumber;
Button Yes_button;
Button No_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes_button = findViewById(R.id.button1);
No_button = findViewById(R.id.button2);
Yes_button.setOnClickListener(this);
No_button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
int get_input = 1; // will be use in if else statement.
getNumber = get_input;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
break;
case R.id.button2:
Intent intent2 = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent2);
break;
}
}
public static int get_Logic() {
return getNumber;
}
}
activity_second_activity.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"
xml:toos="http://schemas.android.com/tools">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:onClick="Click"
android:text="Click" />
</RelativeLayout>
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
private static int output;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
int received = MainActivity.get_Logic();
output = received;
Display();
}
public final void Display() {
if (output == 1) {
CustomActivity custom = new CustomActivity(SecondActivity.this);
custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
custom.show();
} else {
CustomActivity custom = new CustomActivity(SecondActivity.this);
custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
custom.cancel();
}
}
public void Click(View view) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
}
activity_custom_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dp"
android:layout_height="250dp"
xml:toos="http://schemas.android.com/tools">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:text="Skip" />
</RelativeLayout>
CustomActivity.java
class CustomActivity extends Dialog {
Button SkipButton;
private Activity main;
public CustomActivity(Activity constr) {
super(constr);
this.main = constr;
}
#Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setCancelable(false);
setContentView(R.layout.activity_custom_activity);
SkipButton = findViewById(R.id.button);
SkipButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
}
}
checking.xml
<?XML version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#FFFF" />
<corners android:radius="20dp" />
<stroke android:width="4dp" android:color="#color/colorPrimary" />
<gradient />
</shape>
</item>
</selector>
In Android, to pass data from an activity to another, you don't need to declare a static variable (getNumber in MainActivity), you should use Intent and Bundle together. See the below solution.
MainActivity.java
public class MainActivity extends BaseActivity implements View.OnClickListener {
Button Yes_button;
Button No_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes_button = findViewById(R.id.button1);
No_button = findViewById(R.id.button2);
Yes_button.setOnClickListener(this);
No_button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int input = v.getId() == R.id.button1 ? 1 : 0;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("input", input);
startActivity(intent);
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
Display();
}
public final void Display() {
int input = getIntent().getIntExtra("input", 0);
CustomActivity custom = new CustomActivity(SecondActivity.this);
custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if (input == 1) {
custom.show();
} else {
custom.cancel();
}
}
public void Click(View view) {
finish();
}
}
I have 2 buttons and 2 separate activities and I want control flow like this :
Activityone
buttonOne➡ActivityTwo
buttonTwo➡ActivityThree
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="right">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_weight="1.0">
<LinearLayout
android:layout_height="50dp"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button
android:layout_height="match_parent"
android:layout_width="50dp"
android:background="#drawable/rozmare"/>
<TextView
android:layout_height="match_parent"
android:text="کار های روزمره"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="25sp"/>
</LinearLayout>
<EditText
android:textSize="20.0sp"
android:gravity="top|right"
android:id="#+id/editText1"
android:padding="5.0dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text=""
android:capitalize="sentences"
android:layout_gravity="top|right"
android:textColor="#000000"
android:background="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="65dp"
android:orientation="vertical"
android:layout_margin="1dp">
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<Button
android:layout_height="65dp"
android:layout_width="match_parent"
android:id="#+id/notelist"
android:background="#drawable/personal"
android:layout_margin="1dp"/>
<Button
android:layout_height="65dp"
android:layout_width="match_parent"
android:background="#drawable/kasbokar"
android:layout_margin="1dp"
android:id="#+id/notelist1"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.widget.TextView;
import android.widget.EditText;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends Activity {
Button buttonOne;
Button buttonTwo;
EditText editText1;
String fileName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
getActionBar().hide();
getWindow().setSoftInputMode(3);
editText1 = (EditText) findViewById(R.id.editText1);
fileName = getResources().getString(R.string.file_name);
try {
FileInputStream fis = openFileInput(fileName);
byte[] readBytes = new byte[fis.available()];
fis.read(readBytes);
editText1.setText(new String(readBytes));
fis.close();
} catch (Exception e) {
return;
}
}
public void onPause() {
super.onPause();
try {
FileOutputStream fos = openFileOutput(fileName, 0);
fos.write(editText1.getText().toString().getBytes());
fos.close();
} catch (Exception e) {
return;
}
}
public void addListenerOnButton() {
final Context context = this;
buttonOne = (Button) findViewById(R.id.notelist);
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList.class);
startActivity(intent);
buttonTwo = (Button) findViewById(R.id.notelist1);
buttonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList1.class);
startActivity(intent);
}
});
}
});}
public void EXIT(View view)
{
finish();
}}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.whitegate.noteking"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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=".NoteList"
android:label="#string/app_name"
android:windowSoftInputMode="adjustUnspecified"/>
<activity
android:name=".NoteEdit"
android:label="#string/app_name"
android:windowSoftInputMode="adjustUnspecified"/>
<activity
android:name=".NoteList1"
android:label="#string/app_name"
android:windowSoftInputMode="adjustUnspecified"/>
<activity
android:name=".NoteEdit1"
android:label="#string/app_name"
android:windowSoftInputMode="adjustUnspecified"/>
</application>
But both buttons are doing the same action, both of them are going to activityTwo and the other issue is, If i don't touch the ButtonOne the ButtonTwo will not work.
You have added the listener of second button inside the listener of first button so
If i dont touch the ButtonOne the ButtonTwo will not work.
listener of second button only be initialize when you press the first button
but they should be separate
public void addListenerOnButton() {
final Context context = this;
buttonOne = (Button) findViewById(R.id.notelist);
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList.class);
startActivity(intent);
}
});
buttonTwo = (Button) findViewById(R.id.notelist1);
buttonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList1.class);
startActivity(intent);
}
});
}
both of them are going to activityTwo
This behavior is related to simply using the same layout structure so use settext function or make changes in XML code.
You have added second listener in first listener.
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList.class);
startActivity(intent);
buttonTwo = (Button) findViewById(R.id.notelist1);
// >>>>>>>>>>>>>>>>>>>>>>> inside buttonOne
buttonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList1.class);
startActivity(intent);
}
});
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
}); // >>>>>>>>>> button one ends here
Another easy way to declare your onClickListener is to declare it as an object field like below
private View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.notelist1: {
// go to notelist 1
break;
}
case R.id.notelist: {
// go to notelist
break;
}
}
}
};
And set listeners on buttons like below
buttonOne.setOnClickListener(clickListener);
buttonTwo.setOnClickListener(clickListener);
A solution to your issue would be to define the onClick functions in your xml
<Button
android:text="Don't have an account? Join!"
android:onClick="toRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Then define your function in your class
public void toRegister(View view){
Intent newIntent = new Intent(this, SignUpActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
LoginActivity.this.startActivity(newIntent);
}
I'm trying to create a menu using clickable images, but they don't seem to be working? I was following a tutorial and it uses something like
Intent biodata = new Intent(mainActivity.this, profile.class);
but it's not working so I tried to look for something else and someone said to use v.getContext() instad of ....this
it works on my other page but it doesn't work on this page?
mainActivity.java
package skripsi.garden;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class mainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public ImageButton buttonBio;
public void init(){
buttonBio= (ImageButton)findViewById(R.id.buttonBio);
buttonBio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent biodata = new Intent(v.getContext(), profile.class);
startActivity(biodata);
}
}
);
}
public ImageButton buttonList;
public void tombollist(){
buttonList=(ImageButton)findViewById(R.id.buttonList);
buttonList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent listTaman = new Intent(v.getContext(), gardenlist.class);
startActivity(listTaman);
}
}
);
}
public ImageButton buttonWeather;
public void tombolcuaca(){
buttonWeather=(ImageButton)findViewById(R.id.buttonWeather);
buttonWeather.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cuaca = new Intent(v.getContext(), weather.class);
startActivity(cuaca);
}
}
);
}
}
and this is the xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="#drawable/foliagemain"
tools:context="skripsi.garden.mainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#drawable/selamatdatang"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonBio"
android:src="#drawable/buttonbiodata"
android:layout_above="#+id/buttonList"
android:layout_alignRight="#+id/buttonWeather"
android:layout_alignEnd="#+id/buttonWeather" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonList"
android:src="#drawable/buttondaftartaman"
android:layout_above="#+id/buttonHelp"
android:layout_alignLeft="#+id/buttonHelp"
android:layout_alignStart="#+id/buttonHelp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonHelp"
android:src="#drawable/buttonhelp"
android:layout_above="#+id/buttonWeather"
android:layout_alignLeft="#+id/buttonBio"
android:layout_alignStart="#+id/buttonBio" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonWeather"
android:src="#drawable/buttoncuaca"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Put your buttons in your onCreate method. They do not need to be in their own method.
public class mainActivity extends AppCompatActivity {
public ImageButton buttonBio;
public ImageButton buttonList;
public ImageButton buttonWeather;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonBio= (ImageButton) findViewById(R.id.buttonBio);
buttonBio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent biodata = new Intent(mainActivity.this, profile.class);
startActivity(biodata);
}
});
buttonList=(ImageButton)findViewById(R.id.buttonList);
buttonList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent listTaman = new Intent(mainActivity.this, gardenlist.class);
startActivity(listTaman);
}
});
buttonWeather=(ImageButton)findViewById(R.id.buttonWeather);
buttonWeather.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cuaca = new Intent(mainActivity.this, weather.class);
startActivity(cuaca);
}
});
}
}
You could just call your methods init(), tombollist() and tombolcuaca() in onCreate method of your activity after setting the layout.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
tombollist();
tombolcuaca()
}
This would initialise the button to do the action written inside the onClick.
If the methods are not called, the button would not be initialised and hence the click will not work.
You could also use android:clickable="true" in your imageButtons, and then assign the click function init() to the button in your layout xml.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonBio"
android:src="#drawable/buttonbiodata"
android:layout_above="#+id/buttonList"
android:layout_alignRight="#+id/buttonWeather"
android:layout_alignEnd="#+id/buttonWeather"
android:clickable="true"
android:onClick="init"/>
Then you wouldn't need to initialise the buttons in your activity. On click of the button, the method would be called.
I am trying to open new activity in eclipse but only first button(ACKNOWLEDGMENT) work whenever im clicking on otherbutton)
they dnt work and they show error and then app closed
#MainActivity.java
public class MainActivity extends Activity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOne= (Button)findViewById(R.id.buttonACTONE);
btnOne.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getApplicationContext(),Activitytwo.class);
startActivity(intent);
Toast.makeText(v.getContext(),"opening",Toast.LENGTH_LONG).show();
}
});
Button btntwo= (Button)findViewById(R.id.buttonprologue);
btntwo.setOnClickListener(new OnClickListener()
{
public void onClick(View v1)
{
Intent intent2 = new Intent(getApplicationContext(),Activitythree.class);
startActivity(intent2);
Toast.makeText(v1.getContext(),"opening",Toast.LENGTH_LONG).show();
}
});
#AndroidManifest.xml
<activity android:name=".Activitytwo" android:label="#string/app_name"></activity>
<activity android:name=".Acitivitythree" android:label="#string/app_name"></activity>
#Activitytwo.java
public class Activitytwo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
#main.xml
<Button android:layout_height="wrap_content" android:id="#+id/buttonACTONE" android:text="ACKNOWLEDGEMENT" android:layout_width="match_parent"></Button>
<Button android:id="#+id/buttonprologue" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="PROLOGUE"></Button>
#main2.xml
<TextView
android:layout_width="fill_parent"
android:text="testing"
android:bufferType="normal"
android:layout_height="fill_parent" android:fadeScrollbars="true" android:fitsSystemWindows="true"/>
#main3.xml
<TextView
android:layout_width="fill_parent"
android:text="testing"
android:bufferType="normal"
android:layout_height="fill_parent" android:fadeScrollbars="true" android:fitsSystemWindows="true"/>
#Activitythree.java
import android.app.Activity;
import android.os.Bundle;
public class Activitythree extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
}
}
I re-created your project in Android Studio and ran without errors.
Post your logcat.
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.