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.
Related
<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.
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.
In my Android project I'm using a table layout contained in a scroll view. When a product is selected from a spinner the table view must automatically scroll to the table raw showing the relevant product's details. Unfortunately I can't post any of my code work. I have a big problem in scrolling the view. So can someone help me with some helpful code.
Thank you.
I think one solution is to know the index in your list of the selected product. Then you'll have to measure the childs in your ScrollView. When you know the sizes of your children and the index of the child you want to scroll to you can use ScrollView.scrollTo(x, y)
the y will be something like this: index * heightOfChild.
This is ofc if your childs are of the same size. Otherwise youll have to measure each child separatly.
Edit:
scrollTo apparently "clamps the scrolling to the bounds of our child." Not sure what it means... But you could try scrollBy(x, y) the documentation doesnt write anything about clamps for that.
if you are using a listView you could try setSelection() as described here:
Android scroll to the bottom of a listview
Simple aim:
To have default buttons in the navigation bar of my ActionBar with the text centre aligned. I simply want to use the default, simple way of:
ActionBar.Tab myTab = Actionbar.newTab()
myTab.setText("my tab's text");
What's the problem
I cannot find any simple way to do this whatsoever. You can't access the views (as ActionBar.Tab.getCustomView() always returns null due to it being the default view). There is no method that I can see in ActionBar, ActionBar.Tab, etc to get the current view, get or modify LayoutParams (especially Gravity). A visualisation of the problem is below (one line seems to work right, but double lined navigation buttoms definitely seem left aligned):
Unideal solution
Am I right in negatively thinking that the only way to accomplish this is to use custom views for all of my tabs, somehow guess / attempt to copy the default formatting for the tabs (as I can't find anyway to access these views) and assume / hope that the default formatting / font / style of ActionBar.Tab's text does not change any time soon?
Surely there has to be a better way?
AFAIK, there's no way you can access the Views in ActionBar. Using custom layout seems the only solution. However, doesn't Android align the text to center automatically? It apparently does so in all my applications.
please download the follwing example. it will help you. i am sure i take help from this demo.
https://github.com/JakeWharton/ActionBarSherlock
I find myself a little baffled here. I'm fairly new to android/java development so please help out here to identify what I'm doing wrong. I have checked aggressively throughout google, android dev and stack but couldn't find much info on this.
I have a string variable with a number as theRating. e.g. 2.5 (out of 10)
I am trying to display this using ratingbarstylesmall... It's for showing only and not rating.
RatingBar rb = new RatingBar(this, null, android.R.attr.ratingBarStyleSmall);
rb.setIsIndicator(true);
rb.setNumStars(5);
rb.setStepSize((float) 0.5);
rb.setMax(10);
rb.setRating(Integer.parseInt(theRating));
llTextEtc.addView(rb);
The stars load fine inside LienarLayout (llTextEtc), show up in the right place, it's the right style stars that i want (small) but...
it's completely random. Some show 8 stars, some show 15 then back to 7 and so on. Totally random. What am I doing wrong?
Thanks guys
Update:
With help from the accepted answer:
A LinearLayout was added to hold the ratingbar with wrap_content layout. Otherwise the parent (scrollview) became wrap_content.
LinearLayout llRating = new LinearLayout(this);
RatingBar rb = new RatingBar(this, null, android.R.attr.ratingBarStyleSmall);
rb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rb.setIsIndicator(true);
rb.setNumStars(5);
rb.setRating(Float.parseFloat(theRating)/2);
llRating.addView(rb);
llTextEtc.addView(llRating);
setStepSize and setMax was also removed, Rating was set to float instead of integer. The stars are calculated and then set. stepSize and Max doesn't help when isIndicator is true.
I noticed a couple of problems here. First and most important you need to set the layout width to WRAP_CONTENT for it to work properly. The following is from the dev guide:
The number of stars set (via setNumStars(int) or in an XML layout) will be shown when the layout width is set to wrap content (if another layout width is set, the results may be unpredictable).
Secondly I noticed that you have set the stepSize to a float and you are seting the rating using an Interger. I would first convert the value from the URL feed to a Float and then validate it before passing it to the rating bar.