I would like to display the following five times
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/message"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/add"/>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/separator_height"
android:background="#color/separator"/>
Thus I store it in data_item.xml and try calling it via
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/data_one"
android:layout_height="match_parent">
<include layout="#layout/data_item"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/data_two"
android:layout_height="match_parent">
<include layout="#layout/data_item"/>
</LinearLayout>
However if I do that, then all of the items inside data_item.xml layout are going to have duplicate Ids. How would I go about making sure that they do not have duplicate ids, or at least somehow retrieving them? Let's say I want to get title view text from data_three liner layout. How would I do that?
the first question is why you don't use ListView instead and set values in your adapter? this is better.
But if you have to design your layout in this way, you have to at first get reference from your LinearLayout, the find your view by using LinearLayout reference, as following:
LinearLayout dataOne = findViewById(R.is.data_one);
TextView dataOneTitle = dataOne.findViewById(R.id.title);
Related
I wrote an application and home page is LinearLayout and is in Fragment. In this LinearLayout there are two RecyclerViews and SearchBar etc. I want to scroll whole activity. I spent 2 days for it but cannot succeed. How can I do that in easy way? There are lots of adapters and connections in that LinearLayout. How can I achieve that without broke any code.
I want to scroll whole activity.
Thanks in advance.
Set Scroll/NestedScrollView as a parent view in xml to scroll whole layout.
Add below attribute in recycler view to stop recycler scroll:
android:overScrollMode="never"
Try below code:
<android.support.v4.widget.NestedScrollView
android:id="#+id/navigation_nested"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/margin_15">
<TextView
android:id="#+id/video_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_15"
android:drawableLeft="#drawable/ic_video_tutorial"
android:drawablePadding="#dimen/margin_20"
android:fontFamily="#font/poppins"
android:paddingLeft="#dimen/text_15"
android:text="#string/videos"
android:textColor="#color/blackTextColor"
android:textSize="#dimen/text_15" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
You just add this line:
//java
yourRecyclerView.setLayouManager(new LinearLayoutManager(this));
//kotlin
yourRecyclerView.layoutManager = LinearLayoutManager(this)
or from xml:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Notice that there is not only LinearLayoutManager. If you are using a Grid RecyclerView than you might want to use GridLayoutManager
I solved the problem.
Just write ScrollView over the LinearLayout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout>
.....
</LinearLayout>
</ScrollView>
I need to create i number of photo_view and give it a special amount every time (loop). number of photo_view in every time will be different. What exactly should I do in the code ?
my xml file :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/exit_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="20dp"
android:clickable="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_back_ltr" />
</android.support.design.widget.CoordinatorLayout>
Dynamically adding Views to a ViewGroup is what you are looking for. which is done by code not by XML.
In general, there are two ways to add a view dynamically,
either by inflating some layout file in a view then add this view to your parent viewgroup (LinearLayout for ex) by calling the add method on this parent view.
Or by defining a new object of that view and add all params needed to it then add it to the parent viewgroup.
See an example about way 1 here
See an example about way 2 here
in your case you could create a new layout.xml file contains only your custom view com.github.chrisbanes.photoview.PhotoView with all the needed params. then add an id for the parent linearlayout
<LinearLayout
android:"#+id/parentViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
And in your code, loop by the count and add the custom inflated view to parent. like this:
View inflatedView = View.inflate(context, yourViewXML, null);
LinearLayout parentViewGroup = findViewById(R.id.parentViewGroup);
for(int i = 0 ; i<count ; i++){
parentViewGroup.add(inflatedView);
}
I have been trying to add Button's below a GraphView, and all these elements are part of a Fragment. Tried many approaches but none of them worked properly.
This is the layout file for the Fragment (fragment_graph.xml).
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical"
/>
</FrameLayout>
And this is the Java code dynamically adding a graph and button, placed in the Fragment's onViewCreated (View view, Bundle savedInstanceState).
storageGraph = new LineGraphView(getActivity(), graphLabel);
storageGraph.addSeries(graphSeries); // More config calls follow
...
LinearLayout view = (LinearLayout)getView().findViewById(R.id.graph_fragment_layout);
Button button = new Button(getActivity());
button.setText("Test");
view.addView(storageGraph);
view.addView(button);
The Button is not visible though I have set orientation to vertical for the LinearLayout containing it.
EDIT - solved!
I found that nesting the graph under its own LinearLayout and the buttons under another LinearLayout, and both of these wrapped in a LinearLayout fixed the problem! The LinearLayout containing the graph must be weighted (I chose a weight of 0.8).
Layout file looks like:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_wrap"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/graph_buttons"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_navigation_previous_item"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_navigation_next_item"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
I've just tried it and it works. Perhaps your graph is taking all the available space, so added button is below the screen? Try to wrap your LinearLayout into a ScrollView and see if there is a button in the bottom.
I have a common layout (common.xml) which I want to include many times in another layout (layout_a.xml). But it only shows me just one time. Why?
common.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:drawable/alert_light_frame">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="#+id/imageView"
android:src="#drawable/test"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/imageView"
android:id="#+id/textView"
android:text="test"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" />
</RelativeLayout>
</merge>
layout_a.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/common" />
<include layout="#layout/common" />
</LinearLayout>
The ids when defined in XML must be unique. You are including two layouts that have views that contain the same id.
Here is how you would go about fixing it.
p.s. Unless there is more code that you are not including in your first layout file, that merge tag is useless.
As btse said, the ids in the XML must be unique.
It can be achieved in this way:
<include android:id="#+id/common1"
layout="#layout/common" />
<include android:id="#+id/common2"
layout="#layout/common" />
For information about how to access the elements inside those two included views, you can check out this blog post.
That's what I had done in my Project with Inflater.
test1is just a Layout made with a LinearLayout(Vertical) with text and a button and mainofferslayout in that case, the main layout with an image.
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_offers_display, container, false);
View inflatedLayout = inflater.inflate(R.layout.test1, (ViewGroup) view, false);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.mainofferslayout);
ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
I fixed by setting the layout_height of the RelativeLayout to 250dp since they are overlapped.
I have an activity with the associated activity.xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pattern_background"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:clickable="false" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
...
<LinearLayout
android:id="#+id/llComments"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</ScrollView>
<include
android:id="#+id/layoutComments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/widget_comentarios_fragment" >
</include>
In the LinearLayout with id llComments I introduce a fragment called CommentsFragment dynamically from the activity with the following code:
private Fragment commentsFragment;
private FragmentTransaction ft;
...
llComentarios = (LinearLayout) findViewById(R.id.llComments);
...
Bundle args = new Bundle();
args.putString(Constants.IDMODEL, getId());
commentsFragment = CommentsFragment.newInstance(args);
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.llComments, commentsFragment).commit();
The Fragment associated xml file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/transparent"
android:dividerHeight="10dp"
android:paddingLeft="#dimen/indicator_internal_padding"
android:paddingRight="#dimen/indicator_internal_padding" />
</LinearLayout>
The problem I cannot solve is to show in the activity more than one item (in fact I am showing one and a third items) in the list if I do not fix a height for the listview.It should adapt to as many items I add to the list in the fragment through the adapter. I have tried different combinations of heights in the layouts and views, but still does not work. Maybe I am annulating one with other.
Any help would be appreciated.
Thanks in advance
After some days I find out the exact problem. I was including a ListView into a ScrollView, and that's clearly not recomended by Android developers.
You can look for further info here: Why ListView cannot be used in a ScrollView?
Anyway, if you want to do it, you can do that with a hack. Take a look at these proposals:
How can I put a ListView into a ScrollView without it collapsing?
For me that worked. Problem solved.