Could you give me a short Android code to create this? - android

http://habrastorage.org/storage/211878a0/14f474e6/bc73d2e8/a709e893.gif
how can I utilize the Toast object to ask a question?
I searched around but was not able to find how to make a Toast object ask a question
Thanks a lot Stack Overflow

here is a sample:
the solution is using an alert dialog with a layout that has a label and a text box.
public Dialog create()
{
LayoutInflater li = LayoutInflater.from(this._context);
this._view = li.inflate(R.layout.prompt_dialog, null);
AlertDialog ad = new AlertDialog.Builder(this._context).create();
ad.setView(this._view);
if(this._title != null)
{
ad.setTitle(this._title);
}
if(this._icon != 0)
{
ad.setIcon(this._icon);
}
TextView tv1 = (TextView)this._view.findViewById(R.id.prompt_dialog_message);
tv1.setText(this._message);
ad.setButton(DialogInterface.BUTTON_POSITIVE, _context.getString(R.string.ok), this);
ad.setButton(DialogInterface.BUTTON_NEUTRAL, _context.getString(R.string.cancel), this);
return ad;
}
I've used these in my Emergency Tools App And I didn't had any problems :)

Actually that's just a PreferenceActivity. Take a look at the JavaDoc for http://developer.android.com/reference/android/preference/PreferenceActivity.html

What you see is not a Toast notification.
It is a CustomDialog read more here

This is an EditTextPreference inside a PreferenceActivity
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mypref="http://schemas.android.com/apk/res/com.tneele.daynightlwp"
android:title="#string/settings_title"
android:key="daynightwallpaper_settings">
<EditTextPreference
android:dialogTitle="UserName" />
</PreferenceScreen>
PreferenceActivity:
public class MyPreferenceActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}

Related

How to change imageview background color in a preference in android?

I tried to solve my problem so i changed so much code. I change even title of the post.
I can change the color of imageview background in preferences ui successfully. But after leaving fragment and launch again, the ui can not be updated in the same way as before.
First of all, I use sherlockactionbar . There are 3 tabs . when 3rd bar is pressed, a fragment is , including buttons, is loaded. When one of button is pressed, another preference fragment is loaded.
The code below shows how to call preference fragment when one of the button is pressed :
SettingsMenuFragment.java
public class SettingsMenuFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button ICSbutton= (Button) view.findViewById(R.id.CallSearchSettingsButton);
ICSbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
clearFragmentStack();
SettingsIncomingSearchFragment removeSISF = (SettingsIncomingSearchFragment) getActivity().getSupportFragmentManager().findFragmentByTag("SISF");
if(removeSISF != null)
{
getActivity().getSupportFragmentManager().beginTransaction().remove(removeSISF).commit() ;
getActivity().getSupportFragmentManager().executePendingTransactions();
}
SettingsIncomingSearchFragment Prefrag = new SettingsIncomingSearchFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, Prefrag ,"SISF");
transaction.addToBackStack(null);
transaction.commit();
getActivity().getSupportFragmentManager().executePendingTransactions();
}
} );}}
and the code below shows preferencefragment :
public class SettingsIncomingSearchFragment extends PreferenceListFragment implements SharedPreferences.OnSharedPreferenceChangeListener,PreferenceListFragment.OnPreferenceAttachedListener {
Context ctx ;
public static final String SHARED_PREFS_NAME = "settings";
LinearLayout mainlayout ;
LinearLayout sublayout ;
View view ;
Preference myPref ;
ImageView img ;
SharedPreferences sp ;
#Override
public void onCreate(Bundle icicle) {
ctx = getActivity() ;
super.onCreate(icicle);
addPreferencesFromResource(R.xml.pref_incomingsearchsettings);
myPref = (Preference) findPreference("incomingsearchbackgroundcolor");
setColor() ;
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
HSVColorPickerDialog cpd = new HSVColorPickerDialog( ctx, 0xFF4488CC, new OnColorSelectedListener() {
#Override
public void colorSelected(Integer color)
{
sp.edit().putString("incomingsearchbackgroundcolor", String.valueOf(color)).commit();
}
});
cpd.setTitle( "Pick a color" );
cpd.show();
return true ;
}
});
}
private void setColor() {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rectangle_layout, null);
mainlayout = (LinearLayout)view.findViewById(R.id.rectangle_main_layout_ll);
sublayout = (LinearLayout)mainlayout.findViewById(R.id.rectangle_layout_ll);
sp = ctx.getSharedPreferences(SHARED_PREFS_NAME, ctx.MODE_PRIVATE);
String defValue = null ;
defValue = sp.getString("incomingsearchbackgroundcolor", null);
img = (ImageView)sublayout.findViewById(R.id.iv_priority);
img.setBackgroundColor(Integer.parseInt(defValue));
}
#Override
public void onPreferenceAttached(PreferenceScreen root, int xmlId) {
// TODO Auto-generated method stub
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
if(key.equals("incomingsearchbackgroundcolor"))
{
sp = ctx.getSharedPreferences(SHARED_PREFS_NAME, ctx.MODE_PRIVATE);
String defValue = null ;
defValue = sp.getString("incomingsearchbackgroundcolor", null);
Log.d("Debug", defValue);
int iColor = Integer.parseInt(defValue);
img.setBackgroundColor(iColor);
img.invalidate();
if(this.isAdded())
{
getActivity().getSupportFragmentManager().beginTransaction().detach(this).commit() ;
getActivity().getSupportFragmentManager().executePendingTransactions();
getActivity().getSupportFragmentManager().beginTransaction().attach(this).commit();
getActivity().getSupportFragmentManager().executePendingTransactions();
}
}
}
#Override
public void onResume()
{
super.onResume();
sp.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
sp.unregisterOnSharedPreferenceChangeListener(this);
}
}
and this is preference xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:summary = "Options" android:title = "OPTIONS" android:key="options">
<CheckBoxPreference android:key="IncomingCallSearch" android:summary="On/Off" android:title="Enable Incoming Call Search" android:defaultValue="true"/>
</PreferenceCategory>
<PreferenceCategory android:summary = "Screen Settings" android:title = "Screen settings" android:key="ScreenSettings" >
<ListPreference
android:entries="#array/screenLocOptions"
android:entryValues="#array/screenLocValues"
android:key="incomingcallsearch_screenlocation"
android:title="Location"
android:defaultValue="Top"/>
<Preference
android:defaultValue="0xFF000000"
android:key="incomingsearchbackgroundcolor"
android:title="Background Color"
android:layout="#layout/rectangle_layout" />
</PreferenceCategory>
and this is the rectangle_layout 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="wrap_content"
android:padding="5dp"
android:id="#+id/rectangle_main_layout_ll">
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Background color"
android:layout_weight= "0.9"/>
<LinearLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:orientation="vertical"
android:layout_weight= "0.1"
android:id="#+id/rectangle_layout_ll">
<ImageView
android:id="#+id/iv_priority"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
/>
</LinearLayout>
As i wrote previous version of this post, the code above runs successfully in the first the the fragment loaded, imageview background color updated in preferences ui. I try to write failure condition step by step :
1 . I select the 3rd bar from actionbar and a fragment loaded including buttons.2 . I press the button that loads preferences fragment(you can see the code above)
3 . In this preferencefragment, there is a preference including textview and imageview(you can see the details above)
4 . When i click this preference, a color picker ran.(you can see the details above)
5 . I choose a color from color picker and save it to sharedpreference.(you can see the details above)
6 . onsharedpreferencechanged event triggered and i change the background color of imageview(you can see the details above) succesfully.
7 . I leave fragment with choosing another tab from action bar or with using backpress button.
8 . I launch same fragment with pressing same button in 3rd bar.
9 . I again use color picker and onsharedpreferencechanged event is triggered.
10 . I can see with debugging that true color code is taken from sharedpreference and it is set to imageview background color and the code below is run :
getActivity().getSupportFragmentManager().beginTransaction().detach(this).commit() ;
getActivity().getSupportFragmentManager().executePendingTransactions();
getActivity().getSupportFragmentManager().beginTransaction().attach(this).commit();
getActivity().getSupportFragmentManager().executePendingTransactions();
11 . But preference is not updated in this time. The old color or black is seen in imageview.
Thank you very much
getSupportFragmentmanager().detach(this).attach(this).commit()
All the changes cumming only after the commit. So if you will call detach(this) and attach(this) without a commit at the middle you changed nothing.
Try doing some thing like this:
getSupportFragmentmanager().detach(this).commit();
getSupportFragmentmanager().attach(this).commit();
This is what stays behind the commit idea.
P.S
I did not found FragmentManager.detach() method in the API...
at last i solved the problem and it is really simple . I hope this post helps anyone who has problem with dynamic preference updating.
public class SettingsIncomingSearchFragment extends PreferenceListFragment
implements SharedPreferences.OnSharedPreferenceChangeListener,
PreferenceListFragment.OnPreferenceAttachedListener {
Context ctx ;
public static final String SHARED_PREFS_NAME = "settings";
<-- Begin : i delete these global variables and define them in methods locally and
set the values in methods -->
LinearLayout mainlayout ;
LinearLayout sublayout ;
View view ;
Preference myPref ;
ImageView img ;
SharedPreferences sp ;
<-- End : i delete these global variables and define them in methods locally and
set the values in methods -->

Setting the text color of preferences screen

Friends I have created a listpreferences screen ,and it is working fine but I want to change the color of text appearing on the list and the RadioGroup items. So can anyone help me out.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
android:entries="#array/gametype"
android:entryValues="#array/gametype"
android:key="listpref"
android:summary=""
android:title="Set Game Level" />
</PreferenceScreen>
This is my preferences screen.
After Clicking on listpreferences item it show a dialog box which has three options
so i want to change the color of that radiobutton text and the list item text.
here is java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
SharedPreferences sp=getPreferenceScreen().getSharedPreferences();
Preference pref=findPreference("listpref");
ListPreference lp=(ListPreference)pref;
pref.setSummary(lp.getValue());
sp.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Preference pref=findPreference(key);
ListPreference lp=(ListPreference)pref;
pref.setSummary(lp.getValue());
}
public void setcolor(int position){
for(int i=0;i<4;i++)
{
if(i==position)
{
list.getChildAt(i).setBackgroundColor(Color.rgb(238, 180, 180));
}
else{
list.getChildAt(i).setBackgroundColor(Color.WHITE);
}
}
Try like this ;
RadioGroup.setTextColor(Color.GREEN);
This is good solution
If you want to keep a consistent colouring scheme in the whole application I'd suggest learning about adding a theme or styles. This should get you started http://developer.android.com/guide/topics/ui/themes.html.
Use Html.fromHtml to style your text.
mPodFolderPref.setTitle(Html.fromHtml("<font color='red'>" + mPodFolderPref.getTitle() + "</font>"));
mPodFolderPref.setSummary(Html.fromHtml("<font color='red'>" + mPodFolderPref.getSummary() + "</font>"));
Html.fromHtml can do a lot for you.
Mal

Custom layout for EditTextPreference with null validation in custom dialog

I have PreferenceActivity in my app and I want to specify custom layout for EditTextPreference.
I do it in an xml file with android:layout attribute. All is OK, the layout is getting changed but I can't change any value of its fields. The code is running without errors, in debug I can see that values are changed, but visually there are no changes.
I've tried to call view.invalidate() and onContentChanged() but nothing changed.
All of IDs are OK and no exceptions are being thrown. So I have no idea. I'll appreciate you help.
Here is code of my preferences_file_types.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:key="#string/preference_category_file_types"
android:title="#string/settings_supported_file_types" >
<EditTextPreference
android:key="#string/preference_file_type_image"
android:layout="#layout/file_types_list_item"
android:title="#string/settings_supported_file_types_hint" />
</PreferenceCategory>
</PreferenceScreen>
Here is code of my PreferenceActivity class:
public class FileTypesSettingsActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_file_types);
LayoutInflater inflater = getLayoutInflater();
EditTextPreference imageTypePreference = (EditTextPreference) findPreference(getString(R.string.preference_file_type_image));
View specifiedView = inflater.inflate(imageTypePreference.getLayoutResource(), null);
changeViewAttributes(specifiedView);
}
private void changeViewAttributes(View view) {
ImageView fileTypeIcon = (ImageView) view.findViewById(R.id.file_types_list_item_iv_icon);
fileTypeIcon.setBackgroundResource(R.drawable.ic_file_image);
TextView fileTypePredefined = (TextView) view.findViewById(R.id.file_types_list_item_tv_predefined_extensions);
fileTypePredefined.setText("JPG BMP GIF PNG");
TextView fileTypeAdded = (TextView) view.findViewById(R.id.file_types_list_item_tv_added_extensions);
fileTypeAdded.setText("BLA BLA BLA");
}
}

Cannot Combine Custom Title With Other Title Features

I understand what the error is telling me, but I have no idea what I could be using that it considers title features that I'm trying to combine with Custom Title.
inspection_title.axml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inspectionTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="-1"/>
LiftInspection.cs
[Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
public class LiftInspection : ExpandableListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
string callInfo = Intent.GetStringExtra("CallInfo");
RequestWindowFeature(WindowFeatures.CustomTitle);
SetContentView(Resource.Layout.LiftInspection);
Window.SetFeatureInt(WindowFeatures.CustomTitle, Resource.Layout.inspection_title);
TextView title = (TextView) FindViewById(Resource.Id.inspectionTitle);
title.Text = callInfo;
}
There is no additional title customization in the manifest or anything. What could possibly be combining with the CustomTitle to generate this exception?
check in AndroidManifest.xml if your theme's name is "AppTheme", if so try to change it to any other, it's strange but it helped me
try this way:
final Window window = getWindow();
boolean useTitleFeature = false;
if(window.getContainer() == null) {
useTitleFeature = window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
SetContentView(Resource.Layout.LiftInspection);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.inspection_title);
}

Background from PreferenceActivity is not applied to sub-PreferenceScreen

I am testing my application on a Nexus One and i have some problems. My theme is Light and
when an inner sub PreferenceScreen is displayed, the window background
becomes black instead of keeping the PreferenceActivity's one.
<PreferenceScreen android:title="main preferences">
...
<PreferenceScreen android:title="sub screen">
</PreferenceScreen>
</PreferenceScreen>
What is the problem?
Wouter
Use this:
Create theme in style.xml file
<style name="Theme.SettingsBackground" parent="#android:style/Theme.NoTitleBar">
<item name="android:windowBackground">#android:color/black</item>
</style>
and then in manifest file use:
<activity android:name=".Settings" android:theme="#style/Theme.SettingsBackground"></activity>
Do this for all sub activities which you want.
To best understand what is happening here you can refer to this piece of code from the source code for the PreferenceScreen class:
#Override
protected void onClick() {
if (getIntent() != null || getPreferenceCount() == 0) {
return;
}
showDialog(null);
}
private void showDialog(Bundle state) {
Context context = getContext();
ListView listView = new ListView(context);
bind(listView);
// Set the title bar if title is available, else no title bar
final CharSequence title = getTitle();
Dialog dialog = mDialog = new Dialog(context, TextUtils.isEmpty(title)
? com.android.internal.R.style.Theme_NoTitleBar
: com.android.internal.R.style.Theme);
dialog.setContentView(listView);
if (!TextUtils.isEmpty(title)) {
dialog.setTitle(title);
}
dialog.setOnDismissListener(this);
if (state != null) {
dialog.onRestoreInstanceState(state);
}
// Add the screen to the list of preferences screens opened as dialogs
getPreferenceManager().addPreferencesScreen(dialog);
dialog.show();
}
The way that I work around it is to set the parent background color by overriding onCreateView in the first preference added to the preference screen. Of course this requires some custom code but it's not terribly complicated, for instance to set a white background:
package com.justinbuser.livewallpapers;
import android.preference.PreferenceCategory;
public class VideoChooserPreferenceCategory extends PreferenceCategory{
public VideoChooserPreferenceCategory(Context context) {
super(context);
}
#Override
protected View onCreateView(ViewGroup parent)
{
parent.setBackgroundColor(0xFFFFFFFF);
return super.onCreateView(parent);
}
}
You would then of course need to use that custom category by altering your xml, i.e.:
<PreferenceScreen android:title="main preferences">
<PreferenceScreen android:title="sub screen">
<com.justinbuser.livewallpapers.VideoChooserPreferenceCategory android:title="sub screen category" />
</PreferenceScreen>
</PreferenceScreen>
Also, if you notice the android PreferenceScreen changes the theme based on whether or not a title is set, i.e. if a title exists it enables a theme that includes the title bar. So if you want no title bar you should avoid setting the preferencescreen title and set it statically in xml or dynamically through code.
Have you tried this?
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference){
super.onPreferenceTreeClick(preferenceScreen, preference);
if (preference!=null)
if (preference instanceof PreferenceScreen)
if (((PreferenceScreen)preference).getDialog()!=null)
((PreferenceScreen)preference).getDialog().getWindow().getDecorView().setBackgroundDrawable(this.getWindow().getDecorView().getBackground().getConstantState().newDrawable());
return false;
}
Add this method in your PreferenceActivity.
At comment #35 from this source.

Categories

Resources