using shared peferences to save colors - colors

I have an app that uses a color picking dialog to change the app background color, and the text color. The color picker works fine, but when the app closes for any reason it reverts back to default.
I have checked: http://developer.android.com/guide/topics/data/data-storage.html#pref
and
Android Shared preferences example
the below code is the result of my research. The problem I'm having is all my text fields start off without text color, when i use the color dialog the colors change just fine, but are not saved.
Any pointers on where I'm going wrong would be greatly appreciated.
public class TipCalculator extends ActionBarActivity implements ColorPickerDialog.OnColorChangedListener {
//UI element objects to be manipulated.
TextView tipper;
TextView diner;
/*few TextView items left out to save space*/
int color;
RelativeLayout RLayout;
private SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip_calculator);
preferences = getSharedPreferences("TipCalculator", MODE_PRIVATE);
//color = preferences.getInt("TipCalculator", color); did not change anything
color = preferences.getInt("bg_color", color);
RLayout = (RelativeLayout) findViewById(R.id.RLayout);//layout object for background color manipulation
bill = (EditText) findViewById(R.id.bill_amount_text);//UI elements placed in objects for text color manipulation
billNtip = (TextView) findViewById(R.id.bill_tip_text);
/*few TextView items left out to save space*/
bill.setSelection(bill.getText().length());
bill.addTextChangedListener(billWatcher);
}
#Override
public void onPause(){// onStop does not seem to change how the app currently runs
super.onPause();
/*preferences = getSharedPreferences("TipCalculator", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit(); does not work*/
SharedPreferences.Editor editor = getSharedPreferences("TipCalculator", MODE_PRIVATE).edit();
//editor.putInt("TipCalculator", color); does not change anything
editor.putInt("bg_color", color);
editor.commit();
tipper.setTextColor(color);
bill.setTextColor(color);
/*few TextView items left out to save space*/
}
#Override
public void colorChanged(String key, int color) {
color = newColor;
if (decide.equals("font"))
{
tipper.setTextColor(color);
bill.setTextColor(color);
/*few TextView items left out to save space*/
}
else if (decide.equals("background"))
{
RLayout.setBackgroundColor(color);
}
}

Save the color to the SharedPreferences in onPause() instead of onResume(), like this:
#Override
public void onPause() {
SharedPreferences.Editor editor = getSharedPreferences("TipCalculator", MODE_PRIVATE).edit();
editor.putInt("bg_color", color);
editor.commit();
super.onPause();
}
onResume() is called when your app resumes, so if you exit but don't resume, the value won't get saved.
Somewhere in your initialization you will also need to read the color value out of the SharedPreferences e.g:
preferences = getSharedPreferences("TipCalculator", MODE_PRIVATE);
color = preferences.getInt("bg_color", android.R.color.white); // default to white or whatever color you want
tipper.setTextColor(color);
bill.setTextColor(color);

It doesn't look like you are setting the color on the tipper and bill
objects after you load it. In onCreate() after you call getInt() add
these lines: tipper.setTextColor(color); bill.setTextColor(color); You
will need to initialize these objects first obviously. – samgak
This was the final thing I was missing.
Thanks again for the help.

Related

(Animation) Hide Toolbar in Fragment

Hey i want to hide my toolbar when i click inside an editText Therefore i added these lines
mORMetWeight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!enterWeightClicked) {
enterWeightClicked = true;
Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
mToolbar.setVisibility(View.GONE);
mContainer.removeView(cv1);
mContainer.removeView(cv2);
mContainer.getChildAt(0);
header.getLayoutParams().height = (int) getResources().getDimension(R.dimen.smallEnterWeightHeight);
In my onCreateView method i added the transition effect but the animation now of the Toolbar looks horrible..
LayoutTransition transition = mContainer.getLayoutTransition();
transition.enableTransitionType(LayoutTransition.CHANGING);
I simply like to let the Toolbar scroll out and the view to the top? Is this possible with the "simple" animation style?

JDialog doesn't size correctly with wrapped JTextArea

While making a program, I noticed a bug with the JOptionPane.showMessageDialog() call. I use a button to create a JTextArea that wraps and then display a dialog containing this text area.
If the text area is too large, however, the dialog does not size itself correctly to the height of the JTextArea. The Dialog cuts off the OK button in this example.
I replicated the bug in the following code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DialogBug {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final String text = "looooooooooooooooooooooong text looooooooooooooooooooooooooooooooooooooong text";
JButton button = new JButton();
button.setPreferredSize(new Dimension(30, 30));
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JTextArea area = new JTextArea(text, 0, 50);
area.setEditable(false);
area.setLineWrap(true);
area.setWrapStyleWord(true);
area.append(text);
area.append(text);
area.append(text);
JOptionPane.showMessageDialog(frame, area, "why does it do this", JOptionPane.WARNING_MESSAGE);
}
});
frame.add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I would post a picture, but I don't have enough reputation...
Is there a way to fix this without having to use a JScrollPane?
Here's a screenshot:
If you run the pack command on the dialog (a function in the Window class) it will resize based on subcomponents. For your case you will have to rewrite without using the showMessageDialog() to get the resize to work (so make the dialog first, add the text, pack, then show it)
Dialog b = new Dialog();
// add stuff
b.pack();
For my test code it worked perfectly to get the dialogs to be the right sizes
Without pack()
With pack()

changing text of edit box in list view on button click in android

I am using a ListView, in which i have inflated another layout template which contains 2 Images, an edit text, a correction button , and 2 more buttons, one of which is quick edit button and now I want on click on this quick edit button the edit text should become editable and the correction button should become visible. can u please help me.
From the comments, for easy reading:
mv.quick_edit = (ImageButton)convertView.findViewById
(R.id.gateway_list_info_image_button_temp);
mv.quick_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyViewHolder m = new MyViewHolder();
m.correct_button.setVisibility(View.VISIBLE);
m.edit_text.setText("text changed");
}
});
in the adapter's getView method, i believe you have:
editText = (EditText)mView.findViewById(id here);
btnEdit= (Button)mView.findViewById(id here);
set the onClickListener() of the button in the getView method, and inside the onclick, set visibility to invisible or gone, for the other button you mentioned, and set the setEnabled method to true for the editText.
I hope this much clue helps.
EDIT:
For using a viewHolder in your code, please refer to this
Code Snippets:
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolderItem) convertView.getTag();
}
It is after the else block that you define the onClickListeners and use viewHolder.editText.setVisibility(View.INVISIBLE)

how to change textview size into all devices in android

hi i am doing one app in android for all sizes of mobiles and tablets.imageviews displyed in all sizes good.but i facing problm in textview font size.in my app i need to display textview with that background and text,but different sizes text size is displaying not correctly.any one having idea pls help me. i tried using below code...
MainActivity .class
public class MainActivity extends Activity {
float screenHeight,screenWidth,screendensity;
RelativeLayout alpha_page2;
ImageView alpha_back,alpha_back1;
TextView option121;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
screendensity = displaymetrics.densityDpi;
Log.i("screenHeight",""+screenHeight);
Log.i("screenWidth",""+screenWidth);
Log.i("screendensity",""+screendensity);
setContentView(R.layout.activity_main);
int letpading=(int)(116*(screenWidth/1024));
int toppading=(int)(79*(screenHeight/600));
int textsiz=(int)(50*(screendensity/600));
option121 = (TextView)findViewById(R.id.text1);
option121.setBackgroundResource(R.drawable.dog_b_blank);
option121.setText("A");
option121.setText(Color.BLACK);
RelativeLayout.LayoutParams layoutoption121 = (RelativeLayout.LayoutParams) option121.getLayoutParams();
layoutoption121.height=(int)(180*(screenHeight/600));
layoutoption121.width=(int)(180*(screenWidth/1024));
layoutoption121.topMargin=(int)(100*(screenHeight/600));
layoutoption121.leftMargin= (int)(250*(screenWidth/1024));
option121.setPadding(letpading, toppading, 0, 0);
option121.setTextSize(textsiz);
}
}
Thats not the way do it. Android takes care of text size for you, use 'sp' (scale-independent pixel) units when specifying text sizes and if you need to configure different sizes for different screens have a read of this:
Supporting Multiple Screens
Basically, you need to define your text sizes in .xml files in the appropriate resource directory:
res/values-large
res/values-xlarge
etc..
Then refer to those constants in your layout xml or your code.

ListView clipping

i working little bit with the ListView from JavaFx2. I´m running into one issue.
Is it possible to turn off the clipping of the ListCell/ListView?
I add an ImageView that has to be wider than the ListView and JavaFx2 shows automatically a scrollbar.
This my code snipped how i add the ImageView to my List:
list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
#Override
public ListCell<String> call(ListView<String> param) {
final ListCell<String> blub = new ListCell<String>() {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
StackPane p = new StackPane();
Label label = new Label(item);
p.getChildren().addAll(img, label);
setGraphic(p);
p.setAlignment(Pos.CENTER_LEFT);
}
}
};
blub.setStyle("-fx-background-color:transparent");
return blub;
}
});
Big thanks!
I don't think it's possible.
Maybe try to play with the Skin of the ListView. It seems that the scroll bar are managed in this class. It do not use a scroll pane.
Another solution could be replacing the ListView by a VBox in a ScrollPane.
Finally, you could try to modify img (by the way, where it come from, and what Class is it ?) to only show what you need.
Anyway, I'm interested by the solution you will use.

Resources