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.
Related
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>
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.
I have a layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/map_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="#+id/skip_button"
android:layout_width="match_parent"
android:layout_height="#dimen/button_height"
android:text="#string/skip"/>
</LinearLayout>
My code hides skip_button (via .setVisibility(View.GONE)) when user pick its coordinates in map_view webview. Also user has an ability to reset its coordinates and then button must appear again.
It works on Android 5, but on Android 4 button stay hidden even after .setVisibility(View.VISIBLE). I tried:
mapСontainer.getParent().requestLayout();
mapСontainer.refreshDrawableState();
skipButton.refreshDrawableState();
Wrap webview into LinearLayout
But all this doesn't help.
Beat this. It looks like this is a api16 bug so i have to workaround it...
if (dataOk) {
skipButton.setVisibility(View.GONE);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mapСontainer.getLayoutParams();
layoutParams.height = 0;
layoutParams.weight = 1;
mapСontainer.setLayoutParams(layoutParams);
mapСontainer.getParent().requestLayout();
} else {
int visibility = skipButton.getVisibility();
skipButton.setVisibility(View.VISIBLE);
if (visibility == View.GONE) {
int height = mapСontainer.getMeasuredHeight();
ViewGroup.LayoutParams skipParams = skipButton.getLayoutParams();
LinearLayout.LayoutParams containerParams = (LinearLayout.LayoutParams) mapСontainer.getLayoutParams();
containerParams.height = height - skipParams.height;
containerParams.weight = 0;
mapСontainer.setLayoutParams(containerParams);
}
}
In my project i managed to display the database in the form of rows and columns of edit Texts and text Views.But the as the number of rows and columns increase it is not possible to view the added data , they dont fit into the screen.
so i think i needed a horizontal scrollview and a vertical scrollview for viewing entire rows and columns. Below
is my code kindly help me how to achieve this.
Please do help me thanks in advance
xml code is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:stretchColumns="0,1,2,3,4,5"
android:id="#+id/maintable"
android:layout_height="wrap_content"
android:layout_weight="1.0">
</TableLayout>
</LinearLayout>
The following is the code for page where i wanna display my rows and columns
public class ViewActivity extends Activity {
EditText col1;
EditText col2;
TextView Id;
EditText col4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
ScrollView sv = new ScrollView(this);
HorizontalScrollView hs =new HorizontalScrollView(this);
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
//CREATING A LIST OF HEADINGS
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ));
Id = new TextView(this);
Id.setText("COL1");
tr1.addView(Id);
Id = new TextView(this);
Id.setText("COL2");
tr1.addView(Id);
Id = new TextView(this);
Id.setText("COL3");
tr1.addView(Id);
Id = new TextView(this);
Id.setText("Rssi");
tr1.addView(COL4);
Id = new TextView(this);
Id.setText("Delete");
tr1.addView(COL5);
Id = new TextView(this);
Id.setText("Update");
tr1.addView(COL6);
hs.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
tl.addView(tr1,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// CREATING ROWS AND COLUMNS DYNAMICALLY
final DBclass entry = new DBclass(ViewActivity.this);
entry.open();
Cursor c = entry.getAllRecords();
int count =0;
if(c.moveToFirst()){
do{
TableRow tr = new TableRow(this);
tr.setId(count);
tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ));
/* CODE FOR
COLUMNS DISPLAYED IN THE FORM OF
EDITTEXTS AND TEXTVIEWS
*/
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
count++;
}while(c.moveToNext());
}
}
I'm fairly sure you could simply put your TableLayout inside a ScrollView to solve this problem. No need to fiddle with adding Views programatically, just predefine it in the XML layout resource. Like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="wrap_content"
android:stretchColumns="0,1,2,3,4,5"
android:id="#+id/maintable"
android:layout_height="wrap_content"
android:layout_weight="1.0">
</TableLayout>
</ScrollView>
</LinearLayout>
Keep in mind ScrollView is vertical, so you'd need to add a HorizontalScrollView in there to add horizontal scrolling as well.
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);