ScrollView in android - java

I have a TableLayout, in which rows are dynamically created, thus it may cross the limited screen size. To avoid this I am trying to add a scroll view after creating the table layout as:
TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
ScrollView sv = new ScrollView(this);
tl.addView(sv);
for (i = 0; i < count; i++) {
TableRow tr = new TableRow(this);
.
.
.
. //finally iam adding this table row to layout.
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
submit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(),
SmsActivity.class);
// setMessageBody((String) tvf.getContentDescription());
// submit.setEnabled(false);
String messageText = (String) valueTV.getText();
String dateText = (String) labelTV.getText();
myIntent.putExtra("messageText",dateText+"\n"+message);
startActivity(myIntent);
}});
}
However the application is getting force closed if the number of rows increases more than the size of display.
My log cat shows the error as
06-28 14:41:49.533: ERROR/AndroidRuntime(1256): Caused by: java.lang.StringIndexOutOfBoundsException
What is the problem and how can I fix it?

You need to add the TableLayout to the ScrollView, and not the other way around.

ScrollView can only have one child and will throw an IllegalStateException if you try to add more than one. Typical practice is a ScrollView with another view inside it containing the views over which you wish to scroll.
Having said that, you appear to be getting a different sort of IllegalStateException so I'm not 100% sure that this is the issue.
In my view, though, the real solution is to not dynamically populate a ScrollView but instead use a ListView, which is explicitly intended for just this sort of thing. It's a little more work but very rewarding in terms of performance and scalability.

Related

Android Studio TextViews onClick all perform the same action, how do I fix it?

I have this function which is supposed to create an array of TextViews with unique ids.
Each TextView is supposed to perform a unique action, however when any one of them is clicked, they perform the function of the last TextView .
(ie, anyone of them appends a 9 to the last TextView the way this i set up) Do you know why it does this, and how I can fix it?
Any help would be greatly appreciated.
//Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_what_can_imake);
int textViewCount = 10;
TextView[] textViewArray = new TextView[textViewCount];
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myLayout);
for(int i = 0; i < textViewCount; i++) {
textViewArray[i] = new TextView(this);
textViewArray[i].setText("Title"+Integer.toString(i));
textViewArray[i].setPadding(8,8+50*i,8,0);
textViewArray[i].setId(i);
LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
textViewArray[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int myId = v.getId();
((TextView) v).append(Integer.toString(myId));
}
});
myLayout.addView(textViewArray[i],myTitleDimensions);
}
}
You are using different paddingTop to layout your TextViews vertically:
textViewArray[i].setPadding(8,8+50*i,8,0);
this makes the TextViews visually separate to each other, but in fact they are all overlapped, the 2nd one overlapped the 1st, the 3rd one overlapped the 2nd, etc. At last, the 9th one overlapped all, so no matter which text you clicked, you actually clicked the 9th one.
To fix this, you should change the way you layout the TextViews.
For example, use RelativeLayout.addRule(int verb, int anchor):
RelativeLayout.LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
myTitleDimensions.addRule(RelativeLayout.BELOW, i - 1);
}
By the way, 0 is not a valid id, so your 1st TextView will be still overlapped by the 2nd one, just change the way to generate ids a little.

Change Layout position from code

I am creating an app where I will be loading some images and data from a database, it should look like this:
Image______Name of user
Image______Name of user
Image______Name of user
etc..
I tried to create it just with a dummy image and some text to figure out how it works.
I create a LinearLayout, ImageView and a TextView, I add those two to the LinearLayout, and than I add that LinearLayout to a RelativeLayout.
The problem is, that all the images and text are placed in the same place, on top of each other. How can I change it so it is in the format I need?
relativeLayout = (RelativeLayout) findViewById(R.id.rel);
for(int i = 0; i< 30; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView hello = new TextView(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
String hi = "Hey";
if(i == 0){hi = "Hello0";}
if(i == 2){hi = "Hello2";}
if(i == 3){hi = "Hello3";}
if(i == 4){hi = "Hello4";}
hello.setText(hi);
layout.addView(imageView);
layout.addView(hello);
relativeLayout.addView(layout);
}
I am using a for to loop it a few times just for test.
Instead of RecyclerView, add the items in a LinearLayout. You can also set position where to add the new item in the LinearLayout.
I would suggest you do instead is:
create a model object for the user details(name and picture)
Use ListView or RecyclerView with a simple adapter add items to an
ArrayList of model object and notify the adapter when data is
changed.
This way you'll be reusing the views, and that'll improve the performance much better.
for examples, you can take a look at these sample projects.
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/ListView
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/RecyclerView

Android Text View at run time - Text views inflating as multiple row

How to create multiple text views at run time in multiple rows and column? I have inflated a linear layout and created the text views using for loop. Text Views were created successfully, but i'm facing the issue that all created text views are only in single row. I tried to set it with the LayoutParams also, but cant fixed it. How to fix this? Below is my code
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
appointmentSlotList = appointmentSlot.getAppointmentSlots();
if(appointmentSlotList != null && appointmentSlotList.size()>0){
for(int i = 0; i<appointmentSlotList.size(); i++){
View appointmentInflater = layoutInflater.inflate(R.layout.appointment_time, null);
TextView lblDate = (TextView) appointmentInflater.findViewById(R.id.appointmentTime);
lblDate.setText(appointmentSlotList.get(i));
lblDate.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//lblDate.setLayoutParams(lparams);
lblDate.setOnClickListener(onclickTime);
try {
//if(previousSelected!=i)
lnrDateContainer.addView(appointmentInflater);
}catch (Exception e){
e.printStackTrace();
}
}
}
and my output is shown below:
Output for the inflated text views shown below the date picker
Set the textview android layout gravity to fill
android:layout_gravity="fill"
If above wont work then refer to this question : Android multi line linearlayout
As you want to add TextView in rows and columns, you can use TableLayout and add TextView in it pragmatically
Check this answer : https://stackoverflow.com/a/16939325/5345482
To achieve these I think FlowLayout library might be fit to your requirements.
With this lib you can create your TextViews in a single line and when doesn't have space then auto inserts the view in the next line.
The link: https://github.com/ApmeM/android-flowlayout
I hope helps you!
Specify the orientation in your Linear Layout as VERTICAL
<LinearLayout
----
android:orientation="vertical"
---- />

Issue with Button in AlertDialog (Floating off screen)

Here's an image of what my issue looks like:
That box at the bottom is the top half of a button. Whenever I've got too many Stock Options in the dialog box, it first forces the button off screen, THEN it scrolls.
I want the button fixed at the bottom of the dialog and then have the scrolling happen.
Here's the code:
public void buyStock(View view){
Context context = getApplicationContext();
//create ScrollView to hold everything
ScrollView scrollView = new ScrollView(context);
//generate content for dialog
LinearLayout dialogContainer = new LinearLayout(context);
dialogContainer.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 400, 1);
dialogContainer.setLayoutParams(params);
dialogContainer.setPadding(15, 15, 0, 15);
dialogContainer.setBackgroundColor(Color.WHITE);
//each hotel stock options
for (int i = 0; i < hotels.size(); i++) {
Hotel testHotel = hotels.get(i);
testHotel.setPrice(200);
View stockPicker = getStockPicker(testHotel);
LinearLayout.LayoutParams pickerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 100, 1.0f);
pickerParams.gravity = Gravity.LEFT;
stockPicker.setLayoutParams(pickerParams);
dialogContainer.addView(stockPicker);
stockPicker.setBackgroundColor(0xffffff);
}
scrollView.addView(dialogContainer);
LinearLayout dialogLayout = new LinearLayout(context);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
Button buyButton = new Button(context);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
buyButton.setLayoutParams(buttonParams);
LinearLayout buttonLayout = new LinearLayout(context);
buttonLayout.addView(buyButton);
dialogLayout.addView(scrollView);
dialogLayout.addView(buttonLayout);
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(ctw);
buyStockDialog.setTitle("Buy Stock: ");
buyStockDialog.setView(dialogLayout);
buyStockDialog.show();
}
Make dialogLayout a RelativeLayout, and then declare buttonParams as a RelativeLayout.LayoutParams (layout params are declared according the parent viewgroup type, which for buyButton is now a RelativeLayout). Your also going to need to declare a RelativeLayout.LayoutParams for scrollView.
note: This code is in c#/monodroid, not raw java/android, so some quick porting of some of the methods and constants may need to be required, but shouldn't be too time consuming.
If this doesn't work out exactly trying experimenting with other combinations of the RelativeLayout position rules and/or gravity. Let me know if it doesn't work out.
RelativeLayout.LayoutParams scrollParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, some_height);
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100);
Then, add these statements to control where scrollView and buyButton are positioned
scrollParams.AddRule(LayoutRules.AlignParentLeft);
scrollParams.AddRule(LayoutRules.AlignParentTop);
buttonParams.AddRule(LayoutRules.Below, scrollView.Id);
The for the getting the button on the bottom of the screen, try one of two things
buttonParams.AddRule(LayoutRules.AlignParentBottom);
or
buyButton.Gravity = Gravity.BOTTOM
Try explicitly setting a fixed height to the scrollView
scrollView.LayoutParameters.Height = HEIGHT;
I think it defaults to WRAP_CONTENT if not specified, and is why its scaling.
You may have to set it in a layout file if you cant from code
<ScrollView
android:id="#+id/scroll_view_1
android:layout_width="wrap_content"
android:layout_height="100"/>
and then change
ScrollView scrollView = new ScrollView(context);
to
ScrollView scrollView = findViewById(R.Id.scroll_view_1, this);
Try wrapping the scrollView in another LinearLayout, and set this new layout's height to a fixed width that keeps the button low enough to your liking. You can actually replace the Scrollview with a ListView, but don't do this until after at least trying to get this fix working first. ListViews scroll, but you'd still face this problem without this fix.
To fix the new issue you mentioned below (with some quick and dirty code, you should use trial and error to fix it correctly), try the following
//each hotel stock options
for (int i = 0; i < hotels.size(); i++)
{
// ...
}
if( hotels.size() < numberOfItemsNeededForCorrectView )
{
for( int i=0; i < numberOfItemsNeededForCorrectView - hotels.size(); i++ )
{
View blankView = new View(context);
LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(1, 100);
blankView.setLayoutParams(viewParams);
blankView.setViewState(ViewState.INVISIBLE);
dialogContainer.addView(stockPicker);
}
}
You can try replacing your scrollView with a list view still and see if that fixes things. You can also try tweaking layout and gravity settings until you get it, or trying introducing other layouts or reorganizing your layouts (or using RelativeLayout, its really not that bad).
So, now in 2022 I've also had this problem. I've just set fixed size for ScrollView in fragment layout xml-file. When you have this problem, you can set, for example, 150dp or 200dp and be happy! And for more convenience, you can create special layout for landscape screen orientation and set fixed layout_height size there. I'm really confused that there's no information about this problem besides this page on the Internet.
<ScrollView
android:layout_width="match_parent"
android:layout_height="200dp">
<!--your view into scrollView here-->
</ScrollView>

Setting up RelativeLayout in java code

I'm having a hard time getting two text views to appear on top of each other in my java code. Here's the code I'm experimenting with:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
text1 = new TextView(this);
text1.setText("1");
text2 = new TextView(this);
text2.setText("2");
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
q.addRule(RelativeLayout.BELOW, layout.getId());
text1.setLayoutParams(q);
layout.addView(text1);
p.addRule(RelativeLayout.BELOW,text1.getId());
text2.setLayoutParams(p);
layout.addView(text2);
setContentView(layout);
}
This stacks the two text views on the same line, but I want TextView text2, to appear below TextView text1, so in my app I want the following to appear as the output:
1
2
I've tried all sort of things with the "addRule" method, I'm not sure why this isn't working. I want to know how to do this without XML because I plan to build a library of methods that can build up a layout that is easily adjustable through editing an array.
Your TextViews don't have an id (by default the id is -1)... put this after their initialization:
text1.setId(1111); // 1111 is just an example,
text2.setId(2222); // just make sure the id are unique
I don't think you are looking to layout the text1 view below the RelativeLayout since you added all your views to it as children, right? Try removing the first rule; that rule is asking the text view to be below the same view it is in.
EDIT: Also a help is explicitly setting the id of the view you are laying out relative to.
So here:
text1.setId(2);
p.addRule(RelativeLayout.BELOW,2);
you can use xml layout for this :
in relative layout
u set the first textview and assign it some id
fot the next text view we can assign parameter
android:layout_below="id of above text view"
in this way we get 2nd text view below 1st text view

Categories

Resources