I'm pretty new to learning about java and android development; so please help me out. In the next line of code I am trying to create a button but I am having trouble understanding why the 'id' is underlined. When I fix it in the R folder the .blue_yes gets underlined and I am confused what I should do next? Any help would be helpful.
Thanks
Button yes = (Button) findViewById(R.id.blue_yes);
yes.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mBtAdapter.enable();
setResult(CheckBluetoothEnabledActivity.RESULT_SUCCESS);
finish();
}
});
Button cancel = (Button) findViewById(R.id.blue_cancel);
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setResult(CheckBluetoothEnabledActivity.RESULT_CANCEL);
finish();
}
you should have a button in the layout xml file with id blue_yes and blue_cancel before accessing that control in your in your code. probably you have not declared the control in your xml file correctly.
Once you specify a control in Layout file entry in R.Java is automatically made by eclipse android plugin. you don't need to mess with R folder and its file as R.java in automatically generated file and you should not change anything in that.
if even after declaring your control in layout file you are not able to get your control using findViewById method then please post your layout file as well to help you further.
Given the data, one of gazillion possible explanations is that you don't have R imported (and then fix it by creating a new class, and the new class lacks the id attribute).
Related
I'm currently working on making an Android app, but have been having some trouble. I want to be able to push a button with the title of the document as it's TextView and then have that document open to be read. I've looked around for guides but everything I've found is either out of date or doesn't explain any of the code shown. Does anyone know how I can put such a thing in my app? At this point, I'm not even sure where to start with the process.
Note, I'm working in Java not Kotlin.
UPDATE: I was directed to a solution using intents. Now there seems to be an issue loading the file. My code is this:
public class atotf_pdf extends literature {
File file = new File("/storage/emulated/0/AToTF Preview.pdf");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Uri pdfURI = FileProvider.getUriForFile(atotf_pdf.this, "net.whispwriting.whispwriting.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfURI, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
Is it something here that's causing the error?
It looks like the file is somehow not being stored in the filesystem when the app is installed.
Just like #ruben said, you could use a WebView for such a task: https://developer.android.com/reference/android/webkit/WebView. For more information about using WebView to display content, you can take a look at this post: How can I display a pdf document into a Webview?
Another potential solution could be using intents, which has also been discussed before: How to open a PDF via Intent from SD card
Hopefully this helps!
I was watching and tried to do the tutorial for the generator QR Code in this video.
And then like in the end of it when he want to try the generator works, he shows some kind a 2 buttons, 1 for the scanner and 1 for the generator.
But in the video he didn't explain how he do it. I already tried his video about the scanner but still no clue. Can someone help me?
UPDATE:
Now here's my new problem regarding with gradle version:
Error:Could not find com.android.tools.build:gradle:2.10.
Searched in the following locations:
file:/C:/Users/User/Downloads/android-studio-ide-171.4443003-windows32/android-studio/gradle/m2repository/com/android/tools/build/gradle/2.10/gradle-2.10.pom
file:/C:/Users/User/Downloads/android-studio-ide-171.4443003-windows32/android-studio/gradle/m2repository/com/android/tools/build/gradle/2.10/gradle-2.10.jar
https://jcenter.bintray.com/com/android/tools/build/gradle/2.10/gradle-2.10.pom
https://jcenter.bintray.com/com/android/tools/build/gradle/2.10/gradle-2.10.jar
https://maven.google.com/com/android/tools/build/gradle/2.10/gradle-2.10.pom
https://maven.google.com/com/android/tools/build/gradle/2.10/gradle-2.10.jar
Required by:
:QRCode:unspecified
Open File
Anybody can help me fix this?
Look at the description of the Youtube video, there is a link to the source code
https://github.com/prudhvirajkumar10/QR-Code/
download the source code and open MainActivity.java
#Line 10 in MainActivity.java he declared the two buttons like this,
Button gen, scan; // gen for generate and scan for scanning qr code
Again in MainActivity.java you can see this code,
gen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent gIntent = new Intent(MainActivity.this, GeneratorActivity.class);
startActivity(gIntent);
}
});
The above "OnClickListener" gets executed when the gen button is pressed. When "GENERATE" button is pressed it moves from MainActivity to the GeneratorActivity which you can find in "GeneratorActivity.java"
Similar thing happens when clicking scan button, Look at these code in MainActivity.java
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent rIntent = new Intent(MainActivity.this, ReaderActivity.class);
startActivity(rIntent);
}
});
When "SCAN" button is clicked it changes from MainActivity to ReaderActivity which is found in "ReaderActivity.java".
If you have any doubt feel free to ask me.
In my first steps in exploring Android I now start with QR scanning.
Works all pretty well. But I am not able to come back from the ResultHandler after read the QR successfully to my MainActivity.
public class MainActivity extends AppCompatActivity implements
ZXingScannerView.ResultHandler
{
private ZXingScannerView mScannerView
....
#Override
public void handleResult(Result rawResult)
{
// my results are ok in rawResult
// the scanner does not scan anymore but it is still there
// how to go back to my main activity???
}
public void ClickButton (View view)
{
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
}
}
I tried
mScannerview.stopCameraPreview
mScannerView.stopCamera
this.finish
setContentView(R.layout.activity_main); // shows my activity_main
// but I can not click anything
Thanks!!
EDIT
I added some code to describe it a bit better. The idea is from
https://www.numetriclabz.com/android-qr-code-scanner-using-zxingscanner-library-tutorial/
Your question isn't clear but I'm assuming you want to restart the scan process. Normally, you'd have to restart the SurfaceHolder to be in preview mode. Luckily for you the ZXingScannerView already has a method to do that. Call mScannerView.resumeCameraPreview(this) to restart the scan process.
Otherwise can you clarify? You say you want to go back but you're already in MainActivity
If you want to go back into activities/fragments stack you can try Activity.onBackPressed()
if you are in a fragment you must call this method against attached Activity
What do you want is not going back to your activity. You want to restore activity's layout.
I think the better choice is to add ScannerView to your activity's layout file with android:visibility="gone". Then in on click you can get this view and change it's visibility to VISIBILE.
Then when you have handled scanning result, you can reset yuoir ScannerView to visibility = GONE
I too was stuck with this problem for an hour, just like you. And later realised..
To solve this problem DON NOT implementing the ZXingScannerView in the same activity or fragment. Instead start a new activity when you click the button and this activity is just for the ZXingScannerView
Once the Scan is done finish and pass the data back to your activity or fragment
Just restart your MainActivity before this.finish()
the code below will start your main activity through intent...
worked fine for me
startActivity(new Intent(this,MainActivity.class));
this.finish();
remove from onCreate method this line setContentView(your layout) and when you finish scan write it after you stoped the camera then you can use your layout after scan
I had a bit of a look into android concepts and activities.
I put the QR handling in a 2nd activity and it worked well with the finish ().
Thanks for help anyway!!
I think it's too late, but I came with the same problem and I had so find a solution by myself.
You were on the right way, you need two steps more.
I called the methods where I link and set the listener of any buttons
There are the methods
Basically you were right where you set the content view, but you need to give the buttons their functionality back.
(I know its late, but better late than never). Good luck!
I have an activity_main.xml with
tools:context="com.example.android.newwine.MainActivity"
and
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="goToNext"/>
In my MainActivity.java file I have
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
and
public void gaNaarNext(View view){
setContentView(R.layout.activity_main2);
}
I have an activity_main2.xml file with
tools:context="com.example.android.newwine.MainActivity"
When I touch the button next now the activity_main2.xml will be used as ContentView, so that's correct, but when I do anything then (like touching a RadioButton or doing doesn't matter what) in my activity_main2.xml file what calls a method, it says: "New Wine is stopped." Whatever I do what calls a method, the app crashes. I tried much and if I'm going to paste everything I tried here this will be a long, long question:).
But has someone an answer? I really don't know what to do now, so I thought; let's ask something on Stackoverflow. Thanks already!
You might prefer to use another activity or fragments to change your UI as using the method setContentView() multiple times is not recomended because of all the drawing involved.
Since it seems like you don't want to switch activities fragments may be the easiest to use.
This stackoverflow question addresses that matter as well.
After you change your content view, the UI Elements on the screen have changed. All of the variables that you previously set up now point to the old screen - which no longer exists. Once you change your content view, you must set these variables to the correct elements on that layout file. Also, you may want to check out Fragments like Carlos Sifuentes said.
When I do anything then (like touching a RadioButton or doing doesn't matter what) in my activity_main2.xml file what calls a method, it says: "New Wine is stopped." Whatever I do what calls a method, the app crashes.
The problem is you have used activity_main2.xml but you did'nt get the reference of RadioButton of activity_main2.xml. You should get all the view reference after setting new layout using setContentView() and then use as per your needs.
Update your gaNaarNext() method as below:
public void gaNaarNext(View view){
setContentView(R.layout.activity_main2);
// For example: If your activity_main2 contains radio_button then do this
RadioButton radioButton = (RadioButton) findViewById(R.id.radio_button);
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// Do something
}
});
}
Hope this will help~
I m a newbie an trying to learn Java/Android-programming.
I m doing an app for Android in Eclipse and created some buttons.
I have a back and a cancel button.
Example:
I have a EditText there you can write in your name. If you write yourname and press the backbutton, then u will go back to the previous Activity, but if you go to the same Activity, then you will still see the name that you wrote in the EditText.
But if you press the cancelbutton, you will go back to the previous Activity, but when you come back, yourname will be empty. I will "kill" or "stop" the Activity.
This is the code I use for the Backbutton, what would you use for the Cancel Button?
Thank YOU.
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonBack:
Intent intent = new Intent (AllActivity.this, MenuActivity.class);
startActivity(intent);
break;
For the cancel button you can use the below method, this will kill the activity.
finish()
so in your code it will look something like this:
public void onClick(View v) {
switch(v.getId()){
case R.id.cancel:
finish();
break;
There was little difference in this as per requirement of process or application flow. For cancel and back as work are same for example if you open any dialog and provide cancel button will close/dismiss your dialog same way the back button do this. While for implementing with the Activity you if you implement for closing current activity you can just finish with both option by just calling finish() method. As back button was normally work for finish you current activity and back.
Another way to do this that you may be interested in is to wipe out the content of the EditText yourself.
You would need to have in your xml file an id defined for the EditText so that you could access it programatically.
<EditText
layout stuff here:
android:layout_width="fill_parent"
...
and then the id attribute
android:id="#+id/edit_text_id"
>
then in your code you would put the following in your class (not inside any method):
EditText anEditText;
then in your onCreate(), after the inflation of the layout (if it comes beforehand it will cause the app to crash):
anEditText = (EditText) findViewById(R.id.edit_text_id);
the name edit_text_id is not significant, but it is what we used in the layout file
next add to the onClick method for cancel (after the case statement):
//this wipes the text from the textbox
anEditText.setText("");
// add the rest of the back button code after this and your good!
Best of luck! Remember that we were all newbies once. If you want to be a good android programmer, I suggest that you get a strong background in Java first. This free book helped me very much!
Java Notes