In my android application I will show user to enter some input values (in main layout) and when presses a button it fetches data from a website(uses AsyncTask). And after fetching i want to display output in a separate layout.
Layout should be like this:
----------------------------
Status: Success
----------------------------
Details are as follows:
----------------------------
Name of cust. ABCDEFG
Age 16
Total days 365
Present 300
Absent 65
Salary 10000
etc..
----------------------------
BACK
----------------------------
For first two rows, a LinearLayout with TextViews in it is in my mind. For last button "BACK", it is going to be displayed in a LinearLayout. For details i am going to use TableLayout. But this details section is dynamic. that is number of rows varies. When user presses "BACK" button he can do a new fetching and details of new fetch will again be displayed.
I am going to use ScrollView for details section to allow users to view contents if there is larger rows. So I can create the layout dynamically by using code. But I donot know what to do when user presses BACK button and again fetches. Will this already created design stays in memory ? Or any better approach or suggestions ? Also the text in first column of the details section may contain larger texts. So what to do there?
I would advice against the use of tableview, have had several problems with it in the past, and in your case a relative layout will do just as well. Example below highlights what you would need for every row of your details.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
Regarding larger test, assuming the text is long, and not just large font size, you can add
android:ellipsize="end"
to you TextView to make it cut the text if its getting to long, and end it with "...". For that to work you need to tell the first textView1 in the example above to:
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/textView2"
and if you dont want the text cut, just remove the android:ellipsize line.
Related
I have two TextViews. Currently, When Talkback is enabled, the user has to swipe twice to read both TextViews. But I want the screen reader to read the TextViews one after another without any user input (Like WhatsApp reading LastSeen and Chat name in one go). How can I achieve this? Any help would be appreciated
You can set the content description to the layout containing these two views and set individual views as not important for accessibility to combine them.
For example:
<LinearLayout …
android:id="#+id/combinedLayout"
android:contentDescription="Last seen / chat name"
<TextView …
android:text="Last seen"
android:importantForAccessibility="no" />
<TextView …
android:text="Chat name"
android:importantForAccessibility="no"/>
</LinearLayout>
Or in code it would be something like:
combinedLayout.contentDescription = "add content description here"
If any elements are clickable, be careful with setting as not important to accessibility, always test to make sure it behaves as you would expect.
Check out my post about common accessibility issues :) https://medium.com/microsoft-mobile-engineering/android-accessibility-resolving-common-talkback-issues-3c45076bcdf6
I'm a newbie to Android development, so apologies if the answers seem obvious to others...
I want to create a task tracker app for my org, where the set of tasks are usually about the same. Here's a mockup:
Project Progress Mockup
The mockup shows generic task names, but the real one will have different task names and more of them.
I need some help sorting out the layout design. Conceptually, this is pretty straightforward, but the implementation in Android turns out not to be so straightforward...
I thought I should create a "Task Item" class which contains 3 views: a text view for the task name, a progress bar & another text view for the percentage. Then I wanted to create an array of these Task Items.
Setting all this up in XML is not so bad, but I need it to be dynamic, as it will not always be the same list. Also, using XML is kinda brute force, as I would need to have unique code for each item in this activity, rather than looping through the list dynamically.
It seems like I need a ScrollView. I saw from another post the following statement:
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
So I understand that I need to add my list of Task Items to something like a LinearLayout, which is embedded in the ScrollView. This I can simply set up in XML, right?
Then, for each Task Item, I need to:
- create 2 TextViews
- set the text to the task name & percentage, respectively
- set the font, font size, text color, etc.
- set the TextView positions
- create a Progress Bar
- set the Progress Bar position & percentage
None of this is trivial. So I wonder if it would be easier to set up an XML layout for a single task item & then somehow load() the XML layout. Hopefully from there I could simply set the text & Progress Bar percentage. Would this work, or am I going about this wrong? Also, how would I set up the XML for this? Do I add an activity to my project, or just another XML file?
Finally, I need the task name to be clickable, so I can capture the click & open a new activity showing the subtasks for a task & allowing the user to change the status. Therefore the task name TextView needs to have an ID. Should I make the task names Buttons rather than TextViews? How can I set the ID dynamically? I see that there is a setId(int id) method for Views. But how do I know what integer to use? Android reference View.html states:
View IDs need not be unique throughout the tree, but it is good
practice to ensure that they are at least unique within the part of
the tree you are searching.
How do I ensure that these IDs are unique?
In the XML file, IDs are not numbers, but rather text, like:
android:id="#+id/my_button"
And this is referenced in the code like this:
Button myButton = findViewById(R.id.my_button);
But how do I create & use unique numeric IDs in order to capture click events on these task names?
Thanks for any pointers you can give!
New information & questions...
I have the basic functionality working, but a couple things aren't quite right:
If you look at my updated mockup, Updated Project Progress Mockup, I want the area highlighted in green to scroll, while the rest stays in place. However, the heading at the top & the button at the bottom scroll off the screen and won't come back. Not sure what I did wrong.
My layout xml looks like this:
<android.support.constraint.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/summary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBG"
tools:ignore="LabelFor"
tools:context="com.myorg.myapp.ProjSummary">
<TextView
android:id="#+id/txtSumm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/summ"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textColor="#color/colorPrimaryDark"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/txtProj"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="#+id/txtProj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/proj"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textColor="#color/colorPrimaryDark"
app:layout_constraintTop_toBottomOf="#+id/txtSumm"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/edtProj"/>
<EditText
android:id="#+id/edtProj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:paddingTop="5dp"
android:paddingBottom="0dp"
android:paddingStart="0dp"
android:paddingEnd="0dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="#+id/txtSumm"
app:layout_constraintLeft_toRightOf="#+id/txtProj"
app:layout_constraintRight_toRightOf="parent"/>
<ImageView
android:id="#+id/imgLogo"
android:layout_width="120dp"
android:layout_height="48dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/btnSend"
app:srcCompat="#mipmap/my_logo"
android:contentDescription="#string/org_logo"/>
<Button
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorSecondaryDark"
android:text="#string/send"
android:textColor="#color/colorButtonText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/imgLogo"
app:layout_constraintRight_toRightOf="parent"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvTaskList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/txtProj"
app:layout_constraintBottom_toTopOf="#+id/imgLogo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
My last task item never is displayed. (i.e. if I have 10 task items, only the first 9 are displayed.) In my activity class, I have an ArrayList for the summary list of task items:
private ArrayList _alSummaries;
And in my RecyclerView.Adapter class, I have the following method:
#Override
public int getItemCount() { return _alSummaries.size(); }
Anywhere else I should look?
Thanks!
Further update:
in my layout I had...
android:layout_height="wrap_content"
Rather than wrap_content, it should have been "0dp". Seems like a bug in Android, actually - if I say wrap_content, that should never cause the view to become larger than the space available to it (as set by the constraints).
Problem solved - not a problem with the interface, but with the code that loaded my data into the app.
So I wonder if it would be easier to set up an XML layout for a single task item & then somehow load() the XML layout. Hopefully from there I could simply set the text & Progress Bar percentage.
This is what a Recycler View adapter does. You have an xml layout containing the three simple views: a text, the bar and another text.
Now in your code (in the adapter), you inflate each row and set the text and progress bar individually.
Should I make the task names Buttons rather than TextViews? How can I set the ID dynamically?
You do not need to do that. You can just assign custom onClick listeners which do what you want in the adapter code.
For a beginner, the setup is definitely not trivial, but once you get the hang of it, you will find it to be a very valuable tool.
Look here for the official tutorial: https://developer.android.com/training/material/lists-cards.html
Im pretty new to Android Development, so after like three days of trying different things with the XML Layout Design, i give up and hope for help from you guys.
What i want to achieve:
A table layout with with multiple rows, each filled with calculations im making in the background
The first three rows shall contain the input parameters, the following ~12 rows shall contain output parameters
rows 3 to 6 shall be rearrangeable, so to speak change name and shown values.
This is the concept, thats what one row should look like:
My way of trying things was:
Creating a TableRow for "Taupunkt" and "Td" and another one for three textfields and the +/- picture.
But how on earth am i supposed to insert the ">" arrows picture into the layout? Basically it should be centered between the rows.
I hope i did a clear explanation of my problem and hope that there is someone out there who can help me :)
PS.: App is going to support Android 4.0 and above
EDIT: As seen in the picture, how would i go about centering the plus/minus vertically to the textfields? Like, it should have the same space above and below it to the textfields
You can use ListView or RecyclerView as mentioned in comments.
For second question to make your view centrally aligned you can use android:gravity attribute in LinearLayout. Just made one same which is using center_vertical. Checkout -
<LinearLayout
android:id="#+id/btn_google"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/standard_padding"
android:background="#drawable/rectangle_round_red"
android:gravity="center_vertical"
android:minHeight="#dimen/standard_height"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_google" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Google"
android:textColor="#android:color/white"
android:textSize="#dimen/font_18" />
</LinearLayout>
This is how it looks
I'd like to do textual back-and-forth interaction in an Android control. The idea is to have something like this:
This is some text output by the program.
What is your name? |
with the cursor at | (note that editing doesn't start at the beginning of the last line). The user is then free to enter text (using whatever Android input method, keyboard, etc.) but isn't allowed to change any of the output so far. Ideally, the user's input would be styled differently.
Then, as soon as newline is entered, I want the program to be notified and editing to stopped:
This is some text output by the program.
What is your name? Foo Bar
Hello, Foo Bar!
Note that this needs to be a proper control, i.e. one I can compose with other controls to make it just one part of the app's main layout.
Make a TextView and the EditText next to each other then your problem is solved and add the following line of code in EditText.
android:singleLine= 'true';
It allow only one line to be entered to the EditText. let me know whether this is what your expecting.
<?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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="What is your Name?"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView" />
</RelativeLayout>
This is some text output by the program.
What is your name? |
with the cursor at | (note that editing doesn't start at the beginning
of the last line). The user is then free to enter text (using whatever
Android input method, keyboard, etc.) but isn't allowed to change any
of the output so far. Ideally, the user's input would be styled
differently.
I would strongly recommend to rethink about your design as the same thing can be done with the help of LinearLayout,Editext,TextView with very simple and more manageable way.
I would suggest you to create a new LinearLayout(TextView + EditText) and assign the background of layout like EditText and edittext's no background.
Upon editText done, you could show a new TextView in the bottom
You need a ListView at top, to show your conversation & then below it, needs a horizontal view with a TextView (to show question) and EditText(with background transparent - to ask user to fill an answer).
I have something like a message system and listview that hold the messages.So I want to show let's say only the first 10 symbols of the message and after ellipsis.I couldn't find information how to do it, so if somebody can help me.
I assume you know how to set up an adapter.
You can achieve that by editing your XML layout file. You limit the max amount of text your TextView can hold, and tell it to add ellipsis if it's longer.
<TextView
android:id="#+id/secondLineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxLength="10"
android:ellipsize="end"/>
Example from here