How can I pass a setting preference into a Toast - android

I have set my Setting activity and my Menu (I did the old way without fragments since Im working on API 10) my Menu got 2 ítems Settings (which goes to the Activity Settings just fine) and Show Settings, this one has to show into a custom Toast the Setting Values. This is my code so far
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.config:
startActivity(new Intent(MainActivity.this,
OpcionesActivity.class
));
return true;
case R.id.mconfig:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));
TextView text = (TextView) layout.findViewById(R.id.text);
TextView text1 = (TextView) layout.findViewById(R.id.text1);
TextView text2 = (TextView) layout.findViewById(R.id.text2);
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
And this is my custom Toast XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_layout_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
</LinearLayout>
How can I Access to the setting values and what method should I use? .setText();
I have an OpcionesActivity class that looks like this:
public class OpcionesActivity extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
And settings.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General">
<CheckBoxPreference
android:key="opcion1"
android:title="Sonido"
android:summary="Sonido" />
<EditTextPreference
android:key="opcion2"
android:title="Usuario"
android:summary="Nombre de usuario"
android:dialogTitle="Nombre" />
<ListPreference
android:key="opcion3"
android:title="Dificultad"
android:summary="Dificultad"
android:dialogTitle="Dificultad"
android:entries="#array/d"
android:entryValues="#array/dif" />
</PreferenceCategory>
</PreferenceScreen>

add this before you create a toast
SharedPreferences preferences = getApplication().getSharedPreferences("your prefrens name",MODE_PRIVATE);
text.setText(preferences.getString("key","defaultValue"));
text1.setText(preferences.getString("key","defaultValue"));
text2.setText(preferences.getString("key","defaultValue"));

Related

How to reach Custom Preferences items from Fragment?

In my application Settings Layout i need custom preference for profile Category. I searched and done something .
pref.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Profil">
<Preference
android:key="custom"
android:title="My Custom Preferenece"
android:layout="#layout/custom">
</Preference>
<Preference
android:icon="#drawable/transparency"
android:key="email"
android:selectable="false"
android:summary=""
android:title="Email"
/>
custom.xml (i used for preference layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#android:color/white"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
>
<android.support.v7.widget.AppCompatImageView
android:id="#+id/custom_image"
android:layout_width="100dp"
android:layout_height="50dp"
android:src="#drawable/no_photo"
android:textColor="#color/black"
/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/custom_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Custom Text"
android:textColor="#color/black"
android:gravity="center"
/>
</LinearLayout>
I want to reach and set android:id="#+id/custom_image" or android:id="#+id/custom_text" , i didn't reach these items from my fragment.
How can i reach these items from fragment ?
Thank you
My fragment
public class GeneralPreferenceFragmentN extends PreferenceFragment {
private Context mContext;
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_main);
mContext = getActivity().getApplicationContext();
bindPreferenceSummaryToValue(findPreference("phone"));
private static void bindPreferenceSummaryToValue(Preference preference)
{
preference.setOnPreferenceChangeListener(
sBindPreferenceSummaryToValueListener);
SharedPreferences prefs = preference.getSharedPreferences();
Object value;
if (preference instanceof MultiSelectListPreference) {
value = prefs.getStringSet(preference.getKey(), new HashSet<String>
());
} else {
value = prefs.getString(preference.getKey(), "");
}
..................
Try this:
in your onCreate() initialize the view:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_abschliessen_einsatz, container, false);
//#INFO: Create a global View v element if you want to use the view in other funtions (for example on slected and so on). Otherwise just use
this.v = v;
//#INFO: Otherwise just use th View elemnt to to find by ID
myCustomImage = (AppCompatImageView) v.findViewById(R.id.custom_image);
myCustomText = (AppCompatTextView) v.findViewById(R.id.custom_text);
//Set Text or do stuff with the elements afterwards
return v;
}
//Use the view in other fuctions, ex. onSelcted
#Override
public void onSelected() {
//update UI when selected
//#INFO: Use the global View elemnt to to find by ID
myCustomImage = (AppCompatImageView) v.findViewById(R.id.custom_image);
myCustomText = (AppCompatTextView) v.findViewById(R.id.custom_text);
//Set Text or do stuff with the elements afterwards
}

Preference with custom layout not clickable

I'm new to android development. I have created a PreferenceFragment. there is a button(Preference) bottom of the page.after adding the custom layout to the button, the button is not clickable. But outside the button(within preference is clickable) please help me to solve this issue
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="#string/header_user_detail"
android:title="YOUR DETAIL">
<EditTextPreference
android:key="user_name"
android:title="#string/user_name"></EditTextPreference>
<com.empite.oneonus.helpers.custom_preferences.DatePreference
android:key="dob"
android:title="#string/date_of_birth"></com.empite.oneonus.helpers.custom_preferences.DatePreference>
<EditTextPreference
android:key="work_postcode"
android:title="#string/work_postcode"></EditTextPreference>
<EditTextPreference
android:key="home_postcode"
android:title="#string/home_postcode"></EditTextPreference>
<ListPreference
android:dialogTitle="Application language"
android:key="work_category"
android:summary="Select the Application language"
android:title="Language"></ListPreference>
<Preference
android:key="update_profile_key"
android:layout="#layout/update_profile_btn"></Preference>
<Preference
android:key="button"
android:summary="This will act like a button"
android:title="Acts like a button" />
</PreferenceCategory>
<PreferenceCategory
android:key="#string/header_user_account"
android:title="YOUR ACCOUNT">
<EditTextPreference
android:key="edit_email"
android:layout="#layout/edit_email_address"></EditTextPreference>
<EditTextPreference
android:key="edit_password"
android:layout="#layout/edit_password"></EditTextPreference>
</PreferenceCategory>
layout/update_profile_btn
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/update_profile_btn"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="10dp"
android:background="#color/btn_light_green_color"
android:focusable="false"
android:text="Update Profile"
android:textAllCaps="false"
android:textColor="#color/white"></Button>
public class UpdateProfilePrefFragment extends com.github.machinarius.preferencefragment.PreferenceFragment implements Preference.OnPreferenceClickListener {
protected static void setListPreferenceData(ListPreference lp) {
CharSequence[] entries = {"English", "French"};
CharSequence[] entryValues = {"1", "2"};
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.update_profile);
final ListPreference listPreference = (ListPreference) findPreference("work_category");
setListPreferenceData(listPreference);
listPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
setListPreferenceData(listPreference);
return false;
}
});
Preference pref = findPreference("update_profile_key");
pref.setOnPreferenceClickListener(this);
Preference button = (Preference) findPreference("button");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
Toast.makeText(getActivity(), "button", Toast.LENGTH_SHORT).show();
return true;
}
});
}
#Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(getActivity(), "Hello", Toast.LENGTH_SHORT).show();
return false;
}
}
Your button custom preference does not specify a layout. You may need to specify a layout and set android:focusable="false". See Custom preference not clickable
Edit: For the #layout/update_profile_btn file, you may try the following modifications:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false">
<Button
android:id="#+id/update_profile_btn"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="10dp"
android:background="#color/btn_light_green_color"
android:onClick="onButtonClick"
android:text="Update Profile"
android:textAllCaps="false"
android:textColor="#color/white"></Button>
</RelativeLayout>
and define the method
void onButtonClick(View v) {
Toast.makeText(getActivity(), "Hello", Toast.LENGTH_SHORT).show();
}

Show copy/paste context menu on text using code android

I have an EditText and its text gets selected from through code. But I want to allow users to cut/copy the selected text. However, the cut/copy context menu doesn't appear until user long presses on text. But then it loses the actual selection. So, I'm thinking to show the context menu as the text get selected by the code.
I tried this inside onFocusChanged, but nothing appeared.
openContextMenu(EditText);
IF I follow you usecase correctly, you can open context menu from the onFocusChangeListener registered on the testedEditText.
I prepared some small test for that which seems to be correctly supporting your usecase.
You need to openMenu on the hooks which are selecting the content in the EditText.
public class Main extends Activity {
private EditText testedEditText;
private Button selectingButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selectingButton = (Button) findViewById(R.id.button);
testedEditText = (EditText) findViewById(R.id.textView);
registerForContextMenu(testedEditText);
selectingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
testedEditText.setSelection(6, 11);
openContextMenu(testedEditText);
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_all:
return true;
case R.id.copy:
//do something
return true;
case R.id.cut:
//do something
return true;
case R.id.paste:
//do something
return true;
default:
return super.onContextItemSelected(item);
}
}
}
Weirdly enough registering testedEditText.requestFocus(), and setting onFocusChangedListener for EditText was not enough.
Additional xml files for reference:
cmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/select_all"
android:title="select all"
/>
<item android:id="#+id/copy"
android:title="copy"
/>
<item android:id="#+id/cut"
android:title="cut"
/>
<item android:id="#+id/paste"
android:title="paste"
/>
</menu>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World, Initial Text..."
android:layout_centerInParent="true"
/>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="button"
/>
</RelativeLayout>

Button not working when restored using SharedPreferences

I have an app which inflates button views on a click of a button(Button A). The button views inflated work properly, executing the code in their onClick method.
Now to save the layout generated, I keep a count of number of times the Button A is clicked and save this value using SharedPreferences. In onCreate method I create a for loop for the number of times the Button A was clicked and re-inflate those buttons, thus generating the same layout that was on screen when the app was killed. Though now, the buttons generated in the onCreate method no longer function.
I hope I have made some sense. The problem, when the app is started again in future, it builds the layout as it was when the app was clicked, but the button views no longer function for the onCLick method.
Here's the code for reference:
MainActivity.java
public class MainActivity extends Activity {
EditText et;
String z, r;
LinearLayout ll;
SharedPreferences sp;
Button fb1;
int c=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.et);
sp=getSharedPreferences("MY_PREFS", 0);
if (sp.contains("counter")) {
for (int i=1;i<=sp.getInt("counter", 1);i++) {
cr();}
}
c=sp.getInt("counter", 0);
}
public void cr() {
ll=(LinearLayout) findViewById(R.id.ll);
LayoutInflater li=getLayoutInflater();
View vw=li.inflate(R.layout.infla, null);
ll.addView(vw);
fb1=(Button) vw.findViewById(R.id.btn);
fb1.setId(c);
}
public void click(View v) {
int z=v.getId();
switch (z) {
case (R.id.button):
et=(EditText) findViewById(R.id.et);
cr();
et.setText("");
c=c+1;
break;
}
Editor e=sp.edit();
//e.putString("check", r);
e.putInt("counter", c);
//e.putInt("id", c);
e.commit();
for (int i=0; i<=c;i++){
if (z%2==0) {//Present
et.setText("Present");}
else if(z%2==1)
et.setText("Absent");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll"
tools:context=".MainActivity" >
<EditText
android:id="#+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:ems="10"
android:hint="Enter something" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:onClick="click"
android:text="Click" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remove" />
</LinearLayout>
infla.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="click" />
</LinearLayout>
Edit:
You seem to have assigned redundant id's to the button during initialization.Each button would be assigned the value 'c' instead of 0 to 'c'.
Additionally It seems only logical to create click listeners on the fly as well ,since you are inflating button on the go.Try implementing the onclicklistener instead of the hardwired click() method in the xml.
i.e
public void cr() {
ll=(LinearLayout) findViewById(R.id.ll);
LayoutInflater li=getLayoutInflater();
View vw=li.inflate(R.layout.infla, null);
ll.addView(vw);
fb1=(Button) vw.findViewById(R.id.btn);
fb1.setId(c);
fb1.setOnClickListener(new View.OnClickListener(){
//Stuff you wish to do on click
});
}

Unclickable button set on a preference activity

I just set a button on a preference activity from one the answers from this question. My main issue is that this button is unclickable while the other elements of the preference activity is click able.
I created a simple example to demonstrate my dilemma it should show the same symptoms as you copy and paste.
public class preferenceTest extends PreferenceActivity{
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "I got clicked", Toast.LENGTH_SHORT).show();
}
});
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preftest);
// Do Stuff
}
}
}
res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#android:color/black"
android:layout_weight="10"/>
<Button android:text="This is a button on bottom of all preferences."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:textColor="#android:color/black"
android:layout_weight="1"/>
</LinearLayout>
res\xml\preftest.xml
<PreferenceCategory
android:title="New Title"
android:summary="Title">
<ListPreference
android:key="list1"
android:title="List one"
android:summary="List1"
/>
<ListPreference
android:key="list2"
android:title="List two"
android:summary="List2"/>
</PreferenceCategory>
<CheckBoxPreference
android:key="check1"
android:title="check1"
android:summary="CheckBox Test"/>
</PreferenceScreen>
I'm not sure why you would want to add a button in a PreferenceActivity, but what you can do is add a plain Preference and then make that clickable through the activity code.
For instance, on your preferences.xml
<Preference
android:key="#string/pref_key_dummy_pref_button"
android:title="#string/pref_title_dummy_pref_button" />
Then, on your PreferenceActivity, create a Preference object:
Preference mDummyButtonPref;
Initialize it from onCreate:
addPreferencesFromResource(R.xml.preferences);
mDummyButtonPref= findPreference(getString(R.string.pref_key_dummy_pref_button));
Then, add override onPreferenceTreeClicked to handle clicks:
#Override
#Deprecated
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
if (preference == mDummyButtonPref) {
Log.v(TAG, "Dummy got clicked");
}
}

Categories

Resources