How to change a photo? - java

I am trying to change photos in android studio by clicking on my button.
When I put code for changing the photo in my MainActivity.java I keep getting this type of error messages and it says :
Cannot resolve symbol "image"
image.setImageResource(R.drawable.xxx);
I am watching Udemy course for android development and I have done everything same like the professor on that video.
I have tried to restart android studio.
I have tried to make new project.
I have tried to clear invalidate caches and restart.
public void changeImage(View view)
{
ImageView bitcoin = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
}
I hope there is actual error with android studio,because code is clone of the video that I am watching.

You are binding your layout's ImageView in Java file with bitcoin variable and you are trying to set an image on an unknown variable 'image'(maybe it's not defined in the class). So you have to set as below.
ImageView bitcoin = findViewById(R.id.bitcoin);
bitcoin.setImageResource(R.drawable.xxx);

Set Your Code Like this
ImageView image = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);

change your this line
image.setImageResource(R.drawable.xxx)
to this one:
bitcoin.setImageResource(R.drawable.xxx)

Related

Logs in Android Studio

I am a newbie of Android Studio.
I have a problem with display the logs in my app.
For example:
String timeStamp = new
SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cycle);
TextView textView = (TextView) findViewById(R.id.cykleoncreate);
Log.d("[ " + timeStamp + "]", "[onCreate]");
I only want to display this log in my app. How can I do it?
you can display the logs by Toast or set in textView
Toast.makeText(getApplicationContext(),timeStamp,Toast.LENGTH_SHORT).show();
or
textView.setText(timeStamp);
What you have done is write but you can get confused as in where's the log, Log has tag and message, Tags are usually Activity or fragment name so that its easier for one to locate from where did a particular error or message came from, and you can include anything in the message part of it.
we have different types of logs this can be found here
You will find a logcat button on bottom side left side of android studio from there you can select depending on which type you want to see? in your case it's debug
Ok, it works well
textView.setText(timeStamp)
But I don't know why it doesn't work, when I want to build my string:
textView.setText(timeStamp + "my string")
Actually Log is not displaying in app , it will display in android studio . For displaying in app , we need to use Toast . your code is working fine you can check it in your android studio , as its shown in image We can display like this Log.d("onCreate", timeStamp); and search using TAG as onCreate.

Convert Android view to PDF of A4 size

I have an android resume building application. I want to generate a PDF of size A4 from my view. Here's how my layout looks like - At the top I have a Top App Bar, and the whole view in encapsulated in drawer. The main part which contains user's details is encapsulated in nestedScrollView, which contains multiple LinearLayout and TextView. In this screenshot below, I have populated it with mock data, but in actuality, I am fetching data from the Firebase Realtime Database and displaying it on the UI.
I tried to understand iTextPdf solution and multiple question of similar type that has been asked here, but I couldn't find something solid. Please help me out, it would be of great help.
Also, please don't close this question by giving a reason that the question doesn't contain any code. It doesn't because I don't have any. I am trying to solve this problem from scratch. I have tried to describe my problem as much as I could.
try this:
create a WebView and copy the text of your edittext in it:
webview.loadData(youredittext.gettext().tostring, "text/html", "UTF-8");
and convert webview to pdf by below function:
private void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) + " Print Test";
if (printManager != null) {
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
}
after that user can select page size for example A4
There are a lot of libraries that convert layouts to PDF, but let's opt for popular one so we could find answers if we're stuck.
The libraries I listed works like so : They take screenshot of your layout as bitmap image and convert it to pdf.
- 1st solution: iTextPDF https://github.com/itext/itext7 (New Version).
check this detailed tutorial which treates also the case of taking screenshot of a scrollview https://www.codeproject.com/Articles/989236/How-to-Convert-Android-View-to-PDF-2
and this stackoverflow answer https://stackoverflow.com/a/29731275/12802591
- 2nd solution: PdfMyXML library https://github.com/HendrixString/Android-PdfMyXml just follow the steps in the documentation.
They may be other solutions, but these are the popular ones.
Let Me know if it works for you and also if you're stuck. Thank you!

ItemClickListener.onClick on a null object reference

I'm having trying to work on a simple rss reader app for a local news website/paper I volunteer with and I'm trying to make an easy to use and a simple app for the site.
My main issue I'm encountering is that it keeps crashing onClick of an RSS feed item with "null object reference", I just want the app to open the default browser so it can load the article.
I'm linking the code as it is in a zip so anyone willing to help can check the code, I can't remember where I originally had the code from but I think I was following a video tutorial. Mostly likely missing some of the code as I can't be sure if I finished watching it.
Link:
"https://www.dropbox.com/sh/imjgufrfpthbrql/AAA8MdTl-BAb7YzFqHswtYgxa?dl=0"
You have created method but forgot to initialize from activity or fragment.
` public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}`

A button which allows an image to be shown in Android Studio

I am building my application using Android Studio, this app can upload an image from raspberry to my emulator. It works fine. What I want to do now is uploading this image and showing it directly to the user without searching it in the gallery. I thought about creating another class and setting this image as a background image in my xml file, but this is too much like I have to create another class every time I want to upload an image from my raspberry.
Can someone help me please. Thank you
If I'm understanding your question correctly, you'd like to load an image from the Android filesystem into your app and display it to the user.
Drawable, Android's generalized image class, allows you to load from file via Drawable#createFromPath.
This SO question suggests Drawable#createFromPath doesn't work on paths beginning with file://, so depending on your use case you may want to precede that with Uri#parse/Uri#getPath.
Once you have a Drawable, you can display it in one of two ways: put an ImageView in your app and call its setImageDrawable method, or set the Drawable as your background image via View#setBackground (note that setBackground was only added in API 16 - in prior versions, you should call View#setBackgroundDrawable).
Putting all of this together, we end up with the following (untested):
private void loadImage(String imagePath) {
Uri imageUri;
String fullImagePath;
Drawable image;
ImageView imageDisplay;
imageUri = Uri.parse(imagePath);
fullImagePath = imageUri.getPath();
image = Drawable.createFromPath(fullImagePath);
imageDisplay = (ImageView) findViewById(R.id.imageDisplay);
/*if image is null after Drawable.createFromPath, this will simply
clear the ImageView's background */
imageDisplay.setImageDrawable(image);
/*if you want the image in the background instead of the foreground,
comment the line above and uncomment this bit instead */
//imageDisplay.setBackground(image);
}
You should be able to modify this to work with any View just by replacing imageDisplay's declared type with the appropriate View type and changing the cast on findViewById. Just make sure you're calling setBackground, not setImageDrawable, for a non-ImageView View.

Display Images in TextView html

I state that I have already read all the other questions but none is right for me.My app retrieves data from a database. If I put in the database in the app does not display the image, while the other tag html yes because i put:
Text = (TextView) this.findViewById(R.article.text);
String formattedText = db.getText();
Text.setText(Html.fromHtml(formattedText));
For images I would like something that the download so that they are always available. I tried to put ImageGetter but with the loading time of an article in the app increased a lot and very often said that Android is not responding (ANR). I also need something that resizes images depending on the display. Any ideas?
Take a look at AQuery download the latest jar add it in your project
and use it like following
AQuerymAQuery;
mAquery=new AQuery(context);
mAquery.id(ImageView).auth(handle).image(ImagePath,true,true,400,0,null,0,0.0f);

Categories