Navigation Drawer: change selected item after replace another fragment - android-studio

I'm using android default navigation drawer activity. Assume i have fragment A and fragment B, there is one button in fragment A and it will redirect(replace) from fragment A to B after the button is clicked.
But the selected item in navigation drawer is still highlighting fragment A. Is there any method to change it to highlight the fragment B instead of A?
#Override
public void onClick(View v) {
if(carplate.getText().toString().equals("")){Toast.makeText(getActivity(),"Please enter your car plate number",Toast.LENGTH_SHORT).show();}
else{
Status status = new Status();
FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame,status);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
this function is under fragment A. I need the selected item automatic change to fragment B(Status) after i pressed the button in fragment A.

yes it is very easy
first, you have to get Navigation View and then get the menu from that navigation view and high light/show selection on the basis of the item index which is start from the 0. Place the following line of code in the onclick method.
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().getItem(0).setChecked(true);
and if you want to open the fragment use the following lines of code with the stated code lines
getSupportFragmentManager().beginTransaction()
.replace(R.id.mainFragment,new yourNewFragment()).commit()
I hope this is helping

Related

Android: Custom bottom sheet dialog

How can I implement following design functionality with android standard component bottom sheet:
Image when Bottom sheet dialog fragment will appear:
Image when user scrolled to up to view bottom of content:
I will use ViewPager to scrolling header images and RecyclerView to showing descriptions and other informations. And parallax effect to ImageView(which are placed in ViewPager) when scrolling content vertically. Have a minimum height of the ImageView(and ViewPager), user can't collapse fully it (Look to second screenshot, which is user scrolled until the end of content).
I want stop scrolling ImageView when it will reach to minimum height(look to second one Screenshot), but the content of below ImageView should be continue scrolling
This can be done with an if statement in an on scroll view such as shown below:
ScrollView scrollView = findViewById(R.id.scrollView); //Adjust for your code
ImageView imageView = findViewById(R.id.imageView); //Adjust for your code
boolean imageIsHidden = false;
int threshold = 250;
scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY = rootScrollView.getScrollY();
if(scrollY >= threshold){
imageIsHidden = true;
//Move image outside of scroll view so it doesn't scroll
}
else if(scrollY < threshold && imageIsHidden){
imageIsHidden = false;
//Move image inside of scroll view so it does scroll
}
}
});
What this does is has a boolean called imageIsHidden and an integer called threshold. Threshold is where you want it to make it disappear. You will need to play around with this value to find a sweet spot.
You will also need to implement moving the image inside and outside of the scroll view as well in the if and if else statement.

Layout snaps to visible (first time only) instead of slide animation

I'm creating a 2 row info bar in the bottom of the screen.
At first only the top row will be visible.
Press an 'expand' button, the 2nd row will slide up from
below. Now both the rows are visible.
Pressing same button again, the bottom row slides down, and
only the 1st row is visible.
This scenario is very common, and after checking different implementations, I've come to this simple one which works almost perfectly:
2 layouts in 1 > make bottom GONE > onClick - make bottom VISIBLE and apply slide up on entire Bar > onClick - apply slide down on Bar and make bottom GONE
Below is the onClick code:
mInfoBar = (RelativeLayout) mRootView.findViewById(R.id.infoBar);
mInfoBottomRow = (RelativeLayout) mRootView.findViewById(R.id.infoBottomRow);
mBtnExpand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInfoBottomRow.getVisibility() == View.GONE) {
mInfoBottomRow.setVisibility(View.VISIBLE);
TranslateAnimation slideUp = new TranslateAnimation(0, 0, mInfoBottomRow.getHeight(), 0);
slideUp.setDuration(200);
mInfoBar.startAnimation(slideUp);
} else {
TranslateAnimation slideDown = new TranslateAnimation(0, 0, 0, mInfoBottomRow.getHeight());
slideDown.setDuration(200);
slideDown.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
mInfoBar.clearAnimation();
mInfoBottomRow.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
mInfoBar.startAnimation(slideDown);
}
}
});
The problem is, the very first time the button is pressed, the info bar (with both top and bottom row) snaps to its position (without the sliding effect).
All other subsequent button press works perfectly...
2nd press - the bar slides down to show the top bar resting on the bottom of the screen and the bottom row gone.
3rd press - the bar slides up to show both top and bottom row.
and so on...
Can't understand why setVisibility(VISIBLE) is snapping to view only first time, or why the sliding effect is not showing the first time.
I'd rather not change the animation implementation since it's giving me desired effect all but once.
So any insight within this will be really appreciated.
Your hiddenView's height is 0 for the first time as far as i guess. You should make visible your hiddenView in your layout.
use
android:visibility="visible"
instead of
android:visibility="gone"
in your hiddenView.
Get the height and make it gone by following code:
hiddenView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
height = hiddenView.getHeight();
hiddenView.getViewTreeObserver().removeGlobalOnLayoutListener( this );
hiddenView.setVisibility( View.GONE );
}
});
Use this height to animate.. :)

Resetting scroll bar in JavaFX on button click

I have used two scroll bars for controlling brightness and contrast on image after the use i want to reset the scroll bars to their initial value with a button click
I am not getting any links for that to reset scroll bars on button clicks in JavaFX?
The question somewhat unclear. Need explanation of "not getting any links". Some sample code would be helpful. Based on an assumption, try this:
final ScrollBar scrollBar = new ScrollBar();
Button btn = new Button("Reset");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
scrollBar.setValue(scrollBar.getMin());
// Or if you have stored initial value somewhere use it
scrollBar.setValue(myInitialValue);
}
});

how to preserve listview selected/ clicked item color while scrolling?

this is really a strange problem i came across several google searches which returned in vain .
Well i have a list view in which the selected item color should be changed while user click on the item.
This post helped me to change the list view selected item color. Now i am able to change the color while the item is clicked . But if i scroll the list view the color jumps out of selection.
https://stackoverflow.com/a/12080202/1657093
If a item is selected say for instance the listview item on first row the last item is also gets selected while scrolling the item color keeps on moving to the item which i never selected.
here is the list view
<ListView
android:id="#+id/loadedlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/horizontalScroll"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Title"
android:layout_toLeftOf="#+id/sidepanel"
android:scrollingCache="false">
</ListView>
I use array adapter to populate the list view
ringlist.setAdapter(new ArrayAdapter
(this,android.R.layout.simple_list_item_1,rings));
and use list view's on click method to set the color ,
ringlist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos,long id) {
// TODO Auto-generated method stub
try{
String con;
String s = ringlist.getItemAtPosition(pos).toString();
con = s.toLowerCase();
String selectedFromList = con;
int resId = getResources().getIdentifier(selectedFromList, "raw",
"com.example.android");
}
if (row != null) {
row.setBackgroundColor(Color.BLACK);
}
row = view;
view.setBackgroundColor(Color.GREEN);
}
}
});
the above code does the job but while scrolling the list something goes wrong
please advice.
This is common issue of Listview in android.
When we use checkbox/radio button in Listview Item after scrolling down or up, the selected position does gets changed at some different places.
The only solution to this till now is, we need to maintain the selected position in a arraylist(or to any structure you are comfortable).
And as you scroll up/down the list view, the getview does gets called.
So write code in your getview() to make checkbox items selected from arraylist.
Also dont use if-else condition in getview for convertview.
Store clicked/selected positions in a set or array in your onItemClick() method. Write code to show clicked/selected items in getview() method of your adapter using stored positions.

GXT: UiBinder new instance of a Widget

I have a TextField form inside a window. Created with UiBinding. Next to the TextField is a button. I wanted to know if it was possible to create a new TextField widget when that button was pressed using UiBinder?
This is what I have:
Window class:
#UiField
TextField text;
#UiField
HorizontalPanel hPanel;
....
#UiHandler("addText")
public void onClick(SelectEvent event){
hPanel.add(text);
}
My UiBinder file:
<gxt:Window ...(generic setup)...>
<g:VerticalPanel>
<gxt:FramedPanel>
<container:VericalLayoutContainer>
<container:child>
<g:HorizontalPanel ui:field="hPanel">
<form:FieldLabel text="Text">
<form:Widget>
<form:TextField ui:field="text"/>
</form:Widget>
</form:FieldLabel>
<button:TextButton ui:field="addText"/>
</g:HorizontalPanel>
</container:child>
</container:VericalLayoutContainer>
</gxt:FramedPanel>
</g:VerticalPanel>
</gxt:Window>
When I click the button it all it does is move the button from the right side of the text field to the left. I have more textfields in the window so I played around to see what it was really doing. It's taking that field and just moving it next to the button.
Is there a way I can create a new TextField underneath the original?
Probably LazyDomElement will help you.

Resources