How to click Android phone "home" button programmatically in espresso - java

I need to simulate home button click in Espresso on Android phone.
I tried
onView(withId(android.R.id.home)).perform(click());
and onView(withContentDescription("Navigate up")).perform(click());
as some posts suggested but it always fails to find the view.
I'm new to Espresso and not sure how to debug it. Thanks.

Better to use withContentDescription(R.string.abc_action_bar_up_description) rather than "Navigate up" but it doesn't act like a home button click anyway, it only uses the navigation bar's "back" button, so it would only work if you have it in your app.
If you want to simulate the home button click and you are using the UI Automator library, you can simulate it like this
fun pressHome() {
// Might be a good idea to initialize it somewhere else
val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
uiDevice.pressHome()
}
Alternatively, if you don't want to or can't use UI Automator, you might try calling Intrstumentation.sendKeyDownUpSync() with the KeyEvent.KEYCODE_HOME.

Related

How to implement dialog that help users to know the app features?

I need to Implement the dialoges given in Image to introduce attributes of App, I couldn't find the way to implement it yet.
I have tried Firebase InAppMessaging but it's automatically being displayed just after the App starts. What I need is the Message dialog should display after a button gets clicked.
the code I putted in the button.OnclickListener
FirebaseInAppMessaging.getInstance().triggerEvent("CompaignId");
Link to Firebase InAppMessaging Docs from where I get the given code
You can use BubbleShowCase to achieve your dialog.
How to use it in the simplest way:
BubbleShowCaseBuilder(this) //Activity instance
.title("foo") //Any title for the bubble view
.targetView(view) //View to point out
.show() //Display the ShowCase
And if you will read more in the library page you can find info about making a custom dialog.
You can use full-screen dialog with transparent background and show whatever feature you have to show
e.g
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
dialog.show()
There's a lot of libraries out there, but if you want a google like showcase, you can use ShowCaseView
ShowcaseViewBuilder showcaseViewBuilder;
showcaseViewBuilder = ShowcaseViewBuilder.init(this)
.setTargetView(fab)
.setBackgroundOverlayColor(0xdd4d4d4d)
.setRingColor(0xcc8e8e8e)
.setRingWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()))
.setMarkerDrawable(getResources().getDrawable(R.drawable.arrow_up), Gravity.LEFT)
.addCustomView(R.layout.description_view, Gravity.TOP)
.addCustomView(R.layout.skip_layout)
.setCustomViewMargin((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, getResources().getDisplayMetrics()));

Remove Recent Apps Button From Navigation Bar

I am currently developing an app and I would like to have a little more control over it then usual without having to root the device.
I would like to remove the capability of the recent apps button in the navigation bar, or at least make it do something else from the default actions. Is there a way to do this? I'm sure there is since SureLock does the same thing.
Thanks
I have found a workaround for this on this website:http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/
you need to add this permission: android.permission.REORDER_TASKS
And add this code:
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if( !hasFocus)
{
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_WITH_HOME );
sendBroadcast( new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) );
}
}
It is not possible to override the recent apps button.
There is no KeyEvent like there is for the Back Button, and as such this feature is not available.
See documentation here: http://developer.android.com/reference/android/view/KeyEvent.html
You may not be able to disable a button, but you can disable the app that is associated with it. I don't know how it is done, but I have seen kiosk app (for non-rooted devices) that disallow other apps from loading.

Calling UIWebView method from Another Class

OK, I'm sure it's simple but I will try to explain everything I'm doing in detail so someone could show me my mistakes. Xcode, iOS SDK 6.1, I'm using StoryBoard.
-I have my MenuViewController (View Controller) with some buttons. In Identity inspector I have set my custom class. Every button has it's outlet in MenuViewController.h (using Ctrl drag). Every button is synthesized in MenuController.m and only thing I'm doing so far is setting background in -viewDidLoad Method in MenuViewController.m
-I have another WebViewController (View Controller with Navigation Bar, Bar Button and UIWebView). I have also created new class for that ViewController and set it in Identity inspector. WebView has it's outlet in WebViewController.h and it's synthesized in WebViewController.m
-I have made connection between those two ViewControllers also through Xcode. One button from my MenuViewController is opening WebViewController (I'v done that by Ctrl+Dragging that button to WebViewController and style is set to Modal). I'v done the same thing with button in NavigationBar of my WebViewController, made connection that is opening MenuViewController when clicked (something like Back button).
-Right now, that WebView is opening some webpage when -viewDidLoad method in WebViewController is called.
What I WOULD LIKE TO DO is to set buttonPressed actions in my MenuViewController and for example, if one button is pressed, I want to switch to WebViewController and open some specific URL. If another button is pressed I want also to open that same WebViewController and load some different URL. This should not be hard but I would just like someone to show me the right way for calling method of webView from another class. I'm new in Objective C and only language I know so far is Java, so I'm a bit confused with this .h .m stuff, not sure where to declare what properly. Also if someone could compare what would something here in Objective-C look in Java, that would be great for understanding.
Thanks folks!
I dont use storyboard and do some thing like this:
In your WebViewController .m file make a method some thing like this:
-(id)initWithURL:(NSString *)urlstring
{
self = [super init];
receivedURL = [NSURL URLWithString:urlstring];
return self;
}
and in .h file declare an object of NSURL
NSURL *receivedURL;
and just above the #end
-(id)initWithURL:(NSString *)urlstring;
and in your viewDidLoad Method of WebViewController do
[webview loadRequest:[NSURLRequest requestWithURL:receivedURL]];
Now in your MenuViewController when ever the button action is performed do something like this:
WebViewController *webViewObj = [[WebViewController alloc]initWithURL:yourURLString];
[self presentModalViewController:webViewObj animated:YES];

Disabling Android Button depending on Permissions

I have an android app that uses the permission "CALL_PHONE". This simple app would just contain a button that would use the call intent to call a specific number. I would like to install this app on both tablets and phone but when it is installed on the tablet, I would like the button to be disabled during runtime so errors wouldn't show when the user tries to call using the tablet without a call function.
At the moment, I am using the setEnabled() and setClickable() method in my MainActivity.java and setting it to false when the user clicks on the button the first time. My question is whether the button can be disabled and the text changed during runtime or when the app is first opened (in a tablet) so the user wouldn't have to click the button first for it to show that the "call" button should be disabled and unclickable?
Refer to this
That will help you in identifying that your application is running on tablet. Now as for disabling your button, I would suggest something like this:
onCreate()
{
setContentView(R.layout.main);
boolean isTablet = checkDevice();
callBtn = (Button) findViewById(R.id.call);
if (isTablet)
{
callBtn.setEnabled(false);
callBtn.setText("Not allowed to make a call");
}
callBtn.setOnClickListener( new onClickListener(){
//Make a call
});
}
public boolean isTablet()
{
//Code for identifying. Return true if application is running on tablet
//return false otherwise
}
So you won't have to wait for user's click on Call button to disable it in tablet.
Hope that helps.
Use button.setEnabled(false); to make visible but user cant click and
button.setVisibility(View.GONE); to make button invisible.and button.setText("YOUR_NEW_TEXT"); to change the button text runtime
And this is not depend on the size of the screen.
Is this you wanted?? OR be more specific with your queston.
... the text changed during runtime?
You can use the setText(); method.
About the other part of your question, you need first to define "What is a tablet?". Is it a 7", 8", 10" screen? Is it a mdpi, hdpi, xhdpi screen? Is it a device which is able to do phone calls? What is a tablet for you or your project? Depending on your answer, you can filter your code (or xml in folders) to make them work the way you want.

TabHost to launch external browser when clicked

I have some tabs in my app and I want the last tab to launch google in the default system browser. I thought this would work:
Uri uri = Uri.parse("http://www.google.com/");
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Google", res.getDrawable(R.drawable.google)).setContent(new Intent(Intent.ACTION_VIEW, uri)));
But it results in a force close error. Any tips on getting this working?
EDIT
I solved this. Basically what I do is add an onClick event handler to capture when the tab is clicked in the first place (only this tab in question) and then from within that I prevent the default action by returning true (for handled) after launching a new Intent in the regular fashion.
You can start an Activity from Tab Host(that you have mention as last Tab Host).Then from that activity you can launch external Browser.As i think its not possible to launch default activity from TabHost.
Edited
I have checked it.It give ActivityNotFound Exception.Conclusion is that TabHost look for the activity that is registered in Android manifest.If you want to achieve it then go with my first suggestion

Categories

Resources