On Android, how programmatically I can change the ProgressBar, to show itself on different heights, as seen and work using the XML :
<ProgressBar ..... android:layout_marginBottom="100dp" ?
When I change on android:layout_marginBottom=”xxxdp” it change it location, in deferent height. But it fixed. I need to move it up and down during runtime,
I need to show this ProgressBar in different location from listView Bottom during application running.
The layout_* attributes refer to the parent layout's LayoutParams in code.
If the parent layout params is of type ViewGroup.MarginLayoutParams e.g. the parent is a LinearLayout or a RelativeLayout, you can modify it and call requestLayout() to have the changes take effect:
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)progressBar.getLayoutParams();
params.bottomMargin += 123;
progressBar.requestLayout();
If ProgressBar's parent is RelativeLayout, use RelativeLayout.LayoutParams.
LinearLayout.LayoutParams lp = progbar.getLayoutParams();
int margin = lp.marginBottom;
margin += your_value;
lp.setMargins(0, 0, 0, margin);
progbar.setLayoutParams(lp);
Related
I'm using Translucent Navigation bar by adding below attribute to make my app mobile nav menu looks like some Google apps where mobile nav bar is a bit transparent and content will visible through it.
<item name="android:windowTranslucentNavigation">true</item>
But my output is it
I don't want my views to go under it. I just need it when there is More content to scroll on screen like when there is a scrolling activity or in recyclerview. But at the end I need my activity views to not overlap by it.
Any suggestions please...
You may need to apply insets to have space between bottom of the device and navigationbar. RootView is the top layout on xml file.
rootView.setOnApplyWindowInsetsListener { view, insets ->
recyclerview.run {
setPadding(paddingLeft,paddingTop,paddingRight, insets.systemWindowInsetBottom)
}
insets
}
With this snippet you will have transparent navigationbar but your RecyclerView will always be above navigationBar.
And add
android:clipToPadding="false"
flag, this works with RecyclerView, and other scrollable views i guess.
You can check out this medium article or Chris Bane's tivi app to get familiar with insets.
Also put an example on github
What you could try for a static view is:
android:layout_marginBottom="?android:attr/actionBarSize"
If in a ScrollView this obviously won´t work but you can still try to play around wiht different layers inside the xml and fit a margin or padding if needed.
Otherwise it might be helpful if you post your xml file to actually see what you try to implement.
It is possible in both ScrollView and Recyclerview.
If there is scrollview in your Xml file then you should create
two other layout(ex: LinearLayout1 as Scrollview child Layout and
LinearLayout2 as LinearLayout1 child Layout) as a child layouts and
set bottom margin in the Secode child layout(LinearLayout2) same as
your bottom navigationBar height.
Well, here is the example how you can get bottom navigationBar height.
public int getNavigationBarHeight()
{
boolean hasMenuKey = ViewConfiguration.get(getApplicationContext()).hasPermanentMenuKey();
int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0 && !hasMenuKey)
{
return getResources().getDimensionPixelSize(resourceId);
}
return 0;
}
Note: The height of the navigationBar is in px. So, you have to set margin of your child layout in px.
For Recyclerview, you can give bottom margin(RunTime) of parent
layout in adapter class for the last item of the recyclerView
same as navigationBar height.
Example:
if (position==adapter_dueModelList.size()-1){// the list item
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 0, 132);//give margin same as a navigationBar height.
((ViewHolder) holder).linear_layout.setLayoutParams(params);
}
I have an app that uses a circular progress bar.
I had like to change the opacity of the whole layout until it finished loading.
Something like this:
I manage to change the opacity of the layout by using:
cl = findViewById( R.id.Discover_Layout );
cl.setAlpha( 0.45f );
However, it changes the opacity of the ProgressBar as well.
I tried to use:
cl = findViewById( R.id.Discover_Layout );
cl.setAlpha( 0.45f );
pb_Discover = findViewById( R.id.pb_Discover );
pb_Discover.setVisibility( View.VISIBLE );
pb_Discover.setAlpha( 1f );
But it still kept the ProgressBar with Opacity.
How can I exclude the ProgressBar from the layout Opacity?
Thank you
I am assuming that the Discover_Layout is the parent layout which contains the pb_Discover progress bar. so changing the parent opacity will reflect in child views too.you can create a framelayout as parent layout and add Discover_Layout and pb_Discover as 2 separate child views. so the change in opacity of Discover_Layout does not affect the pb_Discover progressbar.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame_layout_badge"
android:layout_width="match_parent"
android:layout_height="match_parent">
< ----Discover_Layout---/>
< ----pb_Discover---/>
</FrameLayout>
Bytheway its better to name view ids to start with a small letter
I think your discover layout is parent of progress view.
You have to make background view with opacity.
I making a drawing pad for teachers, i want to include geometrical shapes in it, there is a button on click of which a shape is displayed.
What i am doing now is i created an image view already and set its visibility to gone, on button i am making it visible.
<ImageView
android:id="#+id/ivReact"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/rectangle"
android:visibility="gone"
android:layout_centerInParent="true"
/>
i want the previous image to be there, when a button is clicked the same image should be added twice in the layout and if the button clicked again it should be added again and go on.
is it possible to do that?
Use this code inside onclick
ImageView imageview = new ImageView(MainActivity.this);
RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativeLayout);
LinearLayout.LayoutParams params = new LinearLayout
.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// Add image path from drawable folder.
imageview.setImageResource(R.drawable.demo_new_image);
imageview.setLayoutParams(params);
relativelayout.addView(imageview);
create a recycler view and put a image view there. Create a arraylist. Onclick add same image to the arraylist and apply adapter.notifyDataSetChanged().
You have to update image programmatically when button is clicked:
imageView.setImageResource(R.drawable.your_image);
EDIT:
If you have ImageViews in LinearLayout, you can simply create new and add to layout:
ImageView imageView = (ImageView) LayoutInflater.from(context).inflate(R.layout.image_view_layout_name, null);
linearLayout.addView(imageView);
This seems like it should be trivial, but I can't figure out how to set the layout_gravity for a FrameLayout's child programmatically for the life of me.
There's a ton of similar questions on SO, but after trying a good 10 of them, none of the setGravity, new FrameLayout.LayoutParams, etc. solutions seem to work.
Below is a simplified version of what I'm trying to achieve.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextureView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</FrameLayout>
However, for external reasons, I'm adding the TextureView to the FrameLayout programatically.
mCameraPreview = new CameraPreview(this, recordMode);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview_wrapper);
preview.addView(mCameraPreview);
When I try to set the layout_gravity on the mCameraPreview, it only gives me an option for gravity, which I don't want.
Is there something I'm missing here?
The layout_gravity attribute lands on the FrameLayout.LayoutParams, not on the view itself. You'll need something like:
mCameraPreview = new CameraPreview(this, recordMode);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview_wrapper);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT,
Gravity.BOTTOM);
preview.addView(mCameraPreview, params);
Can you overlay a view on top of everything in android?
In iPhone I would get the new view set its frame.origin to (0,0) and its width and height to the width and height of self.view. Adding it to self.view would then cause it to act as an overlay, covering the content behind (or if it had a transparent background then showing the view behind).
Is there a similar technique in android? I realise that the views are slightly different (there are three types (or more...) relativelayout, linearlayout and framelayout) but is there any way to just overlay a view on top of everything indiscriminately?
Simply use RelativeLayout or FrameLayout. The last child view will overlay everything else.
Android supports a pattern which Cocoa Touch SDK doesn't: Layout management.
Layout for iPhone means to position everything absolute (besides some strech factors). Layout in android means that children will be placed in relation to eachother.
Example (second EditText will completely cover the first one):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/root_view">
<EditText
android:layout_width="fill_parent"
android:id="#+id/editText1"
android:layout_height="fill_parent">
</EditText>
<EditText
android:layout_width="fill_parent"
android:id="#+id/editText2"
android:layout_height="fill_parent">
<requestFocus></requestFocus>
</EditText>
</FrameLayout>
FrameLayout is some kind of view stack. Made for special cases.
RelativeLayout is pretty powerful. You can define rules like View A has to align parent layout bottom, View B has to align A bottom to top, etc
Update based on comment
Usually you set the content with setContentView(R.layout.your_layout) in onCreate (it will inflate the layout for you). You can do that manually and call setContentView(inflatedView), there's no difference.
The view itself might be a single view (like TextView) or a complex layout hierarchy (nested layouts, since all layouts are views themselves).
After calling setContentView your activity knows what its content looks like and you can use (FrameLayout) findViewById(R.id.root_view) to retrieve any view int this hierarchy (General pattern (ClassOfTheViewWithThisId) findViewById(R.id.declared_id_of_view)).
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id = "#+id/Everything"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- other actual layout stuff here EVERYTHING HERE -->
</LinearLayout>
<LinearLayout
android:id="#+id/overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</LinearLayout>
Now any view you add under LinearLayout with android:id = "#+id/overlay" will appear as overlay with gravity = right on Linear Layout with android:id="#+id/Everything"
You can use bringToFront:
View view=findViewById(R.id.btnStartGame);
view.bringToFront();
The best way is ViewOverlay , You can add any drawable as overlay to any view as its overlay since Android JellyBeanMR2(Api 18).
Add mMyDrawable to mMyView as its overlay:
mMyDrawable.setBounds(0, 0, mMyView.getMeasuredWidth(), mMyView.getMeasuredHeight())
mMyView.getOverlay().add(mMyDrawable)
I have just made a solution for it. I made a library for this to do that in a reusable way that's why you don't need to recode in your XML. Here is documentation on how to use it in Java and Kotlin. First, initialize it from an activity from where you want to show the overlay-
AppWaterMarkBuilder.doConfigure()
.setAppCompatActivity(MainActivity.this)
.setWatermarkProperty(R.layout.layout_water_mark)
.showWatermarkAfterConfig();
Then you can hide and show it from anywhere in your app -
/* For hiding the watermark*/
AppWaterMarkBuilder.hideWatermark()
/* For showing the watermark*/
AppWaterMarkBuilder.showWatermark()
Gif preview -
I have tried the awnsers before but this did not work.
Now I jsut used a LinearLayout instead of a TextureView, now it is working without any problem. Hope it helps some others who have the same problem. :)
view = (LinearLayout) findViewById(R.id.view); //this is initialized in the constructor
openWindowOnButtonClick();
public void openWindowOnButtonClick()
{
view.setAlpha((float)0.5);
FloatingActionButton fb = (FloatingActionButton) findViewById(R.id.floatingActionButton);
final InputMethodManager keyboard = (InputMethodManager) getSystemService(getBaseContext().INPUT_METHOD_SERVICE);
fb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// check if the Overlay should be visible. If this value is false, it is not shown -> show it.
if(view.getVisibility() == View.INVISIBLE)
{
view.setVisibility(View.VISIBLE);
keyboard.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
Log.d("Overlay", "Klick");
}
else if(view.getVisibility() == View.VISIBLE)
{
view.setVisibility(View.INVISIBLE);
keyboard.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
bringToFront() is super easy for programmatic adjustments, as stated above. I had some trouble getting that to work with button z order because of stateListAnimator. If you end up needing to programmatically adjust view overlays, and those views happen to be buttons, make sure to set stateListAnimator to null in your xml layout file. stateListAnimator is android's under-the-hood process to adjust translationZ of buttons when they are clicked, so the button that is clicked ends up visible on top. This is not always what you want... for full Z order control, do this: