I want to disable(not show) the Dialog, when i click the EditTextPreference in a Preference Activity.
When i click the EditTextPreference, a pop up dialog show. Now i want to disable this. I try set an onclicklistener, but the dialog show same.
public class Settings extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
EditTextPreference userName=(EditTextPreference) findPreference("prefUsername");
userName.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
}); ...
I try add this to the EditTextPreference: android:clickable="false", but nothing.
Disable it.
Use
android:enabled="false"
in XML. Or to disable in runtime,
userName.setEnabled(false);
Actually, if you do not want a dialog, you probably should use simple Preference instead of EditTextPreference.
try to replace
EditTextPreference
with
Preference
so your code will be like this
<Preference
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Version "
android:key="edit_text_preference_1"
android:summary="1.0"
android:enabled="true" />
hope this help you
Related
I'm new to Android developing and now I'm trying to simulate click on my AutoCompleteTextView object.
I'm expecting default android's keyboard appearance with the possibility to type something at the element
Here is a simple function, where I'm trying to perform it:
private void someTestMethodName() {
AutoCompleteTextView tagSearchInput = findViewById(R.id.autoCompleteTextView);
tagSearchInput.performClick();
}
And here is .xml element defining:
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:text="TextView"
android:layout_width="188dp"
android:layout_height="62dp"
android:layout_alignParentStart="true"
android:layout_marginStart="108dp"
android:layout_alignParentTop="true"
android:layout_marginTop="292dp"/>
Calling performClick on a TextView does not pop up the soft keyboard, but you can quite easily do that yourself:
private void someTestMethodName() {
AutoCompleteTextView tagSearchInput = findViewById(R.id.autoCompleteTextView);
showSoftKeyboard(tagSearchInput);
}
public void showSoftKeyboard(View view){
if(view.requestFocus()){
InputMethodManager imm =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
}
}
More information can be found here: https://github.com/codepath/android_guides/wiki/Working-with-the-Soft-Keyboard
i never used performClick, you cant use setOnClickListener to catch a click
tagSearchInput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do somthing
}
});
Basically, the problem is pretty straight-forward if you read the title.
I started my test application for testing purposes and when I'm at the main menu, there is that bar with the application name above and when I click on any button that triggers a new activity, the bar disappears.
http://i.stack.imgur.com/uI70f.png
After clicking, in this example, "What's the meaning of life?" button, the result looks like this
http://i.stack.imgur.com/7mvmY.png
The codes that are used in order to make the button are as follows:
options_menu.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is the meaning of life?"
android:onClick="ShowLifeMeaning"
android:textAllCaps="false"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true" />
Life.java
public class Life extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meaning_of_life);
}
#Override
public void onBackPressed() {
finish();
}
MainActivity.java
public void ShowLifeMeaning(View view) {
startActivity(new Intent(this, Life.class));
}
You can try using
public class Life extends ActionBarActivity
Hi I think it 's because of theme, in your manifest file maybe you right this code for theme :
theme."your theme".NoActionbar or something like this.
if you remove it , it would be correct
You need to use ActionBarActivity.
But this class being deprecated , you can use AppCompatActivity with which you can support actionBar starting from API 7 and it supports fragments as well.
I'm a Android-Beginner and want to make a if-question in my settings/preferences-Activity.
Here is my Preferences.java:
package net.dominik.genpush;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
...and my Prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pushCheckBox"
android:title="#string/pushnachr_headline"
android:summary="#string/pushnachr_explain"
android:defaultValue="true">
</CheckBoxPreference>
</PreferenceScreen>
Now I wanted to make something like
public void onCheckboxPush(View b)
{
if (pushCheckBox.isChecked())
{
//CODE
}
else...
but don't know how I can start the methode when the checkbox changes.
In a other way I could use
android:onClick="MethodeNameHere" in my activity.xml-File. This don't work in my PreferenceActivity class so no message.
Is there a (easy) way to do something like the "onClick" in a similar manner?
You can set listener for checkbox.
CheckBoxPreference checkBox = (CheckBoxPreference) findPreference("pushCheckBox");
checkBox.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
//your code here
return true;
}
});
According to documentation return true will update the state of the Preference with the new value.
And simply check inside of listener if checkBox is checked.
If you are simply trying to make one attribute depend on another, you can use the android:dependency attribute. Will that do what you want? E.g.,
<CheckBoxPreference
android:key="pushCheckBox"
android:title="#string/pushnachr_headline"
android:summary="#string/pushnachr_explain"
android:defaultValue="true">
<!-- This setting will be disabled if pref_one is not checked -->
<CheckBoxPreference
android:defaultValue="false"
android:key="preference_two"
android:dependency="pushCheckBox"
android:title="Pref Two" />
But, if you really just want to get the click event to do something else, put something like this in the onResume method.
Preference pushCheckBox = findPreference("pushCheckBox");
pushCheckBox.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
// do something here
return false;
}
});
Ok so every post i find i cannot get this to work, i am trying to inside my list preference
Settingsbasic.xml
<ListPreference
android:title="themes"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="#array/Themes"
android:entryValues="#array/list"
android:defaultValue="default" />
Now about as you can see above this is my listpreference inside my settingsbasic.xml file. Now what i need to know how to do is i have 2 java files, my main activity. and my preferences java file. I need to know how i can when the user clicks one of the the entries it does something, likes opens something or changes the ui, just something i think i can take it from there. I just need to know how and where the code would go. inside the main acitivty or preference activity.
here is my preference java file
public class Prefs extends PreferenceActivity {
ListPreference listPref;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settingsbasic);
}
protected void onResume() {
super.onResume();
// Registers a callback to be invoked whenever a user changes a preference.
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
protected void onPause() {
super.onPause();
// Unregisters the listener set in onResume().
// It's best practice to unregister listeners when your app isn't using them to cut down on
// unnecessary system overhead. You do this in onPause().
getPreferenceScreen()
.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Sets refreshDisplay to true so that when the user returns to the main
// activity, the display refreshes to reflect the new settings.
WebViewClientDemoActivity.????? = true;
}
}
Any example code would help, or adding on to my code above. I just need someone whom can shed some light on this code. i've tried so many different things and none of it works.
Ok so using the method of the sample app that was recommended below here is some more code i have .
Main Activity
public class WebViewClientDemoActivity extends Activity {
public static String sPref = null;
public void onStart() {
super.onStart();
// Gets the user's network preference settings
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// Retrieves a string value for the preferences. The second parameter
// is the default value to use if a preference value is not found.
sPref = sharedPrefs.getString("listPref", "Default");
}
This is the way I used PreferenceActivity:
public class EditPrefs extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.screen_prefs);
}
}
Then in /res/xml folder I have the XML file:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="txtColor"
android:title="textView Color"
android:summary="select color for textViews"
android:entries="#array/txtColor"
android:entryValues="#array/txtColorValues" />
</PreferenceScreen>
And in /res/values I have this XML file includes items and their values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="txtColor">
<item>blue</item>
<item>brown</item>
<item>gray</item>
<item>violet</item>
</string-array>
<string-array name="txtColorValues">
<item>#ff000099</item>
<item>#5F1E02</item>
<item>#333333</item>
<item>#421C52</item>
</string-array>
</resources>
Then I easily call this class from another android class for instance when user clicks on a menu item:
startActivity(new Intent(this, EditPrefs.class));
You can call the preferences in onCreate and onResume like:
#Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String txtColor = prefs.getString("txtColor", DEFAULT COLOR); // for instanse : #ff000099
textView.setBackgroundColor(Color.parseColor(txtColor));
}
I have resolved my question by this.
Prefs
public static String theme = "Theme1";
#Override
public void onBackPressed() {
ListPreference listPreference = (ListPreference) findPreference("listPref");
String currValue = listPreference.getValue();
theme = currValue;
super.onBackPressed();
}
Main Activity
if (Prefs.theme.equals("Theme1"))
setContentView(R.layout.main);
else
setContentView(R.layout.main2);
Preference XML
<ListPreference
android:title="themes"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="#array/listReturnValue"
android:entryValues="#array/listDisplayWord" />
Here is my problem. I setup the buttons exactly the way they are setup in the Android documentation, but I am getting a warning, and the button will not do anything.
Here is my Java code:
package com.variDice;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
public class VariDiceActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//die1Clicked();
}
private void die1Clicked() {
ImageButton die1button = (ImageButton)findViewById(R.id.die1button);
die1button.setImageResource(R.drawable.icon);
}
}
...and the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1" android:layout_gravity="center_horizontal">
<ImageView
android:id="#+id/varidice_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/icon"></ImageView>
<ImageButton
android:id="#+id/die1button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#null"></ImageButton>
</LinearLayout>
...and the warning:
The method die1Clicked from the type VariDiceActivity is never used locally.
I must say that I am completely new to Android development. I made my app for the iPhone, and I am now trying to make a version for the android. The iPhone version was sooo much easier, because of the better interface builder (so I can just make an action and connect it to the button that way), so this is almost impossibly hard for me to understand. In other words, I do not understand how you connect an action to the button. Could somebody please tell me what I am doing wrong?
Try this in your xml:
<ImageButton
android:id="#+id/die1button"
android:onClick="die1Clicked"
...></ImageButton>
And in your code, change the method signature to:
public void die1Clicked(android.view.View v) {
ImageButton die1button = (ImageButton)findViewById(R.id.die1button);
die1button.setImageResource(R.drawable.icon);
}
Here is the Android Button tutorial.
To bind some behavior to an UI button, you need to register a listener that receives notifications of a certain event type. In your case, you register a OnClickListener (for the click event); just like in the following snippet:
// create the implementation of OnClickListener
private OnClickListener mDie1Listener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// get the button from layout
Button button = (Button)findViewById(R.id.die1button);
// register the onClick listener with the implementation above
button.setOnClickListener(mDie1Listener);
...
}
You need to add a click listener to your button. Put this in your onCreate():
ImageButton die1button = (ImageButton)findViewById(R.id.die1button);
die1button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// What to do when the button is clicked
});
Most answers on SO tend to use 'setOnClickListener' instead of using xml properties.
I personally prefer using xml for making items clickable in android.
The mistake you have made is setting your function as private. The function which gets called after clicking the item should be public.
There are 3 things you should keep in mind:
Define the 2 properties in xml.
android:clickable="true"
android:onClick="functionName"
Define that function in the Activity file. Make sure to keep the function public.
public void functionName(View v) {
// TODO Auto-generated method stub
}
Make sure to pass 'View v' as an argument for that function.