Use XML-design of a listview multiple times - android

I've written an app and now want to design the UI. The problem is that I have the same data (well, not the same, but the same kind of data since it's an XML readout) and now I wanted to design it. But I don't think I can get the design how I want it programmatically. So I thought I design it once with XML and then copy the layout in the for loop for the other data as well. Is this possible?
If yes, how is it possible? I finished now my XML for one entry and now I want that all the other entries have the same style and layout as the others... Thanks!

There is simple way to achieve this.
There is include tag in xml file which you can use one layout in multiple screen design.
for eg. you had create header for application so no need to use same code in all file, just you can include that portion in each xml file.
here is simple example which may help you.
Create a Re-usable Layout
titlebar.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:background="#color/titlebar_bg">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gafricalogo" />
</FrameLayout>
Use the Tag:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
...
</LinearLayout>

You should be using an Adapter to define your ListView elements. In which case, you will define a layout for that adapter (using XML) that will apply to every element in your ListView.
Bottom line, use an Adapter, which will set the same XML layout for every element in your List.
(Check item #3)
http://docs.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters

Related

How can i merge two recycler views such that they scroll one after another?

I tried to implement this answer and it worked but only if the both list are prepopulated already, but in my case one list is prepopulated and the other is incremented/decremented dynamically.
Here is my layout structure
Black box represents the recycle list which can be incremented/decremented dynamically, and its main action is to show the users favorite fonts.
Red box represents all the fonts which are available and they are prepopulated.
i want both these list to flow each-other in scroll, such that user can not notice that these are actually two different list.
my xml code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:padding="#dimen/home_section_padding"
android:background="#color/background"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:id="#+id/editText"
android:textColorHint=" #C0C0C0"
android:hint="Paste"
android:textColor="#color/text_color"
android:inputType="text"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:text="Style"
android:textStyle="bold"
android:textColor="#color/text_color"
android:layout_marginBottom="10dp"
android:fontFamily="serif-monospace"
android:textAppearance="?android:textAppearanceLarge"
android:background="#drawable/round_shape_for_button"
android:onClick="fire"
android:id="#+id/button"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyelerViewFav"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</androidx.recyclerview.widget.RecyclerView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyelerView"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp" />
</LinearLayout>
There is 2 options, You can either use ConcatAdapter to combine both adapters in one adapter and use one RecyclerView (Recommended). Or use NestedScrollView to wrap both your RecyclerViews and make sure to set both RecyclerView heights to wrap_content.
But based on what you are trying to achieve, I would recommend to use ConcatAdapter(adapter1, adapter2) to wrap both your adapters and set that new adapter on one RecyclerView. You will still be able to control each adapter by itself Add, Remove and calling notifyDataSetChanged on the changed adapter will reflect changes on the RecyclerView.
I think you should use two lists in one RecyclerView adapter.And have two different view holders.Whenever you need to make a change just make the change in specific array list (1 or 2) and call notifyDatasetChanged() on the single adapter you have.
Something like this answer
Create 2 different xml file apart from activity_main.xml ,create 2 external java file associated with these 2 XML file for user handling code purposes. Give your desired layout for recycler view in those 2 XML file,map those XML file in MainActivity.java class using some java class which will extend RecyclerView.Adapter class and override some methods as onCreateViewHolder(),onBindViewHolder().To inflate the recycler view data ,create 1 more java file which will use some getter() & setter() inorder to populate the recycler view with data by the help of the class extending RecyclerView.Adapter class.

Android: Is there any specific place on the document to find explicit XML reference?

Android documentation for XML is too hard to find. Whatever I get is all Java related documents. I am a newbie to Android and trying to find XML references for Views and Widgets. For now, I am searching for Checkbox but it I am struggling to get XML reference every time for any component.
Can anyone help me to learn how to find and use the reference documents especially for XML and its attributes?
Million thanks.
The XML attributes are referred;
The CheckBox is created from inheriting CompoundButton, TextView etc.
Each view has its own XML attributes. So for CheckBox all the inherited values are applicable. For instance ChackBox has Button bahaviour, TextBox behaviour etc. Hence all above apply for the CheckBox. So if you want to add Button related behavior of CheckBox refer above 'Button Attributes'. If you want to add Text related behavior of CheckBox refer above 'TextView Attributes' etc.
Also refer https://developer.android.com/reference/android/R.styleable
Sample layout xml as follows;
<?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">
<LinearLayout android:id="#+id/llMainFill"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<ListView android:id="#+id/lvLog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>

Reuse a group of buttons across several fragments

I have a fragment defined by the following xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_example"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_example"/>
</RelativeLayout>
Now I would like to reuse the ImageButton (and other buttons I will add) in another fragment, ideally
without copying its definition
by keeping the definition in xml and not adding it programmatically
I.e. the goal is to overlay the same set of buttons in different fragments.
Is there a way to define all buttons in a separate xml file and load them programmatically on fragment creation?
Yes, You can.
Define all buttons in different xml say layout_buttons.xml
and add them in each fragment layout using
<include layout="#layout/layout_buttons" />

Declaratively defining fragments inside ViewPager

I have 5 unique pages of xml that are fairly complex. I want to put the 5 pages inside a ViewPager. All of the samples I can find simply put identical contents in each page via code. I want to declaratively define the xml in the viewpager like the xml pasted below. But this does not work - the app stops with this xml.
If I can't declaratively define it, then can I load individual xml pages into the viewpager? I can find no examples that do this.
thanks,
Gary Blakely
<?xml version="1.0" encoding="utf-8"?>
<fragment >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/imageView1"
android:src="#drawable/flashright" android:adjustViewBounds="true" android:scaleType="fitXY">
</ImageView>
</LinearLayout>
</fragment>
<fragment >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/imageView1"
android:src="#drawable/flashleft" android:adjustViewBounds="true" android:scaleType="fitXY">
</ImageView>
</LinearLayout>
</fragment>
If I follow, you want to declare everything in XML and avoid any programmatic initialization from within your Activity containing the ViewPager. The only way I can think of doing this would be to define 3 different Fragment classes which reference 3 different xml layouts. You could then embed them in your above xml, replacing each <Fragment> element with <FragmentX> <FragmentY> <FragmentZ> etc. Personally, I would rather write 3 xml layouts and create a single Fragment implementation that takes a single int argument to designate which layout to load, and then do the small amount of programmatic initialization necessary to make such a solution work. The rationale being that there is less duplicated code.
EDIT:
Another approach that might more fully address your requirements is to replace your <Fragment> tags with <Layout> tags and give them id's. Then in your Activity code, store refs to them, remove them from the View in onCreate() and finally add each of the stored references into programmatically created instances of Fragment which you then add to your ViewPager. Very ugly but thats the only way I know of to declaratively define everything in XML. You'll still have the problem that its not clear from the XML that these elements are nested within a ViewPager so personally I don't see the point. ListView and ListFragment operate with the same kind of implicit association though so its not unprecedented.

Avoid to rewrite code for activities with the same layout

I'd like to run several Activities on my Application;I'd like each Activity to have a Linear Layout and to show an image as heading; basically I'd like every layout to start like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/Grey"
>
<ImageView android:id="#+id/imageHeader" android:src="#drawable/tf_header" android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#color/Black" android:scaleType="fitXY"></ImageView>
Is it possible not to reapeat this code for each layout? Could I use themes or styles to avoid it? Thank you for your replies.
Yes thats possible use the <include> tag as described in Layout Tricks.
For your example I would add the image as <include> and have a LinearLayout in each Activity layout xml
One of my apps uses the same xml file for 30 or so classes, i just modify it in code to customize it. That approach may work, just leave every label in the xml blank and set it in the .class

Categories

Resources