How to set column and row number programmatically in gridLayout? - java

<GridLayout
<ImageView
android:id="#+id/freeParking"
android:layout_width="52dp"
android:layout_height="58dp"
android:background="#57979F"
app:layout_column="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_row="0"
app:srcCompat="#drawable/ic_launcher_foreground" />
This is one of the child in my gridLayout. I want to change its layout_column and layout_row from MainActivity.

I am answering the question considering that you are working with the latest version of Android Studio.
Programmatically you can define the define the number of rows and columns as:
android:rowCount="number of rows"
android:columnCount="number of columns"
There are a few points you must always keep in mind:
1.First of all you can't directly modify the layout from Main Activity.
2.You can change them by accessing the plentiful options available in the Pallete.
3.Instead for layout_column and layout_row you can access the Linear Layout(horizontal and vertical) respectively.
4.Guideline Layout is also a secondary option available)
5.If you don't wanna use them, then you can manually modify them using the various constraints present in the attributes section (hoping you are familiar with them).
What you are presenting here is just a part of the basic code present in the activity_main.xml portion of the project file. This portion of code has no area that can modify your desired layouts.
In short if you want to define the number of rows and columns in the project file, then there are two attributes present in gridLayout. These are columncount and rowcount present in the attributes. One can use these to define the numbers of columns and rows respectively.

Related

How can I use Android DataBinding to dynamically change android:layout property?

I have the XML layout code:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="info" type="com.mycompany.orm.binders.MyBinderObject"/>
</data>
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:gravity="end">
...
And this works great. However, I have a property inside MyBinderObject called mygravity, and it is set to one of two strings: start or end. I am trying to figure out how to read the Data Binding property mygravity to assign the gravity to the layout.
I.e., android:gravity="#{info.mygravity}" (However, this line does not work. The compile-time databinding code fails).
Does anyone have an idea how to dynamically set the android:gravity based on a Data-bound objects property?
I have a property inside MyBinderObject called mygravity, and it is set to one of two strings: start or end.
Therein lies the problem: you're doing something reasonable and treating the layout XML like XML.
The layout XML attribute values can be of a variety of types. Sometimes, this is obvious, such as android:enabled="false" or android:layout_width="30sp". And, sometimes, the type really is a string or string resource reference (e.g., android:text="Foo"). But sometimes the attribute value looks like an English-language string, but it is really one of a set of enumerated values (e.g., gravity values).
I didn't realize that gravity was actually an enumerated attribute
A rough-cut rule of thumb: if the build would fail if you translated the value into another language, then it is an enumerated attribute (e.g., android:gravity="fin" instead of android:gravity="end", assuming that Google Translate gave me reasonable Spanish there...). If the translated value would work just fine, then it's any valid string or (usually) string resource.
I am trying to figure out how to read the Data Binding property mygravity to assign the gravity to the layout.
You will have to transmogrify the string values into equivalent Gravity constants like Gravity.START.
you can definitely use android:gravity="#{info.mygravity}" in layout file but make sure that mygravity should be int value.
you should refer this
like if you want center gravity, value of info.mygravity should be Gravity.CENTER

Modify TableLayout columns size in android with java

I have designed a table for android app which needs to be dynamic and should populate data from mysql database. The design has been implemented from java code.
However, Im facing a problem with a column in that table which does not appear in the screen but still displays the data which is not visible in the screen.
I want to know how can I fix this problem and avoid table to appear off the screen.
Since i can't upload the image..the image of how the table is...is here
On your TableLayout issue Android APIs section says:
The width of a column is defined by the row with the widest cell in that column. However, a TableLayout can specify certain columns as shrinkable or stretchable by calling setColumnShrinkable() or setColumnStretchable(). If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. If marked as stretchable, it can expand in width to fit any extra space. The total width of the table is defined by its parent container. It is important to remember that a column can be both shrinkable and stretchable. In such a situation, the column will change its size to always use up the available space, but never more. Finally, you can hide a column by calling setColumnCollapsed().
That is why for your purpose use setColumnShrinkable() for a column which content comes out of the screen.
For additional details check, please, documetations the TableLayout tutorial below: Android User Interface Design: Table Layouts

Unable to Set Text Size Using FlowTextView

I found a library which allows an app to wrap text around an image - however once implemented it changed the size of my text - how can the text size be increase when using this library?
android:textSize= has no impact on the text size.
Neither does:
FlowTextView titleTv = (FlowTextView) findViewById(R.id.titleTv);
((FlowTextView) findViewById(R.id.titleTv)).setTextSize(20);
https://code.google.com/p/android-flowtextview/
Example:
<com.pagesuite.flowtext.FlowTextView
android:id="#+id/titleTv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text=""
android:textSize="20sp" >
In the short term a call to invalidate will probably get it working:
FlowTextView titleTv = (FlowTextView) findViewById(R.id.titleTv);
titleTv.setTextSize(20);
titleTv.invalidate();
However, I suspect you are using the JAR file right? It is quite out of date so I would recommend checking the source code out and using it as an android library project - setTextSize() should work properly then without needing a call to invalidate() (plus various other bug fixes etc).
Also - I never added the ability to set the text size via XML - wouldn't be too hard to add this though.
I checked the code of android-flowtextview and they have the text size hardcoded (check line 131 here). You have to change their source code or use the public method setTextSize(int).
Also, this link might help you, as seems that someone already did something as you are trying to do.
https://code.google.com/p/android-flowtextview/source/browse/trunk/src/com/pagesuite/flowtext/FlowTextView.java
There's a 'setTextSize(int)' method that should do exactly what you're looking for.
If you want to set it from XML, it's a little more involved. Since FlowTextView's constructors ignore the AttributeSet that gets passed in, you'll have to code this yourself. Follow guides like this: http://kotikan.com/blog/posts/2012/09/android-attributes to figure out how to add custom attributes to your views.

Changing GridLayout row/column count at runtime in android

I am new in android development. I am trying to use a GridLayout to fulfill my UI design.
This is the scenario:
I defined a GridLayout in xml file as following
<GridLayout
android:id="#+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/linearLayoutBottomLeft"
android:layout_alignParentLeft="true"
android:layout_below="#id/linearLayoutTopLeft"
android:layout_toLeftOf="#id/textView" >
</GridLayout>
Then, I set it in my activity's onCreate() method like following:
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
gridLayout.invalidate();
int rowNumber = 4;
int colNumber = 4;
gridLayout.setRowCount(rowNumber);
gridLayout.setColumnCount(colNumber);
so far so good, everything is working well.
However, I also have some buttons there. In button's click event, I changed the rowNumber and colNumber, set them at runtime. It cause some error I think...
So my question is whether the row number and column number can be set at runtime for gridLayout.
If this is not allowed in android, what is a good practice to realize a GUI like what I described above.
Any help would be appreciated.
You'll need to do a gridLayout.removeAllChildren().
Invalidate is a way of letting the View system know that the content of the View has changed and that it needs to be redrawn.
Today, I figured out some way to solve this problem.
In the xml file, I replaced the GridLayout with a LinearLayout, and made the girdlayout dynamically added as the child of linearlayout. Now the row/column count can be changed in button click event handler.
I am not sure if this is a good way to do it and any performance hit will arise.

Create a properties frame in Java

I am trying to make a properties frame just like the one in netBeans (or Visual Studio). My problem is that I don't know exactly how to design it. First I thought I'll make it with JTable (2 columns, multiple rows) but then I realised that on the second column I will have different types of values (booleans, String, color choosers, etc.), but I think that JTable allows only 1 type of data to be placed in a column.
I would like someone to tell me "JTable allows multiple data types on the same column" and show me how to do it, or tell me a different approach to the problem.
You can perfectly tell a JTable to have a column that contains Object, this way you will be able to put whatever ou want in.
BUT.
You'll then have to implement a very good TableCellRenderer/TableCellEditor pair in order to display whatever the cell contains.
Another option would be to use a Grid or GridBag layout inside of a JScrollPane, then dynamically populate the cells of the grid with different editors depending on the data type of the property.
If you can use external libraries, the JGoodies FormLayout is really suited to create such dialogs. Just take a look at the screenshots in their demo.
There is also a rather good PDF available containing with some examples and explanations.

Categories