How to set ImageView source to content provider throughout code? - java

I want to have an ImageView component within my activity and use Java to change the source.
The content provider is something like this:
content://com.spotify.music/image/OTIzMzlhZThkNjA2YWVkODRhZjY3NDU2YzgxNDM4MmM3NzlhMTU4MA%3D%3D%0A
I want to be able to change this image source whenever I want in the code.

Related

How to copy screenshot into android clipboard (java/kotlin)?

I make a screenshot of part of screen, convert it into bitmap and wonder how to put it into clipboard. Find the only text examples, so is it possible to put bitmap or something like that right into clipboard avoiding saving screenshot into file and using URI for ClipData?
Each Item instance can be one of three main classes of data: a simple CharSequence of text, a single Intent object, or a Uri. See Item for more details.
https://developer.android.com/reference/android/content/ClipData
Official doc says, there are only three types you can use with ClipData. So, it seems, you cannot use bitmap directly, but you can save it and send its URI to ClipManager as ClipData.

Understanding WebView Code

What exactly does this block of code (below) do?
WebView browser = (WebView) findViewById(R.id.activity_main_webview)
I have used it in my WebView app I started, and it seems to be important, but I don't understand what it does.
It just references to your webview view on the basis of ID you have given to view.
To say simple and short:
In android you define UI (for example layout of your activities) in some xml file. Each component in layout of activities could have an Id.
Android automatically create some java files to relate defined id to real components (IDs will save in a file/class which android named it R.java)
So when you want to access real component defined in xml file in the java code (for example in activity source code) you could use method findById and send id of requested component to it. This method returns real component (java object) related to given id.

Modify included layout in Android?

I am using an include tag in an Android layout file to include a relative layout row that I would like to repeat a few times. The problem is that after I include the row I cannot modify the text of a textview inside the layout. Is there anyway I can modify the text of a textview that is part of an include? Mainly I would like to insert 4 of these rows with custom text specified in the xml if possible.
You can do it only at runtime. Even if you do it at runtime, make sure you call findViewById from the parent view as Android doesn't allow you to use the same ID at more than one place in an xml file.
You can either add these 4 includes with a different id and get them from code or do what #Anis said and inflate them at runtime and put there your text.

Android Change image depending on a global variable

the question is simple.
I have 2 images in PNG format (logo1.png and logo2.png) in the project. Currently the project is loaded (in a imageview) the logo1.png, but I would do, depending on the value of a variable load the logo1.png or load logo2.png in imageview control.
The project currently has 20 Activitys with this picture (each with its own layout in XML), I will not be changing code on the 20 screens, it could do with a simple instruction to verify the value of the variable, but would have to make change in the 20 screens.
wonder if there is no way to do that depending on the value of a variable, change the image in the ImageView.
will be able to access the value of the variable from the same XML?
Thanks in advance.
I'm a bit confused, but I'll give it a shot.
Yes, you can have a global variable where you define which image will load. But from my understanding, you would need to change the code in the activities that load that image to make them load the image dynamically via the code-behind.
I suspect it would be something like this:
Get info from the database indicating what value to load.
SetImageToLoad(someValue)
In each class that loads the image, you'll need to retrieve the value that you previously set in Step #2.
public class HelperClass
{
int resIDOfImageToLoad = 0;
public static void SetImageToLoad(String imageName)
{
if(imageName.equals("abc"))
{
resIDOfImageToLoad = R.id.abc;
}
else if(imageName.equals("xyz"))
{
resIDOfImageToLoad = R.id.xyz;
}
}
public static int GetResourceIDOfImageToLoad()
{
return resIDOfImageToLoad;
}
}
Then in the class that needs to load the image, you would call something like this
ImageView myImage = (ImageView)findViewById(...)
myImage.setImageResource(HelperClass.GetResourceIDOfImageToLoad());
If i understand you correctly. Create a new XML View of the image e.g. logo.xml and use it in all 20 Views and when you want to change that image change only in the logo.xml.
Assuming you have a base Activity class, you can define a method getLogo() in the base class that would return the png (or the filename or whatever suits you). Then just call that method when inflating the layout.
Initially, you'd have to change all the Activities, but after that you just need to change the base class if you decide to change the logic that chooses which image to show. (If this is not what you intended, please clarify your question).
I would recommend using Style or Themes. Read over the section that talks about inheritance, and declare a separate Style for each logo. You can then reuse the Style in each one of your XML files.
If you decide to programmatically determine which image to use, you can declare a static method that can be used application wide to determine which logo to use in each Context, then setImageResource() accordingly.

SMS Balloons / Long Chat Boxes

I want to be able to add a text-messaging balloon every time the user revives data from a HttpGet, I want it so that it looks nearly identical to the default Android text messaging UI. I'm fine with all the code, I just need a way to create the UI and create another text balloon every time data comes back from a HttpGet request.
Thanks ever so much, for the answering this questions and I'm sure there's an easy way to do it, yet I've found no way by using the 'ole Google.
I am doing something similar for my app am doing the following to achieve it:
You will need a 9-Patch-Image (a stretchable PNG, see here) that represents the bubble. You want to make the part stretchable that does not include the corners of the bubble. You can create the bubbles using an image editor of your choice (I'd recommend a vector graphics editor like Inkscape). Then use the 9-Patch editor included in the Android Developer Tools to transform the PNG image into a 9-Patch PNG.
Create a custom layout file for one bubble. Create a textview inside it, and add your bubble as a background resource. (android:background)
Use an arraylist with a custom adapter to inflate and fill your items.
So far, this will give you identical bubbles as background for all messages.
If you want to get fancy, you can create different bubbles for participants, and use the setBackgroundResource method in your Adapter to set the correct background.
Further, if you wish to align them left or right, like in the message app, you will need to add spacers to the left and right of your TextView in the layout file. I used FrameLayouts with a fixed width. Make sure to set their visibility to GONE.
As with swapping the different bubble colors, just set the visibility of the left/right spacer.

Categories

Resources