Android: Same Listview display different ArrayAdapter - android

As the topic shown.
I made a TabbedActivity and implement an ListView under it.
I also made an string array to display inside the Listview.
The problem is : Same text will display on different tabbed
For example: There are two tabbed (number_decimal, number_roman),
It should display (1,2,3) (I,II,III)
But after I executed, both two pages are displayed 1,2,3.
What should I do to solve it, Thanks.
Also,
Which Java file should I provide?
SectionPagerAdaptor ? PlaceHokderFragment ? PageVIewModel
If there are similar questions, please let me know.
Belows are the code (XML) .
Here are the code (TabbedActivity and Listview):
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">
<!--
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="#+id/constraintLayout"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
-->
<!-- 結帳 -->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/vegetable">
</ListView>
<Button
android:id="#+id/button_jumptocash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:layout_marginBottom="20dp"
android:text="#string/jumptocash"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

you need to use one fragment inside each tab, then instead of list view inside of each fragment use always recyclerview whit its adapter,
list view is stupid and most of time create strange problem, especially when user scrolls

Related

how to constraintlayiut and include?

enter image description hereI want to put the processbar under the id iv_logo, but the progressbar is located at the top center, how can I do that?
Additionally, I am using databinding. Please let me know if there is any problem here.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/activity_start"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/progress"
android:layout_width="60dp"
android:layout_height="60dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_logo" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/iv_logo"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="#drawable/Logo"
android:layout_marginTop="40dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/iv_alogo"
android:layout_width="62dp"
android:layout_height="30dp"
android:src="#drawable/logo_pnt"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
It seems like you need to rethink your layout structure completely.
In addition, your iv_alogo has no vertical constraints, so it is not known exactly where it will be placed on the layout.
I offer you two options here:
Refuse include and place two AppCompatImageView in the main layout.
Place the progress bar inside the included layout.
In this case, it is not clear why you using include here.
your XML attribute is pointing on some View with tv_logo id
app:layout_constraintTop_toBottomOf="#+id/tv_logo"
but in second XML snippet (assuming part of #layout/activity_start) there is no View with such id... maybe try with one of these ids, which are placed in activity_start layout, so iv_logo or iv_alogo?
app:layout_constraintTop_toBottomOf="#+id/iv_logo"
//or
app:layout_constraintTop_toBottomOf="#+id/iv_alogo"
edit: due to posted image - it looks like you are looking for centering, not placing below another View. for centering use
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

Issue with edittexts when using inside <include> tag

Encountered a weird issue when using ViewBinding with Fragments.
fragment_one_layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/name"
layout="#layout/form_element_layout" />
<include
android:id="#+id/unit"
layout="#layout/form_element_layout" />
<include
android:id="#+id/quantity"
layout="#layout/form_element_layout" />
</LinearLayout>
form_element_layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
style="#style/TextViewRegularHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title"
android:textSize="#dimen/title_text_size" />
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text_forms_bg"
android:hint="#string/form_hint"
android:inputType="text" />
</LinearLayout>
After entering values in name.input , unit.input and quantity.input fields, I've navigated to fragment_two.
When returning from the fragment_two to fragment_one, the values in every field changed to the value of the quantity.input
Eg : name is "John", unit is "Kg" and quantity is "80". After navigating to fragment_two and returned, the name, unit and quantity values are changed to "80", Any solutions?
When saving layout values, Android stores the value of an EditText keyed by its id. Since you are using the same id for each of your EditTexts, each save overwrite the previous save and the last one overwrites all of them. If you want to do a quick test of this, move the "unit" include so its the last one. You should see "Kg" for all the values after doing this. You don't see this issue for the TextViews since they are considered static and are simply recreated each time.
Other than reworking your layout, a way around this is to explicitly save and restore the EditText values yourself.
Let's take the following simple layout that has a top EditText and one below. The key thing to note in this layout is that both EditTexts have the same id.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="I'm the top view!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#id/view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="I'm the bottom view!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
When first displayed, we see the following:
Upon rotation which forces recreation of the layout, we see this:
As you can see, the contents of the bottom EditText was used to fill in the top EditText. This is what you are seeing.

How to put ProgressBar inside of EditText and keep the bar right in it?

I want to have a ProgressBar inside of an EditText, but I want it to be right inside it and, preferably, in the center of the EditText. All the solutions I've come across so far do not really allow the bar to be inside of the borders. Also, I'm looking for as clean solution as possible, not just putting over 9000 constraints in ConstraintLayout.
It should look like this (picture is taken from android progressbar inside edittext):
But the actual result is either this:
or this (if I use smaller style for the bar):
My code is:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"/>
</FrameLayout>
Of course, I could just put some hardcoded values for height, but it's definitely a bad fix of the problem. Also, if I change the style of the bar to the smaller one, it's not a fix, since I want to have some control over it (and it doesn't look perfect, either). I'm planning to add something like error and success icons also, so the solution that fulfills this requirement is extremely welcome! So, if you have any clue about how to put it properly inside of the input field, I'll be grateful.
Thanks in advance, guys!
You can try:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="#+id/edit_text"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ProgressBar
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You can achive like this, Whenever you want to position children inside a layout FrameLayout is not a good layout to use. consider using RelativeLayout or better ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#null"
android:hint="Phone number goes here" />
<ProgressBar
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:padding="5dp" />
</RelativeLayout>
Happy coding!!

What layout elements to use for following type of layout

I am certainly newbie to Andorid Development, and have a knowledge of basic stuff, Relative Layout, Linear Layout, Intent, File Handling etc....
I need to build a project similar to some E-commerce app.
Here's an image of what I want.
How do I achieve the given view of products, as like in blogs or other websites.
Do I have to use List View?
And Please tell what do I have to use to make that "Add Filter Tags" section and how to achieve what I have shown in the picture.
Below is the code which will create skeleton for your UI requirement. You can modify it according to your need.
Your Activity/Fragment xml will look like :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="#+id/cl_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<com.google.android.material.chip.ChipGroup
android:id="#+id/entry_chip_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/cl_parent">
</com.google.android.material.chip.ChipGroup>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/entry_chip_group"
/>
</android.support.constraint.ConstraintLayout>
You Adapter xml for RecyclerView will look like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_product"
android:layout_width="100dp"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Name"
app:layout_constraintStart_toEndOf="#id/iv_product"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Information"
app:layout_constraintStart_toStartOf="#id/tv_name"
app:layout_constraintTop_toBottomOf="#id/tv_name" />
<TextView
android:id="#+id/tv_more_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="More info"
app:layout_constraintStart_toStartOf="#id/tv_name"
app:layout_constraintTop_toBottomOf="#id/tv_info" />
<TextView
android:id="#+id/tv_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
app:layout_constraintStart_toStartOf="#id/tv_name"
app:layout_constraintTop_toBottomOf="#id/tv_more_info" />
<TextView
android:id="#+id/tv_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tags"
app:layout_constraintStart_toStartOf="#id/tv_name"
app:layout_constraintTop_toBottomOf="#id/tv_data" />
</android.support.constraint.ConstraintLayout>
You should use Chips for your Filter tag. You can add them dynamically to your chip group. Below is the link for reference.
How to use Android Chips
A ListView would be the "default" way. I would also have a look at RecyclerView (a newer incarnation of the same idea). It handles scrolling and recycling the list elements as you scroll, which are all things you don't really want to do on your own.
You'll probably have a separate layout for the individual cards, probably mostly LinearLayouts (horizontal for image -> content, and then a vertical one to hold the content, and maybe a third horizontal one to list the tags).
For the tags, you might want to take a look at Material Design "chips", but honestly that's the part of this mockup that would have me the most concerned. You can make it look however you want, but I'm not sure what your designer means there exactly. Is that a static list of filtering options? Is that on a new page? In a dialog?
EDIT: And as for the top bar, check out the standard App Bar before reinventing the wheel there.
I would definitely go with Recyclerview or this tutorial for your products(images and the product description...) and FrameLayout for the top that includes logo and stuff and finally a regular RelativeLayout for the tags.

Android display messages in textview like normal sms

I am building an android application which will send and recieve messages from a server (using JSON and HTTP sender).
Now i thought that it could be cool if the messages were displayed just like when you send or recieve an SMS.
for example:
Picture
I have been trying to google around for some hours now, but all i find is how to send an sms from your android application.
Does anyone know if this is possible?
To create this layout you will need an activity with two fragments. The first fragment will just contain the view of your editor. The second fragment will be a ListFragment that displays the message results. The list fragment will inflate a custom view, that consists of a background image (which is the little speech bubble) and 2 TextView widgets that contain the date and message respectivley, and an ImageView with an onClick listener (or a ImageButton) for the star.
Here is a sample of the overall activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.your.package.MessageListFragment"
android:id="#+id/message_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<fragment
android:name="com.your.package.EditorFragment"
android:id="#+id/editor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
And here is a sample of the fragment that is the message entry interface
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#252525"
>
<Button
android:id="#+id/attach_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/picture_attach_button"
android:onClick="onAttachPicture"
/>
<Button
android:id="#+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/attach_photo"
android:padding="8dp"
android:text="Send"
android:onClick="onSendClick"
/>
<EditText
android:id="#+id/message"
android:layout_alignParentLeft="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/send_buton"
android:layout_centerVertical="true"
android:hint="Enter messaage.."
/>
<ImageView
android:id="#+id/attached_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_above="#id/message"
/>
And for your ListView fragment make a slight modification to the default list view to add some margins (there are other ways to do this)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#252525"
>
<ListView android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:marginLeft="8dp"
android:marginRight="8dp"
/>
<TextView android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="this is an empty list"
/>
And the adapter you attach to your ListView will have to return a custom UI element that looks like this see here how to implement a custom ListView:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/speech_bubble"
>
<ImageButton
android:id="#+id/favorite_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onFavoriteClick"
android:src="#drawable/star"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
<TextView android:id="#+id/response_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/favorite_button"
android:text="this is an empty list"
/>
The speech_bubble drawable should be a 9patch image that does not scale the corners or the indent of the bubble.
You do all of the applications back end work in the onClickListeners (i.e. onSendClick, onAttachPicture, onFavoriteClick). You can populate your custom ListView in any number of ways, but one way is to use a BroadCastReceiver that listens for incoming SMS messages, or define your own receiver in your backend code.
Listview + 2 different layouts for list items (received and sent). Also check this to help you implement a listview with two item types.

Categories

Resources