I have a Button which opens another layout when clicked. The second layout which opens also has a Button but somehow nothing happens on that Button when clicked.
My Activity:
package com.example.android.myapplicationo9;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import static com.example.android.myapplicationo9.R.layout.layout2;
public class MainActivity extends Activity implements View.OnClickListener {
public Button bt;
public Button bt2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LayoutInflater factory = getLayoutInflater();
final View textEntryViw = factory.inflate(R.layout.layout2,null);
bt2 = (Button)textEntryViw.findViewById(R.id.button2);
bt = (Button)findViewById(R.id.button);
bt.setOnClickListener(this);
bt2.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.button:
setContentView(layout2);
Toast.makeText(MainActivity.this,"e", Toast.LENGTH_LONG).show();
break;
case R.id.button2:
Toast.makeText(MainActivity.this,"e", Toast.LENGTH_LONG).show();
break;
}
}
}
As you are setting new content view layout2 for your MainActivity, you have to initialize views of new layout2 again to use later.
1. Get your Button reference from layout2.
2. Set onClick listener to your Button.
Here is the working code:
case R.id.button:
{
setContentView(layout2);
// Initialize button of layout2
Button button = (Button) findViewById(R.id.button_of_layout2);
// Set onClick listener
button .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Layout2 Button clicked", Toast.LENGTH_LONG).show();
}
});
Toast.makeText(MainActivity.this,"e",Toast.LENGTH_LONG).show();
break;
}
Hope this will help~
did you impl. on click method on both buttons? also - make sure you are not consuming the click / touch event thus "not passing" it further on throughout the view hierarchy
Use Intent for opening new Activity
Intent intent = new Intent(this, YourClass.class)
startActivity(intent);
Using setContentView only Changes the layout but you're still in the same class
Try using normal onClick methods and make sure you instantiate your views on the second Activity.
Related
I'm trying to implement a snackbar in android studio where when i click the button in the center of the screen, it will display the snackbar and say "moving to second screen". if the user clicks the button "CLOSE" within the snackbar, the app will close.
the problem is that when i open the app and click the button in the center of the screen, the entire app crashes
this is the code that i have so far:
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.material.snackbar.Snackbar;
public class MainActivity extends AppCompatActivity {
Button button;
CoordinatorLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
Snackbar snackbar = Snackbar.make(layout, "moving to second screen",
Snackbar.LENGTH_LONG).setAction("CLOSE",new View.OnClickListener() {
#Override
public void onClick(View view)
{
MainActivity.this.finish();
System.exit(0);
}
});
snackbar.show();
}
});
}
}
variable layout is not initialised and you have directly used it in Snackbar code
you can use following code to resolve your crash
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "moving to second screen",
Snackbar.LENGTH_LONG).setAction("CLOSE",new View.OnClickListener() {
#Override
public void onClick(View view)
{
MainActivity.this.finish();
System.exit(0);
}
});
or you can initialise layout variable like this
layout = findViewById(R.id.top_parent_container_id);
here top_parent_container_id is id that you have used in your xml layout for top parent
It's just a simple text with two buttons that creates toasts and change the text. I'm very new to this AS so I really don't know how to even attempt to fix this..
Any help is very appreciated...
MainActivity.java:
package com.example.david.davidisawesome;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Started.");
final TextView firstText = (TextView) findViewById(R.id.firstText);
Button firstButton = (Button) findViewById(R.id.firstBtn);
Button secondButton = (Button) findViewById(R.id.secondBtn);
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: First Button Clicked.");
toastMessage("You Clicked the first button");
firstText.setText("Nice Job.");
}
});
secondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Second Button Clicked.");
toastMessage("You Clicked the second button");
firstText.setText("Good Effort.");
}
});
}
private void toastMessage(String message){
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
}
In picture:
activity_main.xml:
Error:
It is not finding your xml elements. The error shows that you have a null exception on setting the onclick interface to the button. So start by confirming that you are inflating the proper xml and finding the proper id.
More specifically your secondBtn is not found.
I am trying to move from one activity to another activity by clicking to the ImageButton. But when I click to button, it doesn't move to the activity which I specify in the code, and even it does not throw an error. I'm not getting where is problem
Here is my code which calls next activity :
package com.birthdayreminder;
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;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class Reminder extends Activity {
ImageButton view, add, edit;
TextView tvadd, tvedit, tvview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
}
public void innicialize() {
// assigning buttons
view = (ImageButton) findViewById(R.id.bView);
add = (ImageButton) findViewById(R.id.bAdd);
edit = (ImageButton) findViewById(R.id.bEdit);
// assign textview
tvadd = (TextView) findViewById(R.id.tvAdd);
tvedit = (TextView) findViewById(R.id.tvEdit);
tvview = (TextView) findViewById(R.id.tvView);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
}
}
I have also declared the class name in the manifest.xml but it does not working
Logcat Log file : 5 lines of last logcat file after clicking the button :
05-15 18:19:25.495: W/AudioFlinger(33): write blocked for 69 msecs, 1245 delayed
writes, thread 0xc658
05-15 18:19:28.964: I/ActivityManager(60): Starting: Intent {
act=com.birthdayreminder.REMINDER cmp=com.birthdayreminder/.Reminder } from pid 548
05-15 18:19:29.409: I/ActivityManager(60): Displayed
com.birthdayreminder/.Reminder: +426ms
try using getApplicationContext() in place of Remainder.this
check if you have a click listener on you imagebutton, you have declared what to do on click, but if you don't have any listener on the imagebutton, it won't work anyway.
You can try something like this :
yourImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Reminder.this, Addreminder.class);
startActivity(intent);
}
});
After doing this, you must have declared in your XML file the activity
<activity android:name="packageName.className" />
Finally, check if the new class is working well too but it must be ok!
==================================================================================
You mustn't declare a function or procedure in a function (like you did in onCreate), you can simply do what you do in initialize in your onCreate function like this :
package com.birthdayreminder;
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;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class Reminder extends Activity {
private ImageButton view;
private ImageButton add;
private ImageButton edit;
private TextView tvadd;
private TextView tvedit;
private TextView tvview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
// Instantiating Buttons
view = (ImageButton) findViewById(R.id.bView);
add = (ImageButton) findViewById(R.id.bAdd);
edit = (ImageButton) findViewById(R.id.bEdit);
// Instantiating Views
tvadd = (TextView) findViewById(R.id.tvAdd);
tvedit = (TextView) findViewById(R.id.tvEdit);
tvview = (TextView) findViewById(R.id.tvView);
// Adding a clickListener on add button
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
// Adding a clickListener on edit button
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
// Adding a clickListener on view button
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
}
}
If you still have a problem, check your XML files, and look if you have well declared your buttons.
Problem Fixed: Never define a function in a function, because everytime you call the first function you'll define again the second function.
What you did which is wrong :
onCreate { //WRONG VERSION
bla bla bla bla
initialisation {
bla bla bla
}
}
What you have to do :
onCreate { //GOOD VERSION
initialisation();
bla bla bla
}
initialisation {
bla bla bla
}
you can call a function in a function, but you can't define a function in a function.
Moreover, you never call your initialisation function, you must call it now in your onCreate function or nothing will happen.
have you set onClickListener on add button.
btnAdd.setOnClickListener(this);
also check the
is your v.getId() and case R.id.bAdd: same?
debug and check that your case R.id.bAdd: is getting executed..
Seperately initialize the buttons in the first activity as
Button bAddbutton= (Button) findViewById(R.id.bAdd);
Button bEditBtn= (Button) findViewById(R.id.bEdit);
Button bViewBtn= (Button) findViewById(R.id.bView);
Then add seperate onClick listeners for each button as given below,
bAddbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
bEditBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
bViewBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
initialize the button and add listener first in oncreate()
Button btnAdd = (Button) findViewById(R.id.bAdd);
btnAdd.setOnClickListener(this);
I've tried using this link but I can't get it to work for a spinner and a radio button.
Is there a better or easier way to share a listener between various items of different varieties?
EDIT - I should have mentioned that the reason I want to share the listener is that I need to use various adapters that I'm having trouble using in other classes.
You can define a class that implement listener for both Spinner and radio button,
create one instance of the class and then assign that instance to both the radio button and the spinner.
For example:
package italialinux.example;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
public class ButtonAction implements OnClickListener, OnItemSelectedListener {
TextView btnLocalText;
public ButtonAction(TextView tv) {
super();
btnLocalText = tv;
}
#Override
public void onClick(View arg0) {
btnLocalText.setText("Hello from a ButtonAction!!");
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
If you want them to perform the same function on the click, the simply add the same listner instance in the setOnClickListener() method to each view/widget. If not then you will have to detect which view was clicked and perform the required action accordingly
i think the code example below is what you looking for
//the import for onClickListener you need is here (along with other imports --- you can get all of them by pressing ctrl+shift+o on Eclips IDE)
import android.view.View.OnClickListener;
public class MyActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button myButton = (Button) findViewById(R.id.myButtonId);
ImageView myImageView = (ImageView) findViewById(R.id.myImageViewId);
RadioButton myRadioButton = (RadioButton) findViewById(R.id.myRadioButtonId);
CheckBox myCheckBox = (CheckBox) findViewById(R.id.myCheckBoxId);
myButton.setOnClickListener(this);
myImageView.setOnClickListener(this);
myRadioButton.setOnClickListener(this);
myCheckBox.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.myButtonId:
// do the work here for Button click listener
break;
case R.id.myImageViewId:
// do the work here for Image click listener
break;
case R.id.myRadioButtonId:
// do the work here for RadioButton click listener
break;
case R.id.myCheckBoxId:
// do the work here for CheckBox click listener
break;
}
}
}
how do i embed a tab layout within a button. My main layout is a linear layout, but i don't know how to program the main activity.java class. Could anybody help me get started this is what my main.java code looks like right now
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class Remote_DocActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//private static final String TAG = "Remote_Doc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View Patient_Button = findViewById(R.id.patientButton);
Patient_Button.setOnClickListener(this);
View Doctor_Button = findViewById(R.id.doctorButton);
Doctor_Button.setOnClickListener(this);
View About_Option = findViewById(R.id.aboutButton);
About_Option.setOnClickListener(this);
View Exit_Option = findViewById(R.id.exit);
Exit_Option.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.aboutButton:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
you can create tabs with the help of TabActivity class. You can take help from android-wireless-application-development book's unit 3 chapter 8 example.