Disabling Android Button depending on Permissions - java

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.

Related

Kotlin Custom Android Keyboard IME - method onUpdateCursorAnchorInfo from InputMethodService is not working

I'm developing a custom android keyboard in Kotlin and I have looked at this post for how to detect cursor movement/change when the user clicks on text editor.
Here is a snippet of my code of my custom keyboard class which extends InputMethodService.
class CustomKeyboard: InputMethodService() {
override fun onCreateInputView(): View {
// initiated keyboard
}
override fun onStartInputView(info: EditorInfo?, restarting: Boolean) {
// call onUpdateCursorAnchorInfo() whenever cursor/anchor position is changed
this.currentInputConnection.requestCursorUpdates(InputConnection.CURSOR_UPDATE_MONITOR)
Log.i("--------------", "onStartInputView is called")
}
override fun onUpdateCursorAnchorInfo(cursorAnchorInfo: CursorAnchorInfo) {
Log.i("--------------", "onUpdateCursorAnchorInfo is called")
}
}
Then I ran this app on Android Studio emulator and tested on an EditText view in the following way:
I click on the EditText view on my app
I input many random characters in the EditText view
I click randomly on different characters in the EditText view
The system log did print out "onStartInputView is called" when I first clicked on the EditText view.
But it did not print out "onUpdateCursorAnchorInfo is called" anywhere.
What I thought would happen was that "onUpdateCursorAnchorInfo is called" would have been printed multiple times as I clicked on different parts of the EditText view.
However, when I changed to a different app and click on the text editor there, the Log did print out "onStartInputView is called" and "onUpdateCursorAnchorInfo is called" once.
But when I repeat step 1~3 on that app it did not work either
I have no idea why this behaviour occurs.
Any help would be appreciated.
Okay never mind somehow it works now. It must have been a bug or something.

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

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.

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.

The difference between implementation of back and cancelbutton?

I m a newbie an trying to learn Java/Android-programming.
I m doing an app for Android in Eclipse and created some buttons.
I have a back and a cancel button.
Example:
I have a EditText there you can write in your name. If you write yourname and press the backbutton, then u will go back to the previous Activity, but if you go to the same Activity, then you will still see the name that you wrote in the EditText.
But if you press the cancelbutton, you will go back to the previous Activity, but when you come back, yourname will be empty. I will "kill" or "stop" the Activity.
This is the code I use for the Backbutton, what would you use for the Cancel Button?
Thank YOU.
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonBack:
Intent intent = new Intent (AllActivity.this, MenuActivity.class);
startActivity(intent);
break;
For the cancel button you can use the below method, this will kill the activity.
finish()
so in your code it will look something like this:
public void onClick(View v) {
switch(v.getId()){
case R.id.cancel:
finish();
break;
There was little difference in this as per requirement of process or application flow. For cancel and back as work are same for example if you open any dialog and provide cancel button will close/dismiss your dialog same way the back button do this. While for implementing with the Activity you if you implement for closing current activity you can just finish with both option by just calling finish() method. As back button was normally work for finish you current activity and back.
Another way to do this that you may be interested in is to wipe out the content of the EditText yourself.
You would need to have in your xml file an id defined for the EditText so that you could access it programatically.
<EditText
layout stuff here:
android:layout_width="fill_parent"
...
and then the id attribute
android:id="#+id/edit_text_id"
>
then in your code you would put the following in your class (not inside any method):
EditText anEditText;
then in your onCreate(), after the inflation of the layout (if it comes beforehand it will cause the app to crash):
anEditText = (EditText) findViewById(R.id.edit_text_id);
the name edit_text_id is not significant, but it is what we used in the layout file
next add to the onClick method for cancel (after the case statement):
//this wipes the text from the textbox
anEditText.setText("");
// add the rest of the back button code after this and your good!
Best of luck! Remember that we were all newbies once. If you want to be a good android programmer, I suggest that you get a strong background in Java first. This free book helped me very much!
Java Notes

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