This is my login.xml.
This is my java file. There are no red marks in my java file, but it is showing error while compiling.
In the following lines.
textButton = (Button) findViewById(R.id.TextButton);
fingerprintButton = (Button) findViewById(R.id.FingerprintButton);
I keep getting this error:
Error:(52, 48) error: cannot find symbol variable TextButton
Error:(53, 55) error: cannot find symbol variable FingerprintButton
I have declared these vairables and included the in string.xml as well. I am new to android studio. Please help me.
emmm...here you want to access IDs of Button right? In Android arch, ids are defined in R.java and no need to edit anything because Android Studio would generate automatically. Things you need to do is simply add your id in your layuot.xml, with a xml tag names android:id='#+id/textButton' under a Button tag. Not String.xml.
Rename the buttons as text_button and fingerprint_button in your login.xml. Then make the changes accordingly in your LoginActivity.java like the following.
textButton = (Button) findViewById(R.id.text_button);
fingerprintButton = (Button) findViewById(R.id.fingerprint_button);
Clean and rebuild your application.
Try this :-
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
public Button textButton, fingerPrintButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
textButton = (Button) findViewById(R.id.textButton);
fingerPrintButton = (Button) findViewById(R.id.fingerPrintButton);
textButton.setOnClickListener(this);
fingerPrintButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v == textButton)
{
//Your Custom Code
}
else if(v == fingerPrintButton)
{
//Your Custom Code
}
}
Related
I'm new to JAVA and Android. Previously, I was working on Javascript & Jquery.
As in HTML using Javascript (or Jquery) we can add custom data attribute to any element. for example-
$("#element").data("customdata", "customvalue");
And later I can get the value by doing-
var customvalue = $("#element").data("customdata");
Is there any method available in Android to achieve this type of thing?
Like, if I need to set multiple type of strings to a single TextView and later get them as needed.
Thank you all in advance.
You can get your textview from XML and set text on it . Something like this
TextView textView = findViewById(R.id.textview_id);
textView.setText("Your awesome text");
// Later fetch the text details back
String textDetails = textView.getText().toString();
This is a basic code sample of how to add and retrieve text from the text-view. Hope this help your case
You can use the View's setTag() and getTag() for that purpose.
However, most of the time there are cleaner ways like using ViewModel architecture etc. but that is a different story.
I saw your link. I think you can use AlertDialog.
this is my code.I think you would need it.
public class MainActivity extends AppCompatActivity {
EditText e1;
TextView t4;
Buttob btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t4=(TextView)findViewById(R.id.t4);
t4.setOnClickListener(t4click);
e1=(EditText)findViewById(R.id.e1);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(btnlogin);
}
private Button.OnClickListener btnlogin=new Button.OnClickListener(){
//click button
public void onClick(View v){
String account=e1.getText().toString();//get edittext value
String tmp="";
tmp=t4.getText().toString()+account; //get textview value and add
edittext string
t4.setText(tmp);//insert new value
}
}
};
private TextView.OnClickListener t4click=new TextView.OnClickListener(){
#Override
//if click textview it,would show dialog
//if your dialog value is from edittext ,you can reuse it.
public void onClick(View view) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("product")
.setMessage("item:pen"+"\nid=A001"+"\nprice:10 USD")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();
}
};
}
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 can't get the context of my activity for some reason. Note - It was working before but now Android Studio shows an error but does not stop my app compiling and running as expected. I've added my code further down but ultimately I think the problem is somewhere else because if I try to get the activity context in a new, empty activity, I get an error.
package com.example.myapp
public class TestActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Context context = this; // Error here
}
}
The error is:
Incompatible types.
Required: android.content.Context
Found: com.example.myapp.TestActivity
This error only happens for this project. My searches for an answer have yielded no positive results. In fact I can't find anything on the exact issue I'm facing.
What I have tried to fix this:
this instead of MainActivity.this - same error as above
getApplicationContext() - cannot resolve method error
getActivity().getApplicationContext() - same error as #2
Clean & Rebuild project / Sync Project with Gradle Files
Restarting Android Studio
Android Studio versions 2.3.3 & 3.0 - same issue
I'm new to Android development so if you have a solution for me, please phrase it as simply as possible. Thanks in advance. Here is my code - I get the error in the onClick method where it says MainActivity.this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the Numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);
// If View is present, set a click listener on that View
if(numbers != null) {
numbers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
startActivity(numbersIntent);
}
});
}
}
}
I found a solution and that was to add google() to my project's build.gradle file like so:
allprojects {
repositories {
jcenter()
google()
}
}
I also deleted the project and cloned it again so it could build from scratch. Not sure whether that helped or not.
This is my first app and I'm having some trouble.
When I run the app it crashes and I don't know how to fix this error.
public class MainActivity extends AppCompatActivity {\
TextView outputBottom = (TextView)findViewById(R.id.output);
}
public void play_the_big_lie(View view) {
the_big_lie.start();
outputBottom.setText("ObamaCare, the big lie");
}
String literal in setText cannot be translated
This is not an error and you can safely ignore it (unless you need translation in your app).
It is simply a notification by Android Studio that you should be using a string resource file instead of a hard-coded string.
If I had to guess at the source of the issue since you did not post the logcat, it is this.
You can't use findViewById before setContentView because there is no view to "find".
Please try something like the following code
public class MainActivity extends AppCompatActivity {
private TextView outputBottom;
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
outputBottom = (TextView)findViewById(R.id.output);
}
There are two issues here. First, you can't use findViewById until you have "created" things and have a view to find things with, so as the previous answer you want to separate them.
public class MainActivity extends AppCompatActivity {
private TextView outputBottom;
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
outputBottom = (TextView)findViewById(R.id.output);
}
...
}
To set text, it's better not to "hardcode" with a string in quotes, but to create a string file and ask for it, which will look more like this:
outputBottom.setText(R.string.my_words);
Here are the developer notes on strings.
If you're using Android Studio there are tutorials for how to make that happen.
You can ignore these warning by adding
lintOptions {
disable 'MissingTranslation'
}
to the gradle.build file.
I've created a simple 'app' to put values from text to an array and it doesn't seem to work.. I don't know why..
public class Register extends AppCompatActivity {
String[] data = new String[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
gotoReg();
}
public void gotoReg() {
Button register = (Button) findViewById(R.id.Submit);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data[0] = "ss";
data[1] = "dd";
}
}
);
}
}
From this code I get the error :
java.lang.NullPointerException: Attempt to write to null array
I did almost the same code in another app and it worked.. seems really wierd to me...
Nevermind, thanks everyone, just re-opened Android studio and everything worked fine.
Wasted on it several hours ughh
Just put your code to get button reference after setContent view then,you can assign the values.
And you have checked the id of button?
Try to add
data = new String[2];
to your gotoReg() method, before setting the onClick listener.