Android ZoomControl layout - java

I'm testing my android app with espresso and want to test the ZoomControls like this:
onView(withId(R.id.zoomIn)).perform(click());
But I don't get the zoomIn/zoomOut id, from the Zoom_Controls. In the Layout Component Tree, the zoomIn and zoomOut are visible.
Can someone help me?
Thanks in advance.
Resources res = Resources.getSystem();
int id = res.getIdentifier("btn_zoom_down", "drawable", "android");
Didn't work.
Got it working with:
Resources res = Resources.getSystem();
int id = res.getIdentifier("#id/zoomOut", "drawable", "android");
onView(withId(id)).perform(click());

Related

App language (Locale) automatically gets reset to default after opening camera to capture a photo or gallery to select image

We have a setLocale function as following:
public static void setLocale(Context context, String locale) {
Locale myLocale = new Locale(locale);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
We use above method as follows:
sessionManager.setAppLanguage(locale, this);
setLocale(context,sessionManager.getAppLanguage(this));
We are using following locales:
English = "en";
Hindi = "hi";
Marathi = "ma";
But everytime when we open a camera, capture a photo, some labels get reset to default language and some labels remain in selected language.
This happens in case of selecting photo from gallery also.
Note: Issue is device specific. Now we are seeing in Samsung star pro (KITKAT 4.1) and Sony Xperia E3 dual (KITKAT 4.4.4)
It might happen that you are calling the setLocale method in your Activity initialization, such as in onCreate method. You might want to call it in onActivityResult also.

Android ViewBadger not showing

I am trying to use a badge in android by exploiting https://github.com/jgilfelt/android-viewbadger
To this end I am inflating a TextView like
TextView target = (TextView) rowView.findViewById(R.id.menu_item_badge);
BadgeView badge = new BadgeView(this.getContext(), target);
badge.setText("1");
int droidGreen = Color.parseColor("#A4C639");
badge.setBadgeBackgroundColor(droidGreen);
badge.show();
The problem is I can't see the badge

Set Background Drawable Resource from String

I have looked everywhere but cannot find an example of programmatically setting a background resource from a string value?
As an example:
Drawable a = getResources().getDrawable( R.drawable.a );
Drawable b = getResources().getDrawable( R.drawable.b );
Drawable c = getResources().getDrawable( R.drawable.c );
abc.setBackgroundResource("b");
Is this possible or would I have to do it as a big switch statement?
you have getResources().getIdentifier for this purpose. It returns the id of the resources from its name.
E.g.:
int resId = getResources().getIdentifier("b", "drawable", getPackageName());
Here you can find the documentation.

Image View set Image Resource in code

I have an imageView that i want to display a little icon of the country that you are currently in. I can get the country code, but problem is i can't dynamically change the imageView resource. My image files are all lowercase
String lowerCountryCode = countryCode.toLowerCase();
String resource = "R.drawable." + lowerCountryCode;
img.setImageResource(resource);
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
setImageResource(id);
abve is my code.
Now, of course this will not work because set Image Resource wants an int, so how can i do this? Thanks in advance
I mean you know the name of files(Drawable) which you wanna use for one and other country.
ImageView imgView=(ImageView)findViewById(R.id.imageView1);
int id = getResources().getIdentifier(lowerCountryCode.toLowerCase(), "drawable", getPackageName());
imgView.setImageDrawable(res.getDrawable(id));
The following code should work well (assuming that you really have a file with name lowerCountryCode in your res/drawable/ folder)
String lowerCountryCode = countryCode.toLowerCase();
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
ivCard[0].setImageResource(id);
i use that code....
ImageView[] ivCard = new ImageView[1];
#override
protected void onCreate(Bundle savedInstanceState)
ivCard[0]=(ImageView)findViewById(R.id.imageView1);

Android Webview, scale content to fit screen

My friend made an application using javascript, and uploaded it to his website.
Now I'm trying to wrap it into a webview in android, and that's working fine in some ways.
The page is 480x320
But no matter what screensize I select on Android, there is a white space at the bottom on the webview. I have tried a lot of ways to make it zoom, but nothing worked.
My code at this moment is this
final WebView browser = (WebView)findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl("http://page.xx");
I needed to scale-to-fit for the width and this variation of FunkTheMonk's answer worked well for me:
#Override
public void onPageFinished(android.webkit.WebView view, String url)
{
super.onPageFinished(view, url);
WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
metrics.widthPixels /= metrics.density;
loadUrl("javascript:var scale = " + metrics.widthPixels + " / document.body.scrollWidth; document.body.style.zoom = scale;");
}
wb.loadUrl("javascript:document.body.style.zoom = "+String.valueOf(scale)+";");
Where scale is a float, which you could calculate - I think in your case you want something like the following: browser.getHeight() / 480dp.
Load this Url after your webpage has finished loading.
Can you try to add :
browser.getSettings().setBuiltInZoomControls(true);
browser.setInitialScale(1);
browser.setPadding(0, 0, 0, 0);
And see if it's working ?

Categories

Resources