How can I remove setBackgroundResource when new View is clicked? - java

I have a Horizontal Listview with a textview. When I click on a textview in the view, that particular textview gets a border.
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.minute_listview, null);
Typeface afBold = Typeface.createFromAsset(getAssets(), "fonts/AftenScreen-Bold.ttf");
minuteText = (TextView) retval.findViewById(R.id.title);
Button button = (Button) retval.findViewById(R.id.clickbutton);
button.setOnClickListener(mOnButtonClicked);
minuteText.setText(dataObjects[position]);
minuteText.setTypeface(afBold);
//THIS IS WHERE THE BORDER GETS SET
minuteText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.border);
}
});
return retval;
}
Now, how can I remove this border and set it on the next textview thats clicked?

Use this it will give you a white background or a translucent one, in other words it has been removed
WhatEverView.setBackground(new ColorDrawable(Color.TRANSPARENT));
okay full code
int pos -1;// default is -1, which means no one has altered it
// replicate this onclick listener logic
minuteText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(pos != -1){ // it is not -1 that means some1 has altered it
parentView.findViewById(pos).
setBackground(new ColorDrawable(Color.TRANSPARENT));
// the above line searched for the view and changed the background
}
pos = v.getId(); // the id of the new view, keep doing it
v.setBackgroundResource(R.drawable.border);
}
});
so use this for all onclick listeners you want that effect on
Does it fit your requirement?

add this
TextView ClicledTv ;//to save the clicked tv id
then
minuteText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(Clickedtv!=null)
clickedtv.setBackground(R.drawable.anotherOne);
v.setBackgroundResource(R.drawable.border);
clickedTv=minuteText;
}
});

Related

Show BottomSheet after change the layout

I have a BottomSheet "buttomSheet" (in this sheet it is a list view that is GONE) and a normal Button "btnShowListView" (out from the "buttomSheet").
I want that when I click on "btnShowListView" the list view in the bottomSheet will be Visible its work but only after 2 clicks on button "btnShowListView"...
this is my code:
final View bottomSheetView = findViewById(R.id.bottomSheetLayout);
listView = (ListView) bottomSheet.findViewById(R.id.listView);
buttomSheet = BottomSheetBehavior.from(bottomSheetView);
btnShowListView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listView.setVisibility(View.VISIBLE);
bottomSheetView.requestLayout();
bottomSheetView.invalidate();
bottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
You can make the listview already visible, there's no need to hide it; you can get te same effect by setting the state of the bottom sheet behavior to HIDDEN.
I tested the code below and it should work also for you.
final View bottomSheetView = findViewById(R.id.bottomSheetLayout);
bottomSheetBehavior= BottomSheetBehavior.from(bottomSheetView);
bottomSheetBehavior.setHideable(true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
btnShowListView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});

How to change background of non-visible buttons in GridView (Button adapter) from an activity

My code works only for visible views.
mGridView = (GridView) findViewById(R.id.gridView);
mGridView.setAdapter(new ButtonAdapter(this, list));
Method called when chronometer ticks:
public void setBackground(int i) {
Button button = (Button) mGridView.getChildAt(i);
button.setBackgroundResource(R.drawable.button_shape);
This method causes NPE because getChildAt cannot access non-visible childs.
I tried some solutions found here but no luck so far (android - listview get item view by position - this solution did not causes NPE but was changing background for more buttons in one tick)
What I need is to change first button background on first tick, second button on second tick.. and last button on last tick and stay consistent while scrolling.
My getView method in ButtonAdapter:
public View getView(final int position,
View convertView, ViewGroup parent) {
Button btn;
LayoutInflater inflater = LayoutInflater.from(mContext);
if (convertView == null) {
// if it's not recycled, initialize some attributes
btn = (Button) inflater.inflate(R.layout.button, parent, false);
int width = mContext.getResources().getDimensionPixelSize(R.dimen.gridView_param_width);
int height = mContext.getResources().getDimensionPixelSize(R.dimen.gridView_param_height);
GridView.LayoutParams params = new GridView.LayoutParams(width, height);
btn.setLayoutParams(params);
} else {
btn = (Button) convertView;
}
btn.setText(list.get(position).getName());
btn.setId(position);
btn.setTag(position);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SetGridViewListener activity = (SetGridViewListener) mContext;
activity.onClickGridView(position);
Button btn = (Button)v;
btn.setBackgroundResource(R.drawable.button_shape_clicked);
btn.setClickable(false);
}
});
return btn;
}
I think my problem is in getView method which is probably not recycling well for my purpose.
Thanks in advance.
I have solved it already. I used ViewHolder pattern like here ListView subobject clickable confilct and this method to access buttons from my activity:
public View getViewByPosition(int pos, GridView gridView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return gridView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return gridView.getChildAt(childIndex);
}
}
(android - listview get item view by position)
If you would like my concrete solution, write in comment and I will add it.

Having trouble removing and then adding views programatically to FrameLayout

I am having trouble removing a LinearLayout from a FrameLayout, to then add a new LinearLayout that is populated with EditText generated programatically.
The first time that the view is loaded, I catch a reference to the FrameLayout using findViewById(), and then I make a new LinearLayout programmatically and add a number of EditText to it.
It looks like this
formPlace = (FrameLayout) findViewById(R.id.form_place);
LinearLayout linearView = new LinearLayout(this);
linearView = makeEditText(arrayItems,category);
linearView.setBackgroundColor(Color.GRAY);
linearView.setOrientation(LinearLayout.VERTICAL);
formPlace.addView(linearView);
Next is the function that makes the EditTexts:
private LinearLayout makeEditText(ArrayList<String[]> arrayString, int position){
LinearLayout view = new LinearLayout(this);
arrayInputs = new ArrayList<>();
String[] arrayHints = arrayString.get(position);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i=0;i<arrayHints.length;i++){
EditText editText = new EditText(this);
Log.i("myApp","tantas veces" + i);
editText.setId(i);
editText.setTextColor(Color.BLACK);
editText.setHint(arrayHints[i]);
editText.setHintTextColor(Color.GRAY);
editText.setLayoutParams(params);
view.addView(editText);
arrayInputs.add(editText);
}
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.gravity = Gravity.RIGHT;
android.widget.Button save = new android.widget.Button(this);
save.setText("Guardar");
save.setLayoutParams(buttonParams);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("myApp", "guardar");
saveClicked(category);
}
});
view.addView(save);
android.widget.Button cancel = new android.widget.Button(this);
cancel.setText("cancelar");
cancel.setLayoutParams(buttonParams);
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("myApp", "cancelar");
cancelClicked();
}
});
view.addView(cancel);
return view;
}
Then, when a spinner event is called, I catch the reference of the FrameLayout and pass .removeAllViews(). This works as intended, and my pevious view is deleted. The problem comes when I try to add new views to the FrameLayout in the makeEditText line:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
if(firstTime==false) {
FormActivity.this.formPlace.removeAllViews();
LinearLayout auxLinearView = new LinearLayout(FormActivity.this);
auxLinearView = makeEditText(arrayItems, position);
FormActivity.this.formPlace.addView(auxLinearView);
}
firstTime =false;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
What happens is that the old view is deleted but the new view is not set properly. It only shows 1 element of the 4 is intended to show, and the buttons that I create programmatically don't show at all. And my FrameLayout occupies all the remaining space in my screen, even though is set to wrap content. What it implies is that the view is not behaving as it should.
EDIT:
Here are some Screenshots
Here is how it looks before removeAllViews():
https://dl.pushbulletusercontent.com/tExAXJexe4BfG5sGuKvb1RFtGOFd51nE/Screenshot_2015-05-04-17-54-18.png
Here is how it looks after adding the views to the FrameLayout:
https://dl.pushbulletusercontent.com/JM72D3ZrLwTh0YC9g7PODREyOfCj667P/Screenshot_2015-05-04-17-54-25.png

how to get textview text

In my android app, I have a activity that contain a listview. And within the each row of the listview, i have another listview let called it lv. Inside this lv, it contain some textview. My question is how can i get the text from the listview (A)?
I know that if there is only one listview and in order to get the text of each row when i click on a button, i can use the following code
textview.getText().toString();
However, when i try with this code to get the text in lv, it always give me the last value (n row) in lv. I am not able to get the text of 1st row to n-1 row of lv.
UPDATE:
The follow are the code where i want to get the text, the ordered[] is passed from a custom adapter to another custom adapter (which is the following code)
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=((Activity) context).getLayoutInflater();
View rowView=inflater.inflate(layoutResourceId, null,true);
txt1 = (TextView)rowView.findViewById(R.id.textView1);
txt2 = (TextView)rowView.findViewById(R.id.textView2);
txt3 = (TextView)rowView.findViewById(R.id.textView3);
txt4 = (TextView)rowView.findViewById(R.id.textView4);
txt5 = (TextView)rowView.findViewById(R.id.textView5);
txt6 = (TextView)rowView.findViewById(R.id.textView6);
txtoid = (TextView)rowView.findViewById(R.id.textView7);
final Button btnGet = (Button)rowView.findViewById(R.id.btnGet);
tmp = position;
String tmp2 = String.valueOf(ordered[position]);
txtoid.setText(tmp2);
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(btnconfirm.getText().toString().equals("Get")){
String tmp1 = txtq.getText().toString();
System.out.println("tmp1: "+tmp1);
}
}
});
....
Anyone help will be nice!
Thanks
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View rootView) {
// TODO Auto-generated method stub
TextView myText = (TextView)rootView.findViewById(R.id.textView7);
Log.d("","myText:" + myText.getText()); //you can get each row values.
}
});

Android - Change text of button in a selected row

i've tried everything but nothing have worked.
I have a listView like this:
[ checkbox ] | [textview] | [button]
What i'm trying to do is change the text of [button] only in the row witch [ checkbox ] is checked.
Here is the problematic block of code:
public View getView(int position, View convertView, ViewGroup parent){
final ViewHolder vh;
final View conv;
if(convertView == null){
LayoutInflater vi=(LayoutInflater)pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=vi.inflate(R.layout.rest_dishes_item, null);
conv=convertView;
final Button b[];
b=new Button[itens.size()];
vh=new ViewHolder();
vh.txt=(TextView)convertView.findViewById(R.id.dish_name);
vh.checkBox=(CheckBox) convertView.findViewById(R.id.dish_item);
vh.qnt=(Button) convertView.findViewById(R.id.qnt);
vh.quantidade=new Quantidade[itens.size()];
for(int i=0;i<itens.size();i++){
b[i]=(Button)conv.findViewById(R.id.qnt);
vh.quantidade[i].quantidade=1;
vh.quantidade[i].order=i;
}
vh.qnt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i=0;i<itens.size();i++){
Titem item=itens.get(i);
if(item.isCheck()){
vh.quantidade[i].quantidade++;
//Log.d("teste",i+""+vh.quantidade[i].quantidade);
b[i].setText(String.valueOf(vh.quantidade[i].quantidade));
}
else if(!item.isCheck()){
Log.d("teste",i+"1");
b[i].setText(String.valueOf(1));
}
}
}
});
//(Button) convertView.findViewById(R.id.qnt);
convertView.setTag(vh);
vh.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
CheckBox box=(CheckBox) arg0;
Titem item=(Titem)box.getTag();
item.setCheck(box.isChecked());
}
});
}else{
vh=(ViewHolder)convertView.getTag();
}
Titem item=itemList.get(position);
vh.txt.setText(item.getName());
vh.checkBox.setText(item.getName());
vh.checkBox.setChecked(item.isCheck());
vh.checkBox.setTag(item);
//Log.d("teste","chegou aqui");
return convertView;
}
With this code the text of the buttons doesn't change at all, no matter which button i click his text still "1".
Without the line:
b[i].setText(String.valueOf(1));
The button of a checked row has his text changed like i want. But When i click a button of a non checked row, the clicked button has text changed.
Maybe i'm doing something stupid or just missing something in the algorithm, at the moment i'm stuck.
Please help.
Thanks.
Sounds like you are confused, getView() is executed once per visible item. This is your code:
vh.qnt=(Button) convertView.findViewById(R.id.qnt);
vh.quantidade=new Quantidade[itens.size()];
for(int i=0;i<itens.size();i++){
b[i]=(Button)conv.findViewById(R.id.qnt);
vh.quantidade[i].quantidade=1;
vh.quantidade[i].order=i;
}
vh.qnt and all instances of b[i] reference the exact same Button (R.id.qnt) in the row that is being rendered.
You also need to update the text of the button after the if/else, when update the rest of the vh views.
I did it!!
I've used setTag() and getTag(), that allow me to use the same id (R.id.qnt) for different buttons. Here is the code:
int aux=0; //before onCreate()
.
.
.
vh.qnt.setTag(aux);//set a tag for each button in row
aux++; //this will be the number of rows
vh.qnt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
int index=(Integer)vh.qnt.getTag();
Titem item=itens.get(index);
if(item.isCheck()){
vh.qnt.setText(String.valueOf(vh.quantidade[index]++));
}
}
});
Thanks for the hint #dmon, you opened my eyes for the " same id " fact.

Categories

Resources