How to implement a context menu for a <Preference> on android? - java

I have a preference stored in variable "preference":
Preference preference = new Preference(this);
I want to add a context menu to be displayed onlongclick. I have already created the menu /res/menu/pref_menu.xml and I tried registerForContextMenu(preference); but it only works on views. how can I implement it with a preference?

I solved it by register in onPostCreate method:
registerForContextMenu(getListView());
And set on the certain preference click listener when need to open:
openContextMenu(getListView());

Related

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.

Add onLongClickListener to tab on TabHost android

On an app that I'm working on, I need a context menu to show up if a user performs a longClick on a tab, which would allow them to close the tab. I can't seem to find a way to add a listener to a tab though. I either need each tab to have its own listener or the listener needs to be able to tell which tab had the longClick performed on it, as it won't always be the active tab.
Any ideas?
I appreciate that an answer has been accepted but if you want to utilise the built-in ContextMenu capabilities rather than set onLongClickListeners on the TabWidget itself, you can do this as follows...
Example, my current TabActivity adds tabs in a for loop and to register each for context menu I do the following.
for (int tabNumber = 1; tabNumber < 8; tabNumber++) {
...
spec = tabHost.newTabSpec(tag).setIndicator(indicator).setContent(intent);
tabHost.addTab(spec);
View v = tabWidget.getChildAt(tabNumber - 1);
registerForContextMenu(v);
...
}
Then in my Activity I simply override onCreateContextMenu(...) and onContextItemSelected (MenuItem item)
#Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {
...
// The parameter v is the actual tab view and not the TabWidget
// this makes it easy to get the indicator text or its tag in order
// to easily identify which tab was long-clicked and build the menu
...
}
#Override
public boolean onContextItemSelected (MenuItem item) {
...
// Process selected item here
...
}
There's no need to set an OnLongClickListener on any views explicitly as that is done by the call to registerForContextMenu(...). Also, the ContextMenu creation and selection handling is all handled for you by the ContextMenu methods exposed by Activity.
Unless you need to handle all of this stuff yourself (for a custom context menu layout for example) it seems easier to just use what's buit-in to Activity.
A TabWidget is a View like any other; you should be able to register an OnLongClickListener with it via myTabWidget.setOnClickListener and use the View argument of OnLongClickListener.onLongClick(View v) to determine which tab was clicked.
When you use a TabSpec to register the indicator for each tab with your TabHost, if the resource you pass in has an associated ID, you should be able to use that ID to look up the tab itself. This may mean you could have to start using Views or layouts as your TabSpec.setContent or TabSpec.setIndicator arguments (if you aren't already) so you can look them up by ID later.

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

Show Tab Widget on each and every Activity

Using Java, how can I show a Tab Widget on each and every Activity, even if that Activity is a subActivity of FirstActivity? If possible, please provide me with some code or examples.
Use THis to start the new Activity
View view = getLocalActivityManager().startActivity("tab1", new Intent(this,tab1.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
setContentView(view);
Create Another GroupActivity and putExtra info along with your intent so when it gets it ,the group activity can check what tab should be opened.Use My code to open the new Tab.

Resetting preferences when using PreferenceScreen

I am loading a PreferenceScreen from an xml file to use as the screen to configure a new event so I'm attempting to clear and reset the values of the SharedPreference this activity is using. The problem I'm encountering is that when attempting to move to using a named preference manager, it seems the preference gets cleared but when I select an EditTextPreference element, the old data is still the default entered text on the popup.
In my onCreate method I'm attempting to initialize the preferences, clear them, then set to default values. My understanding from the dev resources were that there's no way to clear/reset in one step..
private static final String PREFNAME = "newmeetingactivity.preferences";
//load preferences and set name
addPreferencesFromResource(R.layout.newmeeting_preferences);
getPreferenceManager().setSharedPreferencesName(PREFNAME);
getPreferenceManager().setSharedPreferencesMode(MODE_PRIVATE);
//Clear the preferences
_sharedPreferences = getPreferenceManager().getSharedPreferences();
SharedPreferences.Editor ed = _sharedPreferences.edit();
ed.clear();
ed.commit();
//Load default preferences from file again
PreferenceManager.setDefaultValues(this, _sharedPreferences.toString() , MODE_PRIVATE, R.layout.newmeeting_preferences, true);
Edit: To try to better explain what I'm attempting to do (in case my approach is way off): I need to clear shared preferences used on a given activity while not interfering with the settings from other activities (as they should persist indefinitely).
Could you instead try using the PreferenceManager.getDefaultSharedPreferences(context) to get your prefs.
Edit:
adb shell into your application after you choose to reset the values. If you look at the preference file you will see that it's default value has been set. Try refreshing your activity. One way I did this was by simply killing it from the Applications menu. When the activity restarts it will have the expected default value.

Categories

Resources