Is it possible to add a View to a Fragment dynamically? If it is, how can i do that?
Is it possible to add a View to a Fragment dynamically?
Yes.
If it is, how can i do that?
The same way you would add a View anywhere else: call addView() on the View's parent container.
HomeFragment frag = new HomeFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragLogin, frag);
ft.setCustomAnimations(R.anim.right_in, R.anim.right_out);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
Here,R.id.fragLogin is the id of your first fragment which you have declared in your activities xml and HomeFragment is your second Fragment.
Related
Can anyone tell how make Search View hint in centre? Thanks in advance.
If you are using androidx. Use this code
val searchEditText = searchView.findViewById(androidx.appcompat.R.id.search_src_text) as EditText
searchEditText.gravity = Gravity.CENTER
To center the hint and query text use a costume style for your search view :
<style name="AppSearchView" parent="Widget.AppCompat.SearchView" >
<item name="android:textAlignment">center</item>
</style>
and then apply this style to your search view :
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
app:theme="#style/AppSearchView" />
To center the hint on the SearchView you need to center the gravity in the EditText contained in the SearchView.
You can find that specific view by using the Layout Inspector in Android Studio (Tools->Android->Layout Inspector). When you click on the search view in your UI, you can see it has a SearchView$SearchAutoComplete. That's an inner class in the SearchView which extends AutoCompleteTextView, which extends EditText. So, that's the view you want to change. Observe in the property-value pane of the Layout Inspector that the id is id/search_src_text so you can use that to get the view and center the gravity, like this:
int id = getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchEditText = (EditText) mSearchView.findViewById(id);
if (searchEditText != null) {
searchEditText.setGravity(Gravity.CENTER);
}
Since the id of the EditText isn't officially documented, there's a null check in there just in case the id changes or isn't consistent everywhere.
For android.support.v7.widget.SearchView, use the following:
public static void centerHintText(SearchView searchView){
EditText searchEditText = (EditText) searchView.findViewById(R.id.search_src_text);
if (searchEditText != null) {
searchEditText.setGravity(Gravity.CENTER);
}
}
I followed Cody's example but I would like to add onto Cody's answer above that it might actually be better to cast the found view to SearchView.SearchAutoComplete (a public static class) instead of EditText, since that's the view directly associated with R.id.search_src_text. This is to avoid any future problems if the Android team decides to swap out the inherited EditText class for another class.
See the associated view here: https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/search_view.xml
I want to show or hide a subview in a fragment when it is shown or hidden, but no way to check the fragment is visible to user.
In android native, it seems to have a method, but Xamarin no.
Do you have any special way to get it?
I can see only a property for fragment.
public override bool UserVisibleHint {
get {
return base.UserVisibleHint;
}
set {
base.UserVisibleHint = value;
}
}
But I need a method to check this.
thanks.
You can use the IsVisible property on the Fragment (see: https://developer.xamarin.com/api/property/Android.App.Fragment.IsVisible/)
Additionally, you can override the OnHiddenChanged method on your fragment class to get notified when the Fragment's visibility changes
it seems to have a method, but Xamarin no.
What method are you referring to?
I have a scenario where I need to set focus dynamically on search icon in browse fragment .For that I need to get reference of the search icon.Any idea how it can be done.
Thanks
BrowseFragment class accesses search icon using title view's getSearchAffordanceView()
private final BrowseFrameLayout.OnFocusSearchListener mOnFocusSearchListener =
new BrowseFrameLayout.OnFocusSearchListener() {
#Override
public View onFocusSearch(View focused, int direction) {
// If headers fragment is disabled, just return null.
if (!mCanShowHeaders) return null;
final View searchOrbView = mTitleView.getSearchAffordanceView();
.
.
.
Since mTitleView is BrowseFragment's private member, you cannot get search icon reference directly. The only properties you can control for search affordance in the fragment title are: visibility and color. Visibility is controlled by the presence of a search listener.
I try to implement an application with JavaFX 8 where I have a ListView of GridPanes, where each GridPane consists of 2 other controls (ChoiceBoxes). The Gridpanes in the ListView are dynamically added by the user during runtime. What I like to achive is to add a listener to the ChoiceBox such that I know to which ListView item/index the selected choicebox belongs to if a user makes a choice.
My Problem is now that I cannot get the index of the ListView when the user selects an item of the choicebox, because the item (i.e. the Gridpane) in ListView is not (automatically) selected or focused in this case.
Can anybode help me with this?
Thanks for the comments, I got the solution now by looking for the parent of the choicebox (note that paneList is an ArrayList in which I stored the references to the GridPanes which where added by the user)
// define Listener for choiceBox
choiceBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
GridPane parent = (GridPane) choiceBox.getParent();
// keep track of selected choice
selectedChoice = newValue;
logger.debug("selected choice: {}", selectedChoice);
// keep also track of parent GridPane
selectedGridpane = paneList.indexOf(parent);
logger.debug("selected GridPane {}", paneList.indexOf(parent));
});
In Android app, I am using a menubar with tabs attached to it. And each tab has a fragment.
I am trying to set visibility of components (like button) in a fragment to invisible at runtime. But it is not taking effect.
What am I missing?
Main activity class calls a method in Fragment class to set visibility of the button.
But it is not taking effect.
In fragment class,
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fm_hd_fragment, container, false);
multicastUp = (Button) view.findViewById(R.id.FmHdMulticastUp);
multicastDown = (Button) view.findViewById(R.id.FmHdMulticastDown);
multicastDisplay = (TextView)view.findViewById(R.id.FmHdCurrentMulticast);
.
.
.
}
public static void showcontrol(){
multicastDisplay.setVisibility(View.VISIBLE);
multicastDown.setVisibility(View.VISIBLE);
multicastUp.setVisibility(View.VISIBLE);
}
In class that wants to update this fragment
FragmentWrapper.showcontrol();
Figured out the issue...
I was trying to update activity from Service directly.
When I use intents from service to update Activity, the UI update works fine.