I am creating an app that uses a custom camera. The idea is that I just define a Framelayout in my xml file and them programmatically add the SurfaceView (this is the camera preview) and some other ImageViews (e.g. a shutter button, flash button...)
I managed to get the SurfaceView working but now I am a bit stuck. I want to add multiple imageview to the frame layout, but how can I get them setup correctly. I am referring to their location in the frame layout.
Can I create a relativelayout and add that to my frame layout programmatically?? If so, how do I do that?
Please, can anyone give me some tips??
Thanks!
FrameLayou can have only one direct child. You can try this
SurfaceView surface = .....;
FrameLayout frame = findViewById(R.id.frame);
RelativeLayout relativeLayout = new RelativeLayout(this);
frame.addView(relativeLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
relativeLayout.addView(surface, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// here you should add your images to relativeLayout
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 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);
I use the following codes to set background of view such as TextView, LinearLayout, and it works perfectly.
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setInt(R.id.textview, "setBackgroundColor", Color.parseColor(widgetTextColor.toString()));
But I try to set the background of CardView, it didn't works. I have tried
views.setInt(R.id.cardview, "setCardBackgroundColor", Color.parseColor(widgetTextColor.toString()));
Any workarounds?
Based on the developer documentation in this link, u cannot use cardview for App widgets
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
and a discussion about this already been over here What views can i use in an appWidget?
How can I add VideoView programmatically according to the number of videos in specified folder? Any suggestions please or some links to learn from. I am new to Android. I don't want to add VideoView in activity layout.
you can add video view pragmatically like this:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
VideoView video = new VideoView(this);
video.setVideoURI("yourURI");
video.setLayoutParams(new FrameLayout.LayoutParams(550, 550));
layout.addView(video);
I have a tablerow in a tablelayout. Now, I can get tablerows without problem. Tablerow contains a linearlayout and the linearlayout contains views that I need. Is there any possibility to get the linearlayout, and then the from it views?
I cannot do this, because View doesn't have getChildAt method:
View layout = row.getChildAt(j);
EditText first = (EditText)layout.getChildAt(0);
I cannot do this either, because it leads to runtime error when converting View to Layout:
LinearLayout layout = (LinearLayout)row.getChildAt(j);
EditText first = (EditText)layout.getChildAt(0);
If someone has better solution then post it and I'll mark it as solution.
The way I solved this was by adding ID to layout before adding it to the tablerow:
layout.setId(id);
row.addView(layout);
And then getting layout by ID:
View layout =row.getChildAt(j);
LinearLayout layoutLL = (LinearLayout) findViewById(layout.getId());
EditText first = (EditText)layout1.getChildAt(0);
I couldn't post XML-file because layout is made programmatically.