Hide and Show Actionbar elements disappeared - java

I need hide ActionBar at one precise moment hide the ActionBar and place the app in fullscreen, so hide the view of the elements like text view. The problem is that when I go back to normal mode the textview is behind the ActionBar.
Does anyone have any idea how can I fix this?
int uiOptions;
mView = this.getWindow().getDecorView();
TextView text = (TextView) findViewById(R.id.textName);
if (enabled) {
text.setVisibility(View.INVISIBLE);
uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
mView.setSystemUiVisibility(uiOptions);
getSupportActionBar().hide();
customHandler.postDelayed(updateTimerThread, 1000);
} else {
text.setVisibility(View.VISIBLE);
uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
mView.setSystemUiVisibility(uiOptions);
getSupportActionBar().show();
customHandler.removeCallbacks(updateTimerThread);
if (mMenu != null) {
mMenu.setTitle(R.string.menu_start);
}
mView.setBackgroundColor(Color.WHITE);
}

Just put the element to stay visible after the ActionBar is visible and it worked!

Related

Android. How to show popup window directly above button

I need to show popup window directly above button. My button is placed inside scroll view, and popup always shown below button. Here is my code:
private void showPopup(View view, String text) {
if (infoPopup == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View popupView = layoutInflater.inflate(R.layout.popup_credit_request_passport, null);
TextView tvPopupText = popupView.findViewById(R.id.tv_popup_text);
tvPopupText.setMovementMethod(new ScrollingMovementMethod());
tvPopupText.setText(text);
FrameLayout flBackground = popupView.findViewById(R.id.fl_background);
flBackground.setBackground(new BubbleDrawable(getContext(), R.color.azure, 16, 16, 8));
infoPopup = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
infoPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
infoPopup.setOutsideTouchable(true);
infoPopup.showAtLocation(view, Gravity.TOP, (int) view.getX(), (int) view.getY());
} else {
dismissInfoPopup();
}
}
But it is not work. Please hep me.
Change Gravity to Top and reset your coordinates according to your requirement.
infoPopup.showAtLocation(view, Gravity.TOP, 0, (int) view.getY());
When you have to show the popup, find out the location of the button in the screen, then use the showAtLocation() method as Prachi Said using Gravity.TOP and the x,y coordinates of the button on screen...

How to detect when TextView exits/enters screen on Android?

I have this LinearLayout with few elements in it. Since there are many elements I have wrap it inside ScrollView. Inside this LinearLayout I have one TextView element and I won't to detect when it is not longer visible on screen while scrolling.
The thing that I am trying to do is to show this Activity with LinearLayout and make title of toolbar empty, then when user scrolls down, and this TextView exits screen, I want to set title in the toolbar to content of that TextView. And similar when this text view enters screen again to set title in toolbar to be empty.
I have no idea how to implement this and what listeners to use...
EDIT:
This is what I want to create: detect when text view exits or enters screen and update title in toolbar accordingly.
This will work for API23 or higher, but it gives you the basic idea.
ScrollView scroll = (ScrollView) findViewById(R.id.yourScrollView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
Log.e(TAG, "SCROLL = " + scrollY);
if(scrollY > txtView_Y){
setTitle("Hello");
}
else{
setTitle("I'm not there yet");
}
}
});
}
The value txtView_Y is the y position of your TextView. You will have to get that after the Activity has positioned all the views. You will probably need ViewTreeObserver.OnGlobalLayoutListener for that.

How do I get buttons not to sit on top of each other in RelativeLayout

I'm having a problem with this code. I need to dynamically add buttons to my layout. This code works fine, with one exception. The second button sits on top of the first. This must have something to do with LayoutParams, but I'm not sure what.
private void buttonmaker (Button button)
{
RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rlayout.addRule(RelativeLayout.CENTER_VERTICAL);
rlayout.addRule(RelativeLayout.ALIGN_LEFT);
rlayout.width = 100;
button.setId(Atom.count);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
atoms[id].getname();
TextView textview = (TextView)findViewById(R.id.textView2);
textview.setText(textview.getText()+String.valueOf(atoms[id].getname()));
}
});
if (Atom.count > 1) rlayout.addRule(RelativeLayout.RIGHT_OF,Atom.count-1); else rlayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button.setLayoutParams(rlayout);
RelativeLayout v = (RelativeLayout) findViewById(R.id.rlayout);
v.addView(button);
}
the problem is you set de button in the relativelayout, in this component the objects are being added one above the other, you try create linearlayout global with orientation vertical or horizontal depending on what you want and added buttons, and its all

How can I save the state and value of button press between views in a viewpager?

Right now I have an Adapter class, which extends PagerAdapter. Inside I have my InstantiateItem() method.
Here I have instantiate several buttons on the view:
#Override
public Object instantiateItem(ViewGroup parent, final int position) {
final int delPosition = position;
//Get the inflater
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflate the root layout
View view = inflater.inflate(R.layout.collection, null);
//Grab instance of image
ImageView imageView = (ImageView) view.findViewById(R.id.region);
//Sets a drawable as the content of this ImageView.
imageView.setImageResource(regionImages.get(position));
//scores toggle buttons
zero = (ToggleButton) view.findViewById(R.id.number_zero);
one = (ToggleButton) view.findViewById(R.id.number_one);
two = (ToggleButton) view.findViewById(R.id.number_two);
three = (ToggleButton) view.findViewById(R.id.number_three);
four = (ToggleButton) view.findViewById(R.id.number_four);
five = (ToggleButton) view.findViewById(R.id.number_five);
six = (ToggleButton) view.findViewById(R.id.number_six);
seven = (ToggleButton) view.findViewById(R.id.number_seven);
eight = (ToggleButton) view.findViewById(R.id.number_eight);
nine = (ToggleButton) view.findViewById(R.id.number_nine);
ten = (ToggleButton) view.findViewById(R.id.number_ten);
one.setOnClickListener(createClickListener(1));
two.setOnClickListener(createClickListener(2));
three.setOnClickListener(createClickListener(3));
four.setOnClickListener(createClickListener(4));
five.setOnClickListener(createClickListener(5));
six.setOnClickListener(createClickListener(6));
seven.setOnClickListener(createClickListener(7));
eight.setOnClickListener(createClickListener(8));
nine.setOnClickListener(createClickListener(9));
ten.setOnClickListener(createClickListener(10));
//Back Arrow Button
final ImageView back_button = (ImageView) view.findViewById(R.id.back_nav_arrow);
back_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//it doesn't matter if you're already in the first item
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
});
//forward button
final ImageView forward_button = (ImageView) view.findViewById(R.id.forward_nav_arrow);
forward_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//it doesn't matter if you're already in the last item
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
});
parent.addView(view, 0);
return view;
}
Here's the onClick action for the buttosn:
//onClickListener method that returns an interface
private View.OnClickListener createClickListener(final int value) {
return new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonValue = value;
ToggleButton clickedButton = (ToggleButton) view;
RadioGroup radioGroup= (RadioGroup) clickedButton.getParent();
for (int i = 0; i < radioGroup.getChildCount(); ++i) {
View nextChild = radioGroup.getChildAt(i);
if (!(nextChild instanceof ToggleButton)) {
continue;
}
if (nextChild.getId() != clickedButton.getId() || !clickedButton.isChecked()) {
ToggleButton tb2 = (ToggleButton) nextChild;
tb2.setChecked(false);
}
}
}
};
}
I want to be able to save the state of the button press (right now it's like a radiobutton and when the user clicks on it, the button state is pressed down until they move to another button) and the buttonValue between the views in the viewpager.
Ultimately, I want a view at the end of my viewpager where I can list all the scores for each view as well as have the user go back to the other views and still have their button state pressed so that they can confirm it while swiping between views.
I am facing two problems right now:
1) Only the last buttonValue is saved and not all the buttonValues for all the views. How can I achieve saving all the scores in all the views and not just the last score in the last view?
2) Going from one view to another and back, the button state is still pressed. However, if I go to a third view from the last point, the button state pressed is no longer true. How can I fix this?
I think it is time to use fragment, although you can achieve what you want without it but using fragment gives a lot of benefits like saving your value when your activity is destroyed (rotation) or using the life cycle callback methods or reusing it to your future projects. anyway:
1) Only the last buttonValue is saved and not all the buttonValues for
all the views. How can I achieve saving all the scores in all the
views and not just the last score in the last view?
save the state of your button in anyway you like for example in sharedpreference, database or a list of custom object as a member of your activity or adapter something like below:
class MyToggleButton{
boolean mState; // false = unpressed, true = pressed
}
and use List<MyToggleButton> to store them.
2) Going from one view to another and back, the button state is still
pressed. However, if I go to a third view from the last point, the
button state pressed is no longer true. How can I fix this?
you can use List<MyToggleButton> at instantiateItem to determine which button must have which state so you can set it correctly. when user change the state of any button you must update MyToggleButton state so that when ever you are going to look at List<MyToggleButton> find the correct one.

How I can implement a ExpandableListView in java (for Android) that make click display items?

I'm trying to implement a ExpandableListView in java that when click deployment space elements inside, not the typical wish list allows choosing an option. What I try to do is basically something like this
One can see that the spinner "wraps" to have items and does not have a list, Would I could help a little?
You can use ExpandableListView .. check this tutorial http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
You can easily create one on your own. Just create a Button and assign a Layout below. When the Button gets clicked set the View of you Layout to Visible and otherwise to Invisible. Additionaly you can set up and down arrows to the right of your Button.
final Button button = (Button) findViewById(R.id.button);
final LinearLayout linearLayout= (LinearLayout) findViewById(R.id.linearLayout);
linearLayout.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (linearLayout.getVisibility() == View.VISIBLE) {
linearLayout.setVisibility(View.GONE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.expand, 0);
} else {
linearLayout.setVisibility(View.VISIBLE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.collapse, 0);
}
}
});

Categories

Resources