Specifying drawable resource id in a separate xml - android

I have an xml file that specifies a set of image button names. Also, I would like to specify the image resource id as an attribute of an xml node as shown below:
<button name="close" resLocation="R.drawable.close" />
I am parsing the xml in the code, I would like to set the image background for the dynamically created button using the resLocation attribute. Since resLocation is a string, I cannot convert to a Drawable object directly. Is there a way I can workaround?

You can use get getResources().getIdentifier:
String myResourceId = "close"; // Parsed from XML in your case
getResources().getIdentifier(myResourceId, "drawable", "com.my.package.name");
This would require your XML to be a little different:
<button name="close" resLocation="close" />
If you need to keep the R.type.id format in your XML, then you would just need to parse out the type and id:
String myResourceId = "R.drawable.close";
String[] resourceParts = myResourceId.split("\\.");
getResources().getIdentifier(resourceParts[2], resourceParts[1], "com.my.package.name");

You can try
<button name="close" resLocation="#drawable/close" />
or try
ImageButton imgButton=new ImageButton(this);
imgButton.setImageResource(getResources().getDrawable(R.drawable.close));

Related

Android: get Embedded Resource image as drawable inside xml layout file

Is there a way to set as android:src an embedded resource image with Xamarin Forms?
You could not set the embedded resource from Forms project for it inside xml layout file via android:src.
android:src is used to set a drawable as the content. May be a reference to another resource, in the form #[+][package:]type/name or a theme attribute in the form ?[package:]type/name. May be a color value, in the form of #rgb, #argb, #rrggbb, or #aarrggbb.
But you could get set the embedded resource in code behind.
A simple ImageView inside the layout. The code in Activity:
SetContentView(Resource.Layout.layout3);
var imagview = FindViewById<ImageView>(Resource.Id.imageView1);
#if __ANDROID__
var resourcePrefix = "App3.";
#endif
// note that the prefix includes the trailing period '.' that is required
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream
(resourcePrefix + "tiger_24.png");
Android.Graphics.Bitmap bitmap;
var bitmap2 = BitmapFactory.DecodeStream(stream);
imagview.SetImageBitmap(bitmap2);

How to write the Bitcoin logo as text?

I am developing an android app and I want to write the Bitcoin symbol in a TextView.
Is there any code for that like "\u20B9" for Rupees?
Android supports Bitcoin Unicode symbol since Android O, you can read more about it from this link. Just try to run your code in Android O and I'm sure that everything will be fine.
But if still want to use TextView, to show BTC symbol, consider FontAwesome, it's actually designed for WEB, but it also possible to use in Android.
Download icons pack.
Put ttf font file to your assets folder
Then get Typeface object and set it to your TextView:
String fontName = "fa-brands-400.ttf";
Typeface fontAwesome = Typeface.createFromAsset(getAssets(), "fonts/" + fontName);
textView.setTypeface(fontAwesome);
Add string XML resource with FontAwesome code point of BTC symbol:
<string name="btc_fa"></string>
Set this string to TextView:
textView.setText(R.string.btc_fa);
As the result, you will get this:
Create a string in strings.xml like the following one:
<string name="bitCoin">\u20BF</string>
Use the following to create the textView with BitCoin Symbol:
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/bitCoin"/>
You will have your bitcoin symbol in textView like below

Ruby Selenium unable to select image from android image gallery

I am trying to select an image from Android image galley grid view. This grid view has ImageView for every image. Every ImageView has id.
So when i am trying to get by //ImageView[#id="someId"][1] or by //GridView[#id="someId"]//ImageView[#id="someId"][1], it does not find it.
How can I select image from gallery using Selenium webdriver in ruby.
EDIT:
<GridView id = "someId">
<LinearView>
<ImageView id = "someImageId"></ImageView>
</LinearView>
<LinearView>
<ImageView id = "someImageId"></ImageView>
</LinearView>
<LinearView>
<ImageView id = "someImageId"></ImageView>
</LinearView>
<LinearView>
<ImageView id = "someImageId"></ImageView>
</LinearView>
</GridView>
EDIT
As Uiautomator cannot select the thumbnails in Gallery, I tried to pick image using relative co ordinates but this is giving an error.
Is any one has done this? I need to select image from android gallery.
If you want the first image (ImageView) :
//*[#id='someId']/LinearView[1]/ImageView
Output :
<ImageView id="someImageId"/>
You can change the index in the LienarView array selection.
element = driver.find_element(:xpath, "//*[#id='someId']/LinearView[1]/ImageView")
p element
# <Selenium::WebDriver::Element:0x3eb6a9d8 id="{ea847fbe-7fc7-453b-97ea-74fbf325ddac}">
edit :
You can also fetch all elements using find_elements() into an array and then, juste select the one you want :
elements = driver.find_elements(:xpath, '//ImageView')
p elements[1]
# <Selenium::WebDriver::Element:0x..f9bdc4412 id="{1f41fbd9-0a73-4fb2-9c98-44bb877e2388}">
You should be able to find it with Appium using:
//android.widget.ImageView[#id="someId"][1]
or
//android.widget.GridView[#id="someId"]//android.widget.ImageView[#id="someId"][1]

How do I use MvxImageView.DefaultImagePath?

I have successfully used MvxImageView with ImageUrl, but how can I set a drawable icon as a default for those users which don't have a profilepicture (that is, where ProfileThumbnailImageUrl is empty)?
<MvxImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
local:MvxBind="ImageUrl ProfileThumbnailImageUrl"
DefaultImagePath="#drawable/ic_action_user"/>
If this view is just being used once (e.g. not reused in a list) then you can just use android:src for your default image.
If you need to use DefaultImagePath then you can do this in a binding:
local:MvxBind="ImageUrl ProfileThumbnailImageUrl; DefaultImagePath DefaultImageUrl"
where DefaultImageUrl is a drawable accessed as res:name - e.g. res:ic_action_user, a file path (saved using the File plugin), or an http url
You should also be able to do this as a string literal:
local:MvxBind="ImageUrl ProfileThumbnailImageUrl; DefaultImagePath 'res:ic_action_user'"

Android : display html data

I want to display the content of an html page and an image to the right of that in my view.
So i have defined my layout as
<ScrollView android:id="#+id/scrllvwNo1"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="fill_parent" android:background="#drawable/home_bg">
<ImageView android:id="#+id/aboutcmkimage"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:src="#drawable/about"
android:padding="5dip" />
<WebView android:id="#+id/aboutcmk" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textColor="#000000"
android:layout_toLeftOf="#+id/aboutcmkimage"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true" />
</RelativeLayout>
</ScrollView>
and
I try to load the html page as below
WebView web = (WebView)findViewById(R.id.aboutcmk);
web.loadData(getString(R.layout.about),"texl/html","utf-8");
in this case i am getting the error
"this page contains error at line1 ... "
if I try
web.loadDataWithBaseURL(null,getString(R.layout.about),"texl/html","utf-8",null);
no html output and no error
In both case the image is coming
Can anyone help me in debugging
My html has bullets , so i cannot use textview instead of webview
here is the definition of my htmlstring in xml file
<string name="About"><html><body><b>What is CMK?</b> ......</body></html>
Thanks a lot for the help
are you put this line What is CMK? ...... in strings.xml if yes then the correction is in this line
web.loadDataWithBaseURL(null,getString(R.layout.about),"texl/html","utf-8",null);
Correct one:
web.loadDataWithBaseURL(null, getString(R.string.About), "text/html", "UTF-8", null);
First of all, if you have saved your HTML specification as a string resource, your should access it as R.string.about and not R.layout.about. Change that, if it still doesn't work, try escaping the less than characters in your string, like this :
<string name="about"><html><body><b>What is CMK?</b> ......</body></html> </string>
I think you will have to escape the less than characters. Before loading the text, Log it. You'll see the problem.
Instead of storing the HTML in strings.xml, you can store it in its own file in the res/raw/ directory. For example, let's say you save the file in res/raw/mypage.html. I haven't tested this, but you should be able to open up raw resources and load them up in a WebView like:
try {
Resources resources = getResources();
InputStream inputStream = resources.openRawResource(R.raw.mypage);
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String htmlStr = new String(bytes);
webView.loadData(htmlStr, "text/html", "UTF-8");
} catch(Exception e) {
//blah
}

Categories

Resources