Android Studio - Button onClick() is not working - android

I have two buttons on my main Activity, which both should lead to another activity, but when I run the code it is showing an error.
MainActivity.java is this:
package com.assignment2.courier;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void second (View v){
Intent i = new Intent(this, adelivery.class);
startActivity(i);
}
}
}
This is the button code in activity_main.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="85dp"
android:layout_marginBottom="234dp"
android:onClick="second"
android:text="Arrange Delivery"
android:textSize="20sp" />
Error:
Failed to compile values file
What is the problem here?

The problem here is that the second function is inside the onCreate
You need to adapt the code as follows:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void second (View v){
Intent i = new Intent(this, adelivery.class);
startActivity(i);
}
}

You should move second method out of the onCreate method; this is the problem in your code.

Related

How to open a new Activity from a tab?

My project has three tabs.
I want to open a new Activity from one tab
My Java current tab Activity
package com.example.muhsin.tabstes;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by muhsin on 22/07/17.
*/
public class Tab2Feeds extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab2feed, container, false);
return rootView;
}
}
layout:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="open new"
android:id="#+id/button1"
android:layout_below="#+id/section_label"
android:layout_alignParentEnd="true"
android:layout_marginEnd="204dp"
android:layout_marginTop="112dp" />
Then I create a new empty Activity called profile Activity
package com.example.muhsin.tabstes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class ProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
}
}
How to navigate from Tab2Feeds to ProfileActivity?
To start activity from Fragment:
put your code in onCreateView method or onViewCreated method:
Button button = (Button) rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getActivity() != null) { // check if activity not null
Intent intent = new Intent(getActivity(), ProfileActivity.class);
getActivity().startActivity(intent);
}
}
});
In your Tab2Feeds Fragment, get a reference to the button by calling finViewById.
From there set a clicklistener with setOnClickListener.
Finally use an Intent to open the new activity.
You will need a Context which is gotten by calling getActivity()
Button button = (Button) rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ProfileActivity.class);
getActivity().startActivity(intent);
}
});
A the top of the file dont forget your imports
import android.widget.Button;
import android.content.Intent;

Multiple Intents on Change of BG Color from one activity to the other # Android

My initial plan was to have a UI preference so the user can pick a bg color from the main activity whenever he/she wants - I got that to work BUT it isn't showing up the right color it's specified to. i.e. When Red button is pressed in Main Activity, it shows up Blue in the next activity instead.
Here is a snippet of the code with only two buttons using multiple intents for demo purposes...
My Main layout:
<Button
android:id="#+id/buttonRed"
android:onClick="passBG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RED"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/buttonBlue"
android:onClick="passBG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLUE"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button" />
Main Activity is working:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
View view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view=this.getWindow().getDecorView();
view.setBackgroundResource(R.color.gray);
public void passBG(View v) {
Intent intent = new Intent(this, AudioActivity.class);
intent.putExtra("Red", R.color.red);
intent.putExtra("Blue", R.color.blue);
startActivity(intent);
}
}
But something's not right with the second Activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
public class AudioActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
int redBG = getIntent().getIntExtra("Red", -1);
int blueBG = getIntent().getIntExtra("Blue", -1);
RelativeLayout rootView = (RelativeLayout) findViewById(R.id.activity_audio);
rootView.setBackgroundResource(redBG);
rootView.setBackgroundResource(blueBG);
}
}
I figured out that whichever color in "..."BG is implemented first in
rootView.setBackgroundResource("..."BG)
will determine the one that will only pop out. It's something to do with the sequence I figured - AT FIRST ATTEMPT, I tried using the intents on separate methods i.e.
public void goRED(View v)
{
view.setBackgroundResource(R.color.red);
Intent intent = new Intent(this, AudioActivity.class);
intent.putExtra("Red", R.color.red);
startActivity(intent);
}
public void goBLUE(View v)
{
view.setBackgroundResource(R.color.blue);
Intent intent = new Intent(this, AudioActivity.class);
intent.putExtra("Blue", R.color.blue);
startActivity(intent);
}
BUT THAT WILL ONLY END UP IN ERROR so I called the buttons on the same method to prevent that problem. So now I'm stuck with a new problem - how do I correctly implement multiple intents to show up the right color BG on the next activity? should I use some if-else statement into it? Any help would be appreciated. Thanks!
Well try this:
Main Activity is working:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
View view;
private Button redBtn;
private Button blueBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view=this.getWindow().getDecorView();
view.setBackgroundResource(R.color.gray); //This can be achieved by using the `android:background` attribute to your main element on your activity layout
redBtn = (Button) findViewById(R.id.buttonRed); //also, attributes ids on xml layouts shouldnt use uppercase letters, separete words using '_'
blueBtn = (Button) findViewById(R.id.buttonBlue);
redBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onButtonClick(R.color.red);
}
});
blueBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onButtonClick(R.color.blue);
}
});
}
private void onButtonClick(int color){
Intent intent = new Intent(this, AudioActivity.class);
intent.putIntExtra("background",color);
startActivity(intent);
}
}
then, on your second activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
public class AudioActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
int color = getIntent().getIntExtra("background", -1);
if(color != -1){
RelativeLayout rootView = (RelativeLayout) findViewById(R.id.activity_audio);
rootView.setBackgroundResource(color);
}
}
}

android onClick Does not work

The code worked fine in other layout, but in others not..
layout XML:
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="register"
android:id="#+id/welcome_register"
android:background="#android:color/holo_green_dark"
android:textColor="#ffffff"
android:textSize="25sp"
android:onClick="register_Click" />
Activity:
package il.co.smartchip.hobby;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().hide();
}
public void start_login(View view) {
//TODO log in
}
public void register_Click(View view) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
}
I tried several things without success. Do you have any idea why it does not work?
In your case you need Intent i = new Intent(LoginActivity.this, RegisterActivity.class);.
LoginActivity.this points to the instance of the Activity you are currently in and you using it when you work with dynamic inner class like in your case.
this is for your current Object.
Im not so good teacher, hope you understand it.
Delete onclick on xml
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="register"
android:id="#+id/welcome_register"
android:background="#android:color/holo_green_dark"
android:textColor="#ffffff"
android:textSize="25sp"
/>
Init your button like that
package il.co.smartchip.hobby;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class LoginActivity extends AppCompatActivity {
private Button myButton;
private Activity thisActivity=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().hide();
myButton=(Button)findViewById(R.id.welcome_register);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(thisActivity, RegisterActivity.class);
startActivity(intent);
}
});
}}

Android app hangs when referencing the back button on Secondary screen

I have a small app that when button presses navigates when moving from main screen to next screen this works fine, but when I added a button on the next page (to go back) it breaks.
Fun.java
package com.forcetechnology.OptusApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Fun extends Activity {
OnClickListener backListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fun);
Button backButtonf = (Button)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf);
backListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.OptusAppMain");
startActivity(i);
}
};
backButtonf.setOnClickListener(backListener);
}
}
Fun.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:orientation="vertical" >
<ImageButton
android:id="#+id/backtoMainf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/val5" />
</LinearLayout>
Main.xml
<ImageButton
android:id="#+id/funbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/val5"
android:src="#drawable/val5" />
<ImageButton
android:id="#+id/executionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/funbutton"
android:layout_alignParentRight="true"
android:background="#drawable/val2"
android:src="#drawable/val2" />
<ImageButton
android:id="#+id/performancebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/executionbutton"
android:layout_toLeftOf="#+id/funbutton"
android:background="#drawable/val3"
android:src="#drawable/val4" />
<ImageButton
android:id="#+id/innovationbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/executionbutton"
android:layout_alignParentLeft="true"
android:background="#drawable/val3"
android:src="#drawable/val3" />
<ImageButton
android:id="#+id/peoplebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/innovationbutton"
android:layout_toLeftOf="#+id/executionbutton"
android:background="#drawable/val1"
android:src="#drawable/val1" />
</RelativeLayout>
OPtusAppMain.java
package com.forcetechnology.OptusApp;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class OptusAppMain extends Activity
{
OnClickListener funListener,executionListener,innovationListener,peopleListener,performanceListener;;
TextView testView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.forcetechnology.OptusApp.R.layout.main);
ImageButton funButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.funbutton);
ImageButton executionButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.executionbutton);
ImageButton innovationButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.innovationbutton);
ImageButton peopleButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.peoplebutton);
ImageButton performanceButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.performancebutton);
funListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Fun");
startActivity(i);
}
};
executionListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Execution");
startActivity(i);
}
};
innovationListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Innovation");
startActivity(i);
}
};
peopleListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.People");
startActivity(i);
}
};
performanceListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Performance");
startActivity(i);
}
};
funButton.setOnClickListener(funListener);
executionButton.setOnClickListener(executionListener);
innovationButton.setOnClickListener(innovationListener);
peopleButton.setOnClickListener(peopleListener);
performanceButton.setOnClickListener(performanceListener);
}
}
Edit: I have traced the error to this line Button backButtonf = (Button)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf); in fun.java.
In the onClick() of backListener, just call finish() to go back to the previous activity.
Lets take an example: You have two activities "A" & "B", You start your "B" activity from "A" that means your "A" activity is in stack so there is no need to start it again, just finish your "B" activity with "finish()" method.
public class A extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
public void onAbuttonClick()
{
startActivity(new Intent(A.this,B.class));
}
}
}
public class B extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public onBbuttonclick(View v)
{
finish();
}
}
}

button question

I have recently started on android.
i have written this piece of code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ArrangeMe extends Activity {
private Button button1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.button1 = (Button)findViewById(R.id.buttonOne);
this.button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
setContentView(R.layout.main);
}
}
and my main.xml looks as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="ArrangeMe"/>
<Button android:text="Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/buttonOne"></Button>
</LinearLayout>
but when i pass through this line
this.button1 = (Button)findViewById(R.id.buttonOne);
i observe button1 = null. but when i type R.id. eclipse does suggest an auto complete buttonOne (that suggests layout xml is correct !)
where am I going wrong ?
edit:
interstingly, i tried the following code,
still the button does not appear. it has stopped crashing, but the button does not appear !
button1 = new Button(getContext());
button1.setText("1");
addView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
this was giving error
i changed them to
button1 = new Button(getBaseContext());
button1.setText("1");
addContentView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
I think you should call setContentView(R.layout.main); After that you activity class should be aware there to find views.
You are assigning the button1 before setting the ContentView in your Activity. Change your code as follows,
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ArrangeMe extends Activity {
private Button button1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // you misplaced this line in your code
this.button1 = (Button)findViewById(R.id.buttonOne);
this.button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
}
}
you must use small letter for id of your view and second one build your project and clean that may generate id for button in R.java.

Categories

Resources