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);
}
});
Related
The recyclerView is used to display a custom ArrayList of products and this view can be switched between different types of products by the user via radio buttons that call swapAdapter. The products are all filtered by diameter via spinner bar selection, and since the user will want to get products of matching diameter, the filter must hold as the adapters swap. All of this is working in the view, however when the user selects an item to add to the cart, the cart will show the wrong item. It will be from the same position however, from the previous adapter. As these adapters switch so does the last adapter. Why are the views refreshing but the data linked to the position is always one and only one step behind?
Here is the RecyclerView
mRecyclerView = findViewById(R.id.inventory_recycler_view);
mLayoutManager = new LinearLayoutManager(this);
aAdapter = new CustomAdapter(aList);
bAdapter = new CustomAdapter(bList);
cAdapter = new CustomAdapter(cList);
dAdapter = new CustomAdapter(dList);
eAdapter = new CustomAdapter(eList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(aAdapter);
and here is the radio group
bButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mRecyclerView.swapAdapter(bAdapter, true);
bAdapter.getFilter().filter(filterDiameterString);
// bAdapter.notifyDataSetChanged();
}
});
cButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mRecyclerView.swapAdapter(cAdapter, true);
cAdapter.getFilter().filter(filterDiameterString);
// cAdapter.notifyDataSetChanged();
}
});
dButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mRecyclerView.swapAdapter(dAdapter, true);
dAdapter.getFilter().filter(filterDiameterString);
// dAdapter.notifyDataSetChanged();
}
});
eButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mRecyclerView.swapAdapter(miscAdapter, true);
eAdapter.getFilter().filter(filterDiameterString);
// eAdapter.notifyDataSetChanged();
}
});
The following has been attempted:
notifyDataSetChange
recyclerview.invalidate
adapter.clear()
I was able to resolve this issue by adding a setter to my arraylist in the custom adapter and then changed the list via the setter then called notify data set change.
I know that was already asked but it is outdated:
I have 2 buttons that represent 2 choices and if one is selected the background color gets changed to yellow. But if i want to change the choice i need to somehow reset the button:
I already try to set it back but some old design comes out. Can you provide me the id of the modern button style? And show me how to implement it?
int myChoice;
if (view == findViewById(R.id.choice1)){
myChoice = 1;
choice1.setBackgroundColor(getResources().getColor(R.color.highlightButton));
choice2.setBackgroundResource(android.R.drawable.btn_default);
}
else if (view == findViewById(R.id.choice2)){
myChoice = 2;
choice2.setBackgroundColor(getResources().getColor(R.color.highlightButton));
choice1.setBackgroundResource(android.R.drawable.btn_default);
}
}
Use Tags with getBackground(). This will assure you are always setting back to original.
Add following in beginning of function
if (v.getTag() == null)
v.setTag(v.getBackground());
Then instead of setBackgroundResource, use
v.setBackground(v.getTag());
Starting from here, you can store the default color of the button into a Drawable and grab the selection color (Yellow in your case) into anther Drawable, then toggle background colors of buttons with these Drawable variables
please check below demo
public class MainActivity extends AppCompatActivity {
private Drawable mDefaultButtonColor;
private Drawable mSelectedButtonColor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn1 = findViewById(R.id.btn1);
final Button btn2 = findViewById(R.id.btn2);
mDefaultButtonColor = (btn1.getBackground());
mSelectedButtonColor = ContextCompat.getDrawable(this, R.color.buttonSelected);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleButton(btn1, true);
toggleButton(btn2, false);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleButton(btn1, false);
toggleButton(btn2, true);
}
});
}
private void toggleButton(Button button, boolean isSelected) {
button.setBackground(isSelected ? mSelectedButtonColor : mDefaultButtonColor);
}
}
I have a Scrollview in my activity , I want when I press a button the view scrolls down to a specific ID in the view. After some search here I found I can use this code
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
but it scrolls all the page down. I want to scroll to a specific ID in the middle of the view
ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);
scrollView.postDelayed(new Runnable() {
public void run() {
scrollView.scrollTo(0, (int)view.getY());
}
}, 100);
view = your view you want to scroll to
You can do this as below:
private void smartScrollToPosition(ListView listView, int desiredPosition) {
//Write your logic here.
listView.smoothScrollToPosition(desiredPosition);
}
On button handler you can call this as follows
ListView listView = (ListView) findViewById(R.id.lessonsListView);
smartScrollToPosition(listView, 0); // scroll to top
return true;
My problem is I have a button and that button is doing create new textview but that textviews removing when i click back button. How I saved textviews in activity?
My java sourcecodes here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notlar);
Button btnNotEkle = (Button) findViewById(R.id.btnNotEkle);
final EditText etNot = new EditText(NotEkle.this);
final LinearLayout layoutNotlar = (LinearLayout) findViewById(R.id.layoutNotlar);
final TextView tv1 = (TextView) findViewById(R.id.tvnotOrtalama);
etNot.setInputType(InputType.TYPE_CLASS_NUMBER);
AlertDialog.Builder notEkle = new AlertDialog.Builder(NotEkle.this);
notEkle.setTitle("Notunuz");
notEkle.setView(etNot);
//Positive button
notEkle.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
tvNot = new TextView(NotEkle.this);//girelen not burdaki textview e yazdırılacak.
girilenNot = etNot.getText().toString();//Girilen notu alıyoruz
tvNot.setText(girilenNot);//girilen notu textviewa veriyoruz
notTopla += Integer.parseInt(girilenNot);//Notları topluyoruz
layoutNotlar.addView(tvNot);
count = layoutNotlar.getChildCount();
dersOrtalamaYazdir=String.valueOf(dersOrtalama());
tv1.setText("Ders Ortalamanız : "+dersOrtalamaYazdir);
dialog.cancel();
}
});
final AlertDialog notEkleCreate = notEkle.create();
btnNotEkle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
notEkleCreate.show();
}
});
}
}
Try giving your TextView objects ids.
You need to know that when you click back button - by default your activity is destroyed so all views are removed.
When you are adding new TextView you should add information about this TextView (like the text itself) to some list declared as field in your activity.
Then you can save this list when activity is recreated see: onSaveInstanceState/nRestoreInstanceState
You can also pass this list back or to new activity so that they can take actions based on this list.
Following my understanding your TextView had been created inside Dialog and after you press back button the dialog dismisses and all views you created inside will be removed and you can't access it from your Activity.
You may try to create TextView in onCreate, pass and in Dialog just call setText. I hope this is the answer you're looking for.
Cheers.
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;
}
});