Remove container padding for BottomSheet Android - java

Have a space under the BottomSheet, I think it's the navigation bar what I hide before. I want remove this space. In Layout Inspector I noticed the R.id.container (FrameLayout) of BottomSheet have padding 48dp, bu idk what is it. Tried remove it, but it doesn't work. Screenshot
Layout Inspector
private void showAndHandleBottomSheetDialog() {
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
bottomSheetDialog.setContentView(R.layout.bottom_sheet_dialog_layout);
bottomSheetDialog.setCanceledOnTouchOutside(false);
FrameLayout bottomSheet = (FrameLayout) bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
RadioGroup radioGroupTip = bottomSheetDialog.findViewById(R.id.tip_list_custom);
ImageView buttonClose = bottomSheetDialog.findViewById(R.id.button_close);
ArrayList<TipItem> tipItemList = new ArrayList<TipItem>(Arrays.asList(TipItem.values()));
if (checkedButtonID != -1) {
radioGroupTip.check(checkedButtonID);
}
bottomSheetDialog.show();
buttonClose.setOnClickListener(view -> {
checkedButtonID = radioGroupTip.getCheckedRadioButtonId();
bottomSheetDialog.dismiss();
});}

The below approach will hide the systembars, if your problem's cause is the navigationbar then it should solve it.
private void showAndHandleBottomSheetDialog() {
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
// here you need to inflate your view and hide system bars, then set it.
// The added code for kotlin, kindly convert it to java
val bottomSheetView = LayoutInflater.from(requireContext()).inflate(R.layout.bottom_sheet_dialog_layout,
view?.findViewById(R.id.IdOfYourDialogParent)
)
bottomSheetView.systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
bottomSheetDialog.setContentView(bottomSheetView);

Maybe you can check parent of container has padding too.
I can't see where is the problem from code or picture.

The source code for BottomSheetDialog

Related

Kotlin - EditText in Inflated View keyboard show on back with black opacity

I inflated popup view with below code
but the problem is keyboard is shown backof popup
val inflater = it.context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
val menuPopup = inflater!!.inflate(R.layout.popup_search_new_cosmetics,null)
val popup = PopupWindow(menuPopup, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT, false)
popup.showAtLocation(menuPopup, Gravity.CENTER,0,0)
val imgClose = menuPopup.findViewById<ImageView>(R.id.imageClose)
menuPopup.findViewById<ImageView>(R.id.imageClose)
recycler = menuPopup.findViewById(R.id.editProductPage)
showKeyboard(menuPopup.findViewById(R.id.editTextSearch),this)
private fun showKeyboard(mEtSearch: EditText, context: Context) {
mEtSearch.requestFocus()
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
I suggest to use Alert dialog...
Use this code. Your problem will be solved
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

How do I make my Bottom Sheet Dialog dynamic?

it's the first time I am trying to implement a BottomSheetDialog into my Android Studio project. In order to get a little bit more familiar with the process I tried following this tutorial on Youtube: https://youtu.be/hfoXhiMTc0c. In my actual Java Class, the BottomSheet is activated when I am scanning an NFC-Chip containing different information. However I am not able to display the information from the Chip dynamicly on the Sheet. I guess that is due to the Sheet being static? How would I be able to display the information from the chip which is already stored in a variable in my Java class to be displayed in a textfield of the BottomSheet?
Any help is appreciated, thank you!
here's the code snippet of the java class where the BottomSheet is extended:
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
Scan.this, R.style.BottomSheetDialogTheme
);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(
R.layout.layout_bottom_sheet,
(LinearLayout)findViewById(R.id.bottomSheetContainer)
);
bottomSheetView.findViewById(R.id.addToCloset).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bottomSheetDialog.dismiss();
}
});
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetDialog.show();```
I am not familiar with BottomSheetDialog. But,
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(
R.layout.layout_bottom_sheet,
(LinearLayout)findViewById(R.id.bottomSheetContainer)
);
You should be able to replace above code with,
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(
R.layout.layout_bottom_sheet,
null
);
Now go to layout_bottom_sheet.xml layout file. According to your code it should have a linear layout with no id. Give it a id. I'll take that id as "test_ll".
Now below the above code you can,
LinearLayout ll = bottomSheetView.findViewById(R.id.test_ll);
After that you can add views dynamically to ll. For adding views dynamically to LinearLayout refer,
Add text view to Linear layout
Edit:
If you want to work with Views inside LinearLayout, you can do it by,
View view = ll.findViewById(R.id.view_id);
If your textview is in the ll,
TextView textView1 = ll.findViewById(R.id.tvcolor);
textView1.setText("Hello!!");
This will solve your problem.

Change toolbar from fragment

I am writing an app using jetpack recommended architecture, NavigationUI, and the navigation graph. So I have one main activity with a Toolbar, a BottomNavigationView and the NavHostFragment.
Everything worked nicely until now: I need to change the Toolbar to use a CollapsingToolbarLayout and hide the BottomNavigationView in one of my fragment.
I tried to add a navigation listener (as described here) to hide my Toolbar and BottomNavigationView, and in my fragment, I inflate the new Toolbar and call setSupportActionBar() on the main activity.
// in MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
if(destination.getId() == R.id.detailFragment){
bottomBar.setVisibility(View.GONE);
topBar.setVisibility(View.GONE);
}else{
bottomBar.setVisibility(View.VISIBLE);
topBar.setVisibility(View.VISIBLE);
}
});
// ...
}
public void changeToolbar(Toolbar toolbar){
getSupportActionBar().hide();
setSupportActionBar(toolbar);
}
// in DetailFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// ...
navController = NavHostFragment.findNavController(this);
AppBarConfiguration.Builder builder = new Builder(
R.id.accuracyFragment,
R.id.dataFragment,
R.id.magnetFragment,
R.id.settingsFragment);
AppBarConfiguration config = builder.build();
NavigationUI.setupWithNavController(toolbarLayout, toolbar, navController);
((MainActivity)getActivity()).changeToolbar(toolbar);
// ...
}
It almost works correctly, but:
when I navigate up or go to another fragment, the BottomNavigationView is not correctly displayed. It seems to be pushed down by the Toolbar.
the transition is ugly: the toolbar is visibly changing, I can see it disappearing before being changed
So the question is: is there another way to change/hide the navigation elements from the fragment? If not, should I create a new activity?
It's been a wild ride, but I finally found a solution. For the issue number 1, this is due to the way Android manages the fitsSystemWindows property propagation. For this to work correctly, I made a few changes to my layouts. I created a custom FitSystemWindowLinearLayout, which is simply a class extending the standard LinearLayout and overriding onApplyWindowInsets like this:
#Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
int childCount = getChildCount();
for (int index = 0; index < childCount; ++index) {
getChildAt(index).dispatchApplyWindowInsets(insets);
}
return insets;
}
My main activity now looks like this:
+-- CoordinatorLayout, fitsSystemWindows=false
+-- FitSystemWindowLinearLayout, fitsSystemWindows="false"
+-- Toolbar
+-- NavHostFragment, fitsSystemWindows="false"
+-- BottomNavigationView, fitsSystemWindows="false"
For the second issue, namely the transition being ugly, I mitigated that by adding a shared element to the transition.
All in all, I think it's easier to use a new activity for this kind of things, the NavigationUI falls a bit short for now.
Here are some resources that helped me:
https://medium.com/androiddevelopers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec
Android fitsSystemWindows not working when replacing fragments
Collapsing Toolbar problems with Status Bar and Bottom Bar. Fitssystemwindows="true" not working
fitsSystemWindows effect gone for fragments added via FragmentTransaction
https://www.reddit.com/r/androiddev/comments/aryxvu/fitssystemwindows_misbehaves_with_navigation/
-https://medium.com/androiddevelopers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec

Is it possible to hide the Toolbar and disable the scroll when swiping to specific page?

I would like to be able to hide the Toolbar on swipe to a specific tab and then lock it. Expanding it and then locking would also work for me although i prefer the first.
I have tried doing stuff like in the code below, but it gives rise to some snappy behavior because the page is scrolled of the screen originally. As soon as i set the scroll flags to 0, the whole page snaps back up and then locks the screen with the toolbar expanded which makes sense because with scroll flags set to zero, the page should not be able to scroll off the screen at all so it just snaps back up.
The page I am tending to is a chat page and in order to have a static text input bar at the bottom I really need to disable the scrolling for that page while making it possible for the others.
Can you guys think of some way to accomplish this?
Activity:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// add all the needed fragments for the tabs inside a Vector
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(4);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.clearOnPageChangeListeners();
viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListenerGroups(tabLayout, this, fab, appBarLayout));
viewPager.setCurrentItem(CURRENT_TAB);
}
public void disableToolbarScrolling()
{
logger.d("disabling scrolling toolbar");
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0); // clear all scroll flags
toolbar.setLayoutParams(params);
}
public void enableToolbarScrolling()
{
logger.d("enabling scrolling toolbar");
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); // clear all scroll flags
toolbar.setLayoutParams(params);
}
And then using a PageChangeListener to handle the event:
public TabLayoutOnPageChangeListenerGroups(TabLayout tabLayout, Activity activity, FloatingActionsMenu fab, AppBarLayout appBarLayout)
{
...
this.appBarLayout = appBarLayout;
...
}
private void enableScrolling()
{
((GroupActivity) mActivity).enableToolbarScrolling();
toolbarState = toolbarState_enabled;
}
private void disableScrolling()
{
((GroupActivity) mActivity).disableToolbarScrolling();
toolbarState = toolbarState_disabled;
}
#Override
public void onPageSelected(int position)
{
if(position == 0)
{
logger.d("pos 0");
disableScrolling();
appBarLayout.setExpanded(false);
}
else
{
logger.d("pos != 0");
enableScrolling();
}
}
EDIT 1:
I have tried setting toolbar visibility to both GONE or INVISIBLE. But they only make the toolbar white, giving a white bar at the top of the screen. Still allowing for the scrolling behaviour.
add this line to your code in onCreate
setSupportActionBar(toolbar); and then add
toolbar.setVisibility(View.GONE);
For hiding the toolbar you can just try :
getSupportActionBar().hide();

Adding layout views from an xml file to the content view

As a bit of background on this - I created a layout for a user interface that sat ontop of a GLSurfaceview. As I progressed I wanted to add adverts from admob on top of the GLSurfaceview. After some reading it seemed like the only way to do this was to create a new Layout view then add each dynamically created view to this. Then set the new Layout view as the content view. This means that all the views in my UI are dynamically created in java code.
My question is:
Is there any way you can use a view from an xml layout that is not set as the content view and somehow add it to the content view just like you would when dynamically creating a view?
I did try this but got null pointer exceptions from which ever view I tried to findViewById.
Any sort of write up explaining this would be helpful
EDIT added code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("******************************").build();
adView.loadAd(adRequest);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
final TextView Score = new TextView(this);
Score.setText(" 0");
RelativeLayout.LayoutParams scoreParams = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Score.setLayoutParams(scoreParams);
Typeface tf = Typeface.createFromAsset(getAssets(),"Fonts/BOYCOTT_.ttf");
Score.setTextSize(getResources().getDimension(R.dimen.textsize));
Score.setTypeface(tf);
Score.setTextColor(0xFFFFFFFF);
// Check if the system supports OpenGL ES 2.0.
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
if (supportsEs2)
{
// Request an OpenGL ES 2.0 compatible context.
mGLSurfaceView = new RBGLSurfaceView(this); new GLSurfaceView(this);
mGLSurfaceView.setEGLContextClientVersion(2);
final DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mGoogleApiClient = getApiClient();
mGLSurfaceView.setRenderer(new RBRenderer(this, Score, mGoogleApiClient),displayMetrics);
layout.addView(mGLSurfaceView);
layout.addView(Score, scoreParams);
layout.addView(adView, adParams);
//Set main renderer
setContentView(layout);
}
else
{
// This is where you could create an OpenGL ES 1.x compatible
// renderer if you wanted to support both ES 1 and ES 2.
return;
}
}
Ok so the above code is creating an adview, textview and surface view and adding them to the layout which is then getting set as the contentview - works great.
But is it possible to add the text view from an xml layout even if this xml layout is not set as the contentview?
Doing this doesnt work
final TextView test= (TextView) findViewById(R.id.test);
// set properties here etc
layout.adView(test);
because 'test' is null because the layout in which the view 'test' belongs to is not the set as the content view.
is it possible to add the text view from an xml layout even if this xml layout is not set as the contentview?
Yes, it's totally possible! Try this:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View textView = inflater.inflate(R.layout.text_view, null);
layout.addView(textView);
Hope this helps!

Categories