Grid of fixed number of rows and columns - android studio - java

I want to create a grid or table of fixed number of rows and columns(Ex. 6x6 grid) in a layout of Android Studio. I tried using GridLayout and GridView but it requires you to put 36 EditText(Plain text) Views in it for creating a 6x6 grid. Same is the case with TableLayout where you can only insert TableRows but cannot fix the number of columns.
Bottom line is, I want a 6x6 grid which has only a 6-letter word(one character in each cell) in the beginning and one letter is typed in every turn to make a word with the existing letters.
For this, from any cell, I should be able to read the characters in the adjacent cells. I don't think creating a GridView or TableLayout and creating 36 objects of EditTextView will be the best idea. Is there a good and efficient way to do this? I need the .xml file code and also its Java class file's code.

Why you don't think that creating a layout holding views in Android framework is good and efficient way for exposing some data in a graphical interface? This is the only reason for which View class even exists. It's a main building material of your application's GUI.
Also you don't need to create it by hand. You just need to be able to address your ViewGroup (Layout) object from your Java code where you build your Activity instance. From there you have an addView() method and you can add views in a simple loop (notice that this way you can create grids of every size, not only 6x6).
Please familiarize yourself with official Android Developers site where you can read pretty much about anything relating Android Framework. For more info about layouts click here.

Try this library it might help you.
https://github.com/InQBarna/TableFixHeaders

Related

Possible efficient ways of creating a timetable in Android (Java)

I've been trying to create a timetable and I have tried different layouts, the best i could get is to use a grid layout.
a preview of how it looks:
The header is a fixed view, I added that manually in the xml file.
but each of the "Off white" cells and cells that contains time are textviews added dynamically using a loop.
However I think it's not the most efficient way of creating this, also I wouldn't be able to locate those cells unless I add an id to each of them dynamically and store those Ids.
This example shows an empty time table. but those cells should be able to expand and merge with others when an event is added.
Is Grid Layout the only way to build this and is there more a more efficient way to locate each cell position?
this is similar to what I'm trying to achieve

Android Studio: Group elements together

I am fairly new to android studio and I am trying to make a car rental app for a university project.
I designed an app which has a horizontal slider containing blocks of different cars however I am unsure on how I would do this, I understand how the slider works and how to add elements to it.
I have a linear layout within the horizontal slider which will contain all of the blocks but I am unsure how to make a block of elements. Within the block it should have two buttons, an image and some text. Here is a picture of the design.
As you can see the available blocks are surrounded by a square. this is what I want, a small container which surrounds my data. Sort of like a div box in html.
Is there a way to do this?
Please just point me in the correct direction!
Many thanks!
It seems like cardviews can handle most of the grouping you want to do. I would reccomend looking into them because they incorporate a lot of material design components right out of the box.
https://developer.android.com/guide/topics/ui/layout/cardview

How to check if a View reach the end of ViewGroup

I have been working on an app that adds views programmatically to a linear layout.
The problem is if I add too many views it will go off the screen.
I would like to know how to check if a certain child has hit the end of the same view group so I could add it into another layout (a linear layout below the first one) before it "flows" and go off the screen. How might I accomplish this?
Rather than reinvent the wheel yourself, I suggest that you check out the FlexboxLayout project by Google: https://github.com/google/flexbox-layout
A FlexboxLayout will automatically give you the behavior you're describing, plus the potential for much more.
Well, there are a good number of ways you could implement this in android other than going through this hustle. What ever you are trying to do at the moment may fall under one of the following cases.
Creating views programmatically most likely means you have a dynamic data set probably from an external source.
Your dataset is limited or static but just more than the average screen can display.
if any of the above apple then you are better off using a ListView or RecyclerView (Recommended). That way your data is full displayed as a scroll-able list and you don't have to worry about some items or views not showing or going of the screen. This can range from simple string list to complex nested views.
This will be very efficient as it will automatically handle optimization and usage of memory as well as performance.

Changing Gridview number of columns based on position

Im currently building an application that implements a GridView. However, i need to split the GridView up. For example, at position zeta the number of columns is 3 then at position 2 3 4 the number of columns change to 1.
I have had a look around but it doesn't look like its possible. Coming from an iOS Background ive done it before in some previous iOS projects so i thought i may be able to replicate it in android.
Any suggestions would be great.
Just a idea not sure if it would work. Making the gridView an auto_fit column,
then based on position make the item cell view width properly so the gridView could either put in multiple cells or just one cell in the row.

Android - How do you efficiently load a large amount of text in a TextView?

I'm writing an Android app that reads a single text file and display it on a TextView.
What I'm doing right now is read the whole file into a String (using BufferedReader and StringBuilder) and display it on a TextView using setText(string). A 700KB text file can take about 2 to 3 seconds before it is being displayed on the screen.
But I've used some other ebook readers on the market and they can display the same text almost instantly. Anyone know how I can achieve this?
Thanks you.
Edit: Many suggest ListView, but it doesn't work for my particular case. This is from my reply to one of the answer: ...[ListView] doesn't work for me for a few reasons. (1) To make the Listview look like a TextView, we have to break the text up on new line character. If I load a single large paragraph, it's just as slow as a loading a TextView. (2) Since a ListView only measures the item on the screen, I cannot know ahead of time the total 'pages' or 'height' of the entire text.
Essentially, the key is to only load the data that you need, when you need it. One way to do this would be to put every paragraph into it's own TextView, which is put into a ListAdapter, which is included into a ListView. There has to be some kind of an index set in place, such that each paragraph knows where in the data file to find. This interface will allow you to load only what you need, when you need it. Your list adapter looks something like this (This code isn't complete, but it should give you an idea at least of what you should do):
class ParagraphAdapter extends ListAdapter{
ArrayList<Integer> mLocations; // Somewhere define this to your locations, I'll leave that for you to figure out
protected View getView(int position,View convertView, ViewGroup parent)
{
mLocations.get(position); // Read the file starting at this position, until the next value
String text; // This is the output of the above
TextView tv=new TextView(context);
tv.setText(parent.getContext());
}
}
It can be noted that Amazon uses a system of paging for the Kindle App. If you have the app, you can see at the bottom of each page what section you are on. Each "page" is probably closer to a sentence or so in length. Then it's just a matter of getting the right page, which can fairly quickly be done.
To add on what #PearsonArtPhoto has said -
I suggest you implement some sort of paging mechanism, to divide your text into pages.
What you should do is split your text according to let's say N +M number of characters per pages.
N = fixed number of characters.
M = number of characters from N to nearest end of line character (so you won't see the last
line being "cut").
I would suggest that if your android device allows you to hold this "in memory" - do that,
and don't try to fetch this from the file one page after the other, but rather fetch from the "in memory" structure - this will improve performance.
Once you scroll and realize you need to fetch the next page, fetch it from the "in memory" structure.
You can use Android Paging Library from Jetpack with custom View or RecyclerView
https://developer.android.com/topic/libraries/architecture/paging
Implement DataSource abstraction that provides List of data ranges
Implement Storage abstraction that provides real data (from Database, Network, Files, etc)
Use RecyclerView and Adapter to display data
Sample app to display content of large files can be found here
https://github.com/YablokovDmitry/FileView
Lucas Rocha built a nice library called Smoothie for that purpose.
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
At the end of those performance tips for Android listview there's a link to an explanation about Smoothie and finally you'll find the library available on github.
Originally described for loading images, the approach applies for loading text as well.
You must to consider if you are using right components.
Maybe it is mutch better to read lines and put them to listview.
try
List<String> lines;
listview.adapter(new ArrayAdapter<String>(...,lines));
And many times that what you see (Look and Feel) is not like you are think.

Categories

Resources