Dynamically adding buttons to scroll view - java

I would like some help with dynamically adding buttons to a ScrollView. I've got it working with a LinearLayout but obviously I can only add so many buttons before they no longer appear on screen. My code is below with an attached image of its current state.
I tried changing every occurrence of LinearLayout with ScrollView in the code but when I ran it, I got an error that stated something along the lines of ScrollViews can only have 1 direct child.
I'm not sure how to make it work, so if someone could give me some guidance on how to do it, I would be very grateful.
My XML Code:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#id/imageTextView"
android:layout_centerHorizontal="true">
</LinearLayout>
</RelativeLayout>
My Java Code (where I'm dynamically creating the buttons):
public class Main5Activity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
for (int i = 0; i < 6; i++)
{
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++)
{
final Button btnTag = new Button(this);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
btnTag.setText("" + (j + 1 + (i * 4)));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
}
}
Image of the current layout.

Yes ScrollView can only have 1 child, and often it is the ViewGroup like LinearLayout, RelativeLayout, etc.
You need to wrap your LinearLayout with a ScrollView, like this:
<ScrollView>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#id/imageTextView"
android:layout_centerHorizontal="true"/>
</ScrollView>
Or you can change your topmost RelativeLayout into ScrollView if it has only one child.

Related

How to get programmatically the children of same type from linearlayout?

I have a linearlayout with many progressbars (horizontal progressbars).
I can add programmatically many progressbar to my linearlayout as I want.
But I would like to get a progressbar in a specific index.
for example: If I add 4 progressBar to my linearlayout.
And I want to get the second progressBar and set a progress and max progress just to the second progressbar in my linearlayout.
I tried to loop through all the children of linearlayout and get the child in a specific index what it doesn't work.
// add progressbar to linearlayout
linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
linearLayout.removeAllViews();
int newWidth = linearLayout.getWidth() / 7;
int newHeight = linearLayout.getHeight();
while (i < 7) {
i++;
LinearLayout child = (LinearLayout) getLayoutInflater().
inflate(R.layout.stories_progress,
linearLayout,
false);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
lp.width = newWidth;
lp.height = newHeight;
linearLayout.addView(child);
}
}
});
// get progressbar children of linearlayout at a specific index
int count = linearLayout.getChildCount();
View v = null;
for (int i=0; i<count; i++) {
// i want the progressbar in index 1 (the second)
v = linearLayout.getChildAt(1);
// this code produces a blank page in my app
ProgressBar pg = (ProgressBar) v;
pg.setProgress(20);
pg.setMax(100);
}
I'm triyng to achieve the same result as Instagram Stories, where when an user posted 4 images/videos, we see above the 4 progressbars and seeing the first video/image (at index 0) the progress settings is activated to start counting.
My stories progress:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ProgressBar
android:id="#+id/stories_progressBar_id"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ProgressBar>
</LinearLayout>
My Activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear_layout_id"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
tools:context=".stories">
</LinearLayout>

Android making an scrolled two independent listview in activity

I am working in Android Studio in a new app and I want to create a layout with two independent scrolled listview (each listview scrolling in independent way) in the same activity. I cannot obtain the target that I want. Could you give me some advice?
I am not sure about efficient way but I worked in following way.
First, I have created two dynamic ListView and put in into one activity.
It will calculate the height dynamically rather than match_parent.
Here is the code:
public void setListViewDynamicHeight(ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
int height = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
layoutParams.height = height + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(layoutParams);
listView.requestLayout();
}
You can build it in your layout xml like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll_view_1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hallo!" />
<!--Other views inside NestedScrollView-->
</android.support.v4.widget.NestedScrollView>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll_view_2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hallo!" />
<!--Other views inside NestedScrollView-->
</android.support.v4.widget.NestedScrollView>
</LinearLayout>

Inflate TextViews to LinearLayout

I want to inflate TextViews to LinearLayout.
I have a layout activity_play_game and java class PlayGameActivity.
Currently, for hardcoded number of TextViews it looks like this:
Now I tried to add number of TextViews according to the value of variable numberForbiddenWords. I added a piece of code that looks like this:
LinearLayout playGame = (LinearLayout) findViewById(R.id.activity_play_game);
TextView temp;
TextView[] textViews = new TextView[numberForbiddenWords];
for(int i = 0; i < numberForbiddenWords; i++) {
temp = new TextView(this);
playGame.addView(temp);
textViews[i] = temp;
}
And I have a method that adds textViews to the activity_play_game. Unfortunately now it looks like this:
As you can see it added TextViews at the bottom. So I removed all TextViews from activity_play_game and added LinearLayout to inflate TextViews:
How can I inflate TextViews to this LinearLayout above the buttons?
I found that kind of solution:
activity_to_add_words:
<?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"
android:orientation="vertical">
<TextView
android:id="#+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
In the java class:
final View addView;
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView = layoutInflater.inflate(R.layout.activity_to_add_words,
null);
final TextView textOut = (TextView) addView.findViewById(R.id.textout);
but this is for a View, not TextView. Do you know how to do it?
Get reference of your LinearLayout container_blue and add TextView's to it.
Try this:
LinearLayout playGame = (LinearLayout) findViewById(R.id.container_blue);
TextView temp;
TextView[] textViews = new TextView[numberForbiddenWords];
for(int i = 0; i < numberForbiddenWords; i++) {
temp = new TextView(this);
temp.setText("TextView " + String.valueOf(i));
temp.setId(i);
temp.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
playGame.addView(temp);
textViews[i] = temp;
}
make one layout file using textview like this :
layout_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/txt_spin"
android:textSize="18sp"
android:background="#fff"
android:padding="5dp"
android:textColor="#000"
android:layout_height="wrap_content"/>
then apply following code in your java file:
LinearLayout playGame = (LinearLayout) findViewById(R.id.container_blue);
LayoutInflater li =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.layout_textview, null);
TextView txt= (TextView) view.findViewById(R.id.txt_spin);
playGame.addView(view);
Hope it works for you...happy coding..:)
As far as I see your initial problem was about adding the items dynamically before the three buttons. If you want to add them before the three buttons simply call playGame.addView(temp,0); This will always add the item to the first position in the layout.
If you need a specific order of the added views you need to play with the for loop to add them in the right order.

Buttons in linearlayout appearing vertically instead of horizontally?

I'm very new to Android and I'm trying to dynamically add buttons in my android app, the problem is that they appear vertically, while this should be horizontally.
What I'm getting:
What I'm expecting (and want):
Code I'm using:
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.main_linearlayout);
for(int x = 1; x <= 5 ; x++)
{
LinearLayout tmpLinearLayout = new LinearLayout(this);
tmpLinearLayout.setOrientation(LinearLayout.VERTICAL);
tmpLinearLayout.setLayoutParams( new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f));
tmpLinearLayout.getLayoutParams().height = 200;
ll.addView(tmpLinearLayout);
for(int i = 0;i<5;i++)
{
Button tmpButton = new Button(this);
tmpButton.setText("nr:" + i +" - " + x);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
tmpLinearLayout.addView(tmpButton, lp);
}
}
}
Layout (activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/main_linearlayout"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#drawable/background"
android:orientation="horizontal" >
</LinearLayout>
Can anyone explain why it does this/correct me?
Thank you!
You're programmatically setting vertical orientation.-
Replace this line
tmpLinearLayout.setOrientation(LinearLayout.VERTICAL);
with this one
tmpLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
PS: As far as I know, Horizontal is the default orientation, so you actually could just delete the orientation line.
Change the android:orientation to vertical in your xml layout.

TableLayout Trouble

I am making 10x10 board of buttons, however, whenever I run my program, it is just 1 column with buttons extending to the bottom until I can't see them anymore. Here's my code:
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.game);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(32, 32);
final TableLayout container = (TableLayout) findViewById(R.id.tableLayout5);
Button btn[][] = new Button[10][10];
for(int i = 0; i<10; i++){
for(int j = 0; j<10; j++){
btn [i][j] = new Button(this);
container.addView(btn[i][j],i,lp);
}
}
}
and my XML code
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout5">
<Button
android:id="#+id/newgamebutton"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/newgame" />
<TableLayout
android:id="#+id/tableLayout5"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/newgamebutton"
android:orientation="vertical" >
</TableLayout>
</RelativeLayout>
I'm wondering why the buttons are extending all the way to the bottom and disappearing, I don't know if it has something to do with my layout height or not. I intend it to look like a 10x10 board
Firstly if you need a 10x10 board of buttons you need to create TableRows as well. Currently you have none. Please read the TableLayout documentation to understand how it works.
Secondly if you do need to programmatically set layout parameters on a Table you have to use TableRow.LayoutParams, and not ViewGroup.LayoutParams.
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(32, 32);
I used this instead:
TableRow.LayoutParams lp = new TableRow.LayoutParams(32, 32);

Categories

Resources