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

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)

Related

using shared peferences to save 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.

android Expandable List view slide up down animation for child view

I want to animate slide up slide down animation on expandable list view. when I click the group Item, It will expand the child view and collapse it.In the get Child view method I am populating the child views.
The following code to populate child views.
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Log.i(TAG, "getChildView");
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.child, null);
}
View toolbar = convertView.findViewById(R.id.toolbar);
setAnimationView(toolbar);
((LinearLayout.LayoutParams)
toolbar.getLayoutParams()).bottomMargin = -75;
toolbar.setVisibility(View.GONE);
ExpandAnimation expandAni = new ExpandAnimation(toolbar, 1000);
toolbar.startAnimation(expandAni);
TextView tv = (TextView) convertView.findViewById(R.id.childTo);
tv.setText(text);
return convertView;
}
How can I solve my problem.please help...
Thanks & Regards,
Venkatesan.R
I recently just wrote an widget for doing exactly what you've asked for. I've released the source code here:
Android ExpandableListView using animation
I may improve the implementation and add comments in the future, but the main idea will stay the same. Take a look at this post if you are interested in how it works: Android ExpandableListView using animation.
the ListView classes are made for performance for a great number of rows with just in time drawing while sliding. so animation is no goal of this kind of view and not that easy.
maybe something like android:animateLayoutChanges="true" could work.
you can also take a look here: link

can i put a dialog in a spinner item?

I'm new in the android world but I got experience in java.
Im trying to show a dialog when I select an especific item in my spinner but, when I do it, my application has stopped.
I have 1 fragment and 1 class for the listener, instantiate an object from the fragment in the listener and try to call the dialog.
The code is somthing like this:
//Instantiate of class Guardar extends SherlockFragment.
Guardar controlador = new Guardar();
public void onItemSelected(final AdapterView parent, View view, int pos,long id) {
String addSM = parent.getItemAtPosition(pos).toString();
if (addSM == “Anyadir”){
// custom dialog
final Dialog dialog = new Dialog(controlador.context);
dialog.setContentView(R.layout.dialog_afegirsuper);
dialog.setTitle(“Title…”);
// set the custom dialog components – text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(“Android custom dialog example!”);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
Is this possible to implement? another similar idea?
thanks a lot for any solution

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.

Extend View and Custom Dialogs

I am working on a game where I am extending the view and performing operations in the class. I need to have a popup in the game which will have 3 buttons inside it. I have managed to have the popup show-up using custom dialogs but when I configure the onClick as follows:
private void popUp() {
Context mContext = getContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_fullimage_dialog);
dialog.setTitle("Cheese Market");
Button one = (Button)findViewById(R.id.firstpack);
one.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cheeseLeft = cheeseLeft + 10;
masterMoveLock = false;
return;
}
});
}
It force closes giving a nullpointerexeption even though it is defined in the custom_fullimage_dialog layout.
Can someone help me figure out how to have the button click detected in this scenario?
Thank you.
Try calling dialog.findViewById instead.
You're setting the contentView for the dialog, but by calling findViewById you're looking for it under your activity's content view.

Resources