Problems with coding Preference Screen - android

i have a big Problem with this Code:
public class Preferences extends PreferenceActivity {
private LinearLayout rootView;
private LinearLayout titleView;
private ListView preferenceView;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
rootView = new LinearLayout(this);
rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
rootView.setOrientation(LinearLayout.VERTICAL);
textView = new TextView(this);
textView.setText(R.string.tx_einstellungen);
textView.setTextColor(Color.WHITE);
textView.setTextSize(20);
titleView = new LinearLayout(this);
titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 45));
titleView.setBackgroundResource(R.color.background);
titleView.addView(textView);
titleView.setGravity(Gravity.CENTER);
preferenceView = new ListView(this);
preferenceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
preferenceView.setId(android.R.id.list);
PreferenceScreen screen = createPreferenceHierarchy();
screen.bind(preferenceView);
preferenceView.setAdapter(screen.getRootAdapter());
rootView.addView(titleView);
rootView.addView(preferenceView);
this.setContentView(rootView);
setPreferenceScreen(screen);
}
private PreferenceScreen createPreferenceHierarchy() {
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceScreen lang_screen = getPreferenceManager().createPreferenceScreen(this);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
// Screen preference
lang_screen.setKey("language_pref");
lang_screen.setTitle("Sprache");
lang_screen.bind(preferenceView);
root.addPreference(lang_screen);
// Example of next screen toggle preference
CheckBoxPreference nextScreenCheckBoxPref = new CheckBoxPreference(this);
nextScreenCheckBoxPref.setKey("next_screen_toggle_preference");
nextScreenCheckBoxPref.setTitle("");
nextScreenCheckBoxPref.setSummary("");
lang_screen.addPreference(nextScreenCheckBoxPref);
return root;
}
The First PreferenceScreen is a nice white screen with a blue line on the top and in the line we have the Text "Einstellungen" in english "Preferences" in color white.
However when i click on "Sprache" the next prefcreen is black and there is an android Titlebar.... i want to create more "under-prefscreens" and they all have to be white with these blue line on the top.
PLEASE HELP
Thankx Alex

This is a known bug in Android 2.1u1.
If you use multi-level preference screen the Light theme is not propogated to next level screens. For higher SDK 2.2 and above it works perfectly fine. The issue is also on some devices. Unfortunately no solution yet for this apart from leaving the theme as dark.

Related

Create view programmatically with style

i have a problem with my application. Basically what it does, is when i click a button, it creates 3 fields (2 editText and 1 spinner) on a scrollView. The thing works well, the only problem that im having, is related with the style, the activity bgColor is white(as the rest of the app) but, when i create elements programmatically, these elements doesnt have the look of the rest of my app. The editTexts are white with white letters (impossible to read since my bgColor is white as well) and its the same thing with the spinner. What can i do? Here is a snipet of code so you can see what im doing here.
public class AddIngredients extends Activity {
public int Count = 0;
public String[] spinnerArray = {"Gr", "kg", "Cups", "ml", "L", "oz"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addingredients);
final TableLayout lm = (TableLayout) findViewById(R.id.TableMain);
TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button addMore = (Button)findViewById(R.id.addmore);
addMore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TableRow ll = new TableRow(getApplicationContext());
//ll.setOrientation(LinearLayout.HORIZONTAL);
EditText product = new EditText(getApplicationContext());
product.setHint(" Ingredient "+Count +" ");
// Create Button
EditText amount = new EditText(getApplicationContext());
// Give button an ID
amount.setId(Count);
amount.setHint("Quantity");
final Button btn2 = new Button(getApplicationContext());
btn2.setId(Count);
btn2.setText("Remove " + Count);
Spinner spinner = new Spinner(getApplicationContext());
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
ll.addView(product);
ll.addView(amount);
ll.addView(spinner);
lm.addView(ll);
Count = Count + 1;
I know my XML is working well because if i create the 3 views on my xml, they look great. PD: Thx in advance for any help! Greetings.
you can use
amount.setTextColor(Color.BLACK);
to set colour of text to black or any other colour
same can be used for spinner
Here's how I set the colors of my edit text lines and other theme-related android views.
http://android-holo-colors.com/
I just picked the color and views I wanted, then unzipped them in my res folder, then set the theme according the android tutorials.
I recommend backing up your res folder first, in case you don't like the results.
Garret
I had a error in my code. When creating the fields, i was using
(getApplicationContext());. I fixed it using MyApplicationName.this.

Adding a new edit text in list view on a button click in android

Is it possible to add a new EditText automatically in a ListView on a Button click?
If so please let me know.
EditText name = new EditText(youractivity);
convertView.addView(name);
Call notifydatasetchanged() but you have to save the field that you add becouse after scrolling into the list you will have in all list that editText and you have to remove where you don't want to appear.
What I recommand you is to make an editext into your cellView hidden and make it visible when you tap on your button.
You could make a custom adapter for your listview and give it a layout containing the edittext with the visibility set to gone. You can then set it to visible onbuttonclick.
I would like to suggest one code its not about listview but see if it could give you some idea,by adding views dynamically in linear layout which is inside scroll view.
public class MainActivity extends Activity {
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
static int i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
Button button = (Button)findViewById(R.id.button);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView view = new TextView(MainActivity.this);
view.setText(++i+" view");
linearLayout.addView(view, layoutParams);
}
});
}}

Dynamically add element to layout

I am quite new to developing apps. Still I would have thought that this is a basic action, so if there is already a solved thread I would be OK with the link. But since I am searching for over 2 hours for this I am asking anyway:
I want to dynamically add an element to my layout every time the user clicks a button.
By now I have this:
XML (R.layout.game.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit_choice"
android:onClick="submitChoice"/>
</LinearLayout>
Java
public void submitChoice(View view)
{
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
LinearLayout ll = new LinearLayout(this);
ll.addView(View.inflate(ll.getContext(), R.layout.game, null));
ll.addView(textView);
setContentView(ll);
}
Since the XML file does not change, it only works once.
So how can I add a second text when the user clicks the button a second time (without changing the XML file)? Examples are appreciated.
The problem comes from this line, that recreate the whole layout every time:
LinearLayout ll = new LinearLayout(this);
You should define it and setContentView(ll) outside the submitChoice function. Then on click only create and add the textView , then call ll.invalidate(); to see the changes.
Something like:
LinearLayout ll;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
ll = (LinearLayout) findViewById(R.id.ll_game);
}
// More code...
public void submitChoice(View view) {
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
ll.addView(textView);
ll.invalidate();
}
where ll_game is the id you have to set in xml for your LinearLayout.

How to make a non-XML PopupWindow appear on the screen in Android

I've created a class that generates a PopupWindow based on parameters fed into it. I believe this will suit my needs better than manipulating XML-based content for my PopupWindow.
Creating the window and it's content seems to go through smoothly - it's actually getting that content to appear on the screen that I haven't been able to manage yet. The problem is that I haven't been able to find an example of PopupWindow code in use that doesn't rely on the LayoutInflater function to place it on the screen. As my PopupWindow isn't generated from an XML file, I can't use LayoutInflater to place it on the screen.
Something else I should probably explain is that my PopupWindow-generating class is in it's own file. i.e. It is not a subclass of an Activity file. I've done it this way so that I can easilly copy my custom-PopupWindow class to any future projects I might develop.
Here is the basic layout of my class:
class myPopup extends Object {
public myPopup(parameters){
ViewGroup winBody;
// "winbBody" will be the content of the PopupWindow.
// Code that fills and adjusts "winBody" based on the parameters goes here.
int width = //Determined by parameters.
int height = //Determined by parematers.
PopupWindow pw = new PopupWindow(winBody, width, height, true);
//This is as far as I seem to get before getting stuck.
}
}
I gather that I am supposed to somehow use the PopupWindow function "showAtLocation", but I am unclear what parameters I am supposed to use for this. Could someone please tell me how to get my popup window to appear on the screen? Hopefully in the very center of it. :)
Try something like that
you may define the whole popup window as an activity and just make its background disapear, so it will look like a popup window:
<style name="MyTransparentPopup">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
</style>
public class PopupWindowActivity extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Show popup");
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
popUp.update(0, 0, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hello popup");
layout.addView(tv, params);
popUp.setContentView(layout);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}

If a toggle button is checked or not

Well I am trying to see if a checkbox is checked or not, but I get errors.
Defining the checkbox code:
public class Preferences extends PreferenceActivity {
CheckBoxPreference togglePref;
...
}
CheckBox Code:
public void checkNotify() {
if (togglePref.isChecked()==(true)) {
...
}
}
OnCreate code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//SharedPreferences settings = getSharedPreferences("EasySettingsPreferences", MODE_PRIVATE);
//boolean notify = settings.getBoolean("notify", false);
checkNotify();
rootView = new LinearLayout(this);
rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
rootView.setOrientation(LinearLayout.VERTICAL);
togglePref = new CheckBoxPreference(this);
textView = new TextView(this);
textView.setText(R.string.app_name);
titleView = new LinearLayout(this);
titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 26));
titleView.setBackgroundResource(R.drawable.pattern_carbon_fiber_dark);
titleView.addView(textView);
preferenceView = new ListView(this);
preferenceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
preferenceView.setId(android.R.id.list);
PreferenceScreen screen = createPreferenceHierarchy();
screen.bind(preferenceView);
preferenceView.setAdapter(screen.getRootAdapter());
rootView.addView(titleView);
rootView.addView(preferenceView);
this.setContentView(rootView);
setPreferenceScreen(screen);
}
Logcat Picture:
logcat pic http://img710.imageshack.us/img710/8529/unledxq.png
Debugger Picture
debugger pic http://img847.imageshack.us/img847/1192/unled1rn.png
Please help me if you can. Thanks!
I guess that you never initialise togglePref. To be sure we need to see the onCreate(). (I will update my answer if my guess was wrong...)
edit
I was right. You call checkNotify() before your even initialized the togglePref variable. Check your logic if it really makes sense to call that method before everything else or if it is fine if you call it later.
A tip: You can simplify your if statement:
// yours:
if (togglePref.isChecked()==(true)) {
// simplified:
if (togglePref.isChecked()) {
In your onCreate() method, you're calling checkNotify() before togglePref has been initialized. You want to move that initialization up (or move checkNotify() down.)

Categories

Resources