I am new to Android development and still trying to get the grasp of some of the concepts. I find the best way to learn is to jump right into the deep end with a project. With that said, here is my question:
I have integrated ZXing Android Embedded into my application; however, I am having trouble understanding the way in which you use IntentIntegrator. All I am trying to do at the moment is call the QR scanner to the screen when the user taps a button. I have been trying to follow the instructions on their github link [here][1] but have been unsuccessful.
Here is what my function looks like so far:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
IntentIntegrator integrator = new IntentIntegrator(this);
IntentIntegrator.forFragment(this).initiateScan();
}
});
I keep getting an error saying:
Error:(109, 25) error: constructor IntentIntegrator in class
IntentIntegrator cannot be applied to given types; required: Activity
found: Intent reason: actual argument Intent cannot be converted to
Activity by method invocation conversion
Also, as I put my mouse over '(this)' in Android Studio, it says:
anonymous android.view.View.onClickListener
Any help would be greatly appreciated, thanks! If you need any other information, please let me know.
An even easier solution than ChrisStillwell´s would be to make your activity-/fragment-class implement the OnClickListener, this way you do not need the reference variable:
public class SomeFragment extends Fragment implements View.OnClickListener {
// Rest of your code
#Override
public void onClick(View v) {
if (v.getId == button.getId) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
IntentIntegrator.forFragment(this).initiateScan();
}
}
}
If you are implementing a fragment-class, note that you have to call getActivity() when creating the IntentIntegrator.
You are referencing your OnClickListener and not your Fragment when you say this inside your onClick. You need to reference your Fragment, the easiest way to do that is setup a global variable and call it myFragment or something to your liking, then assign it this. For example:
Fragment myFragment = this;
public void myFunction(){
// Code and stuff //
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
IntentIntegrator integrator = new IntentIntegrator(this);
IntentIntegrator.forFragment(myFragment).initiateScan();
}
});
}
following can work:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new IntentIntegrator(getActivity()).initiateScan();
popupWindow.dismiss();
}
});
Related
I have successfully implemented a custom Dialog box that appears when the user tries to leave an activity via a back button or by using onBackPressed(). They can simply cancel the dialog box or continue, and leave the activity. This function has been implemented in multiple activities, however its making my code a lot longer than it needs to be. I wanted to know how to create a util that can be referenced in different activities, without the need for the chunk of code to copy pasted multiple times. Please note that I am retrieving the dialog title and description from string.xml
This is my code:
Dialog customDialog;
Button button_one, button_two;
TextView dialog_title, dialog_description;
customDialog = new Dialog(this);
//Back button will close app
#Override
public void onBackPressed() {
customDialog.setContentView(R.layout.custom_dialog_box);
dialog_title = customDialog.findViewById(R.id.dialog_title);
dialog_title.setText(getString(R.string.leaving_activity_warning_title));
dialog_description = customDialog.findViewById(R.id.dialog_description); dialog_description.setText(getString(R.string.leaving_activity_warning_description));
button_one = customDialog.findViewById(R.id.button_one);
button_one.setText(getString(R.string.cancel));
button_two = customDialog.findViewById(R.id.button_two);
button_two.setText(getString(R.string.leave_anyway));
button_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
button_two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
});
Objects.requireNonNull(customDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
customDialog.show();
}
UPDATE
Created a Java file called "DialogBoxMessage"
DialogBoxMessage Code:
class DialogBoxMessage {
private Dialog customDialog;
private TextView dialog_title, dialog_description;
private Button button_one, button_two;
//Custom Dialog Box Initialization
DialogBoxMessage(Button myButtonOne, TextView myDialogTitle, TextView myDialogDescription, Dialog myCustomDialog) {
customDialog = myCustomDialog;
button_one = myButtonOne;
button_two = myButtonOne;
dialog_title = myDialogTitle;
dialog_description = myDialogDescription;
}
void leaveActivity() {
customDialog.setContentView(R.layout.custom_dialog_box);
dialog_title = customDialog.findViewById(R.id.dialog_title);
dialog_title.setText(Resources.getSystem().getString(R.string.leaving_activity_warning_title));
dialog_description = customDialog.findViewById(R.id.dialog_description);
dialog_description.setText(Resources.getSystem().getString(R.string.leaving_activity_warning_description));
button_one = customDialog.findViewById(R.id.button_one);
button_one.setText(Resources.getSystem().getString(R.string.cancel));
button_two = customDialog.findViewById(R.id.button_two);
button_two.setText(Resources.getSystem().getString(R.string.leave_anyway));
button_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
button_two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
Objects.requireNonNull(customDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
customDialog.show();
}
}
I input the following code in another activity
Other activity code:
//Reusable exit dialog message
DialogBoxMessage dialogBoxMessage;
//Back button will close app
#Override
public void onBackPressed() {
dialogBoxMessage.leaveActivity();
finish();
}
But it doesn't seem to work, I think there are a lot of issues... please help :(
I assume customDialog is a seperate class you wrote - therefore i would suggest you put main information like contentview, title, message or type in the constructor when you initialize ur Dialog.
For your onClick Method I suggest you create an Interface to handle Button Clicks in your
customDialog class.
This could be implemented as a static method in a utilities class. The method would require 'this' as a parameter, which contains the activity context. The method should return the result of the button press. The activity can use this response to determine if finish() should be called or not.
UPDATE
I had suggested a simple static method, but you've gone down the object-oriented route. That's fine.
However, your constructor requires passing in several views, which wouldn't appear to achieve the code efficiency you are after.
Your constructor should just require the Activity context; everything else is encapsulated in your new class.
In each Activity's onBackPressed method you will need to create the object with
dialogBoxMessage = new DialogBoxMessage(this);
before you can call any of that object's methods.
hello friends i am new in android project i have a problem regrading click listener please check code below of mine project and help me guys how can i get out from this problem here is my output logcat of my project.
here is logcat
at android.app.ContextImpl.startActivity(ContextImpl.java:1238)
at android.app.ContextImpl.startActivity(ContextImpl.java:1225)
at com.kd.book.Adapter.MyComicAdapter$1.onClick(MyComicAdapter.java:60)
at com.kd.book.Adapter.MyComicAdapter$MyViewHolder.onClick(MyComicAdapter.java:96)
My code :
myViewHolder.setRecyclerItemClickListener(new IRecyclerItemClickListener(){
#Override
public void onClick(View view, int position) {
//save the comic selected
Common.comicSelected = comicList.get(position);
context.startActivity(new Intent(context,ChapterActivity.class)); } }); }
You have 2 option:
Option 1:
use
implements View.OnClickListener
at the end of your activity and set
myViewHolder.setRecyclerItemClickListener(this);
then you can call startActivity from the implemented method:
#Override
public void onClick(View v) {
startActivity()
}
Option 2:
use a final variable:
final Activity activity = this;
and then call the variable in your clickListener
activity.startActivity()
Hello I'm starting in Android Studio and I wanted to know how to pass from one activity to another. I've tried different methods which I've seen in youtube tutorials but all of them show me the same error:
Cannot resolve symbol 'activity_menu'.
Does anyone know how to solve it?
Here is my code:
public class Menu extends AppCompatActivity {
Button boton_start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
boton_start=(Button) findViewById(R.id.boton_menu);
boton_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(activity_menu.this,activity_dishes.class);
startActivity(in);
}
});
}
}
The same happens with the other activity, but I suppose that the solution is the same for both.
You've mentioned the layout file names instead of name of the activities
Your code:
Intent in = new Intent(activity_menu.this,activity_dishes.class);
It should be:
Intent in = new Intent(Menu.this,Dishes.class);
Mention the name of the activity, not layout files.
Try getting activity_menu like this: R.layout.activity_menu
I have trouble understanding this code. I get that findViewById will get the button widget and then it'll cast it. Then, it's going to use the button to call the setOnClickListener method. However, I don't know what is that argument being passed into the setOnClickListener and I have never seen code like that before. How is it that it creates a new object but is able to create a method of its own within another method's argument? Would be great if someone could explain that. Also, what type of object is the setOnClickListener method taking in?
btn = (Button)findViewById(R.id.firstButton);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
tv.setText(months[rand.nextInt(12)]);
tv.setTextColor(Color.rgb(rand.nextInt(255)+1, rand.nextInt(255)+1, rand.nextInt(255)+1));
}
});
It works like this. View.OnClickListenere is defined -
public interface OnClickListener {
void onClick(View v);
}
As far as we know you cannot instantiate an object OnClickListener, as it doesn't have a method implemented. So there are two ways you can go by - you can implement this interface which will override onClick method like this:
public class MyListener implements View.OnClickListener {
#Override
public void onClick (View v) {
// your code here;
}
}
But it's tedious to do it each time as you want to set a click listener. So in order to avoid this you can provide the implementation for the method on spot, just like in an example you gave.
setOnClickListener takes View.OnClickListener as its parameter.
This is the best way to implement Onclicklistener for many buttons in a row
implement View.onclicklistener.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
This is a button in the MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_submit = (Button) findViewById(R.id.submit);
bt_submit.setOnClickListener(this);
}
This is an override method
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.submit:
//action
break;
case R.id.secondbutton:
//action
break;
}
}
That what manual says about setOnClickListener method is:
public void setOnClickListener (View.OnClickListener l)
Added in API level 1 Register a callback to be invoked when this view
is clicked. If this view is not clickable, it becomes clickable.
Parameters
l View.OnClickListener: The callback that will run
And normally you have to use it like this
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
...
}
Take a look at this lesson as well Building a Simple Calculator using Android Studio.
its an implementation of anonymouse class object creation to give ease of writing less code and to save time
It works by same principle of anonymous inner class where we can instantiate an interface without actually defining a class :
Ref: https://www.geeksforgeeks.org/anonymous-inner-class-java/
I need to define a layout for multiple activities in android and from the UI part, it is successful. But to code those elements to perform on each click listeners, I need to define it in all the java pages I use.
Can we globally define this in a java page and include it in the required pages?
menuButton = findViewById(R.id.menuButton);
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), MenuActivity.class);
startActivity(i);
finish();
overridePendingTransition(0, 0);
}
});
Yes and No.
You can create a BaseActivity which has the common logic that has to be executed for each button click.
But you need to implement the listener for the button on specific activity, since life cycle of each activity is independent of other activity.
To make the code readable better (avoiding implementing listener/setOnclickListener), you can use ButterKinfe, and create a method for OnClick() annotation, and call the method in BaseActivity.
What you essentially want to do is call the findViewById(), which can only be called if you have a reference to a Context variable. You should use your Activity Context, hence you pass this to the static function, which can then access all methods accessible via Context .
public class ExampleActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MenuUtils.setListener(this);
}
}
Define the static class like this:
public static class MenuUtils{
public static void setListener(Context context){
menuButton = context.findViewById(R.id.menuButton);
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do stuff
}
});
}
}
What you should be careful about is that any Activity you pass to this function should have a menuButton in it's layout, otherwise you run the risk of getting a NullPointerException, which is when findViewById() cannot find menuButton.