add buttons to TableRow programatically and fill in screen - android

I can add TableRow and also some buttons in it programaticaly like below image:
but I want to figure something like this that button fill in screen:
and here is my codes:
<TableLayout
android:id="#+id/Tb1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r2"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r3"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
</TableLayout>
and:
Button btn1;
private TableLayout buttonTableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
buttonTableLayout = (TableLayout) findViewById(R.id.Tb1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int row = 0; row < 3; row++) {
TableRow currentTableRow = getTableRow(row);
for (int column = 0; column < 5; column++) {
Button newGuessButton = (Button) inflater.inflate(R.layout.my_button, null);
String myName = new String(String.valueOf((row * 5) + column + 1));
newGuessButton.setText(myName);
currentTableRow.addView(newGuessButton);
}
}
}
});
}
private TableRow getTableRow(int row) {
return (TableRow) buttonTableLayout.getChildAt(row);
}
and the xml(my_button):
<Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/newButton" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"></Button>
please give me some advice to solve it.

Android TableRow class actually extends LinearLayout class, so you can use all of LinearLayout's features here.
In this case you can use android:layout_weight to equally stretch those buttons. Where you're adding those buttons just add the following piece of code.
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1;
button.setLayoutParams(p);
Or alternatively add these lines to your button.xml layout file:
android:layout_width="0dp"
android:layout_weight="1"
Docs say:
Equally weighted children
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".

Related

Dynamically add TableRow to above of TableLayout

I want to dynamically add TableRow to above of TableLayout. I use blow code:
In my activity:
TableLayout table = (TableLayout)ChatActivity.this.findViewById(R.id.tblChats);
for(int i = 1; i < 10; ++i)
{
TableRow row = (TableRow)LayoutInflater.from(ChatActivity.this).inflate(R.layout.chat_row, null);
((TextView)row.findViewById(R.id.attrib_name)).setText("A");
((TextView)row.findViewById(R.id.attrib_value)).setText(i + "");
table.addView(row);
}
table.requestLayout();
chat_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/attrib_name"
android:textStyle="bold"/>
<TextView android:id="#+id/attrib_value"
android:gravity="right"
android:textStyle="normal"/>
</TableRow>
As you know these codes adds TableRow in the bottom of the TableLayout.
Is there any way to add it to the above of TableLayout?
yes you can add row as runtime in table layout.
use below code like chart.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tblChats"
android:layout_width="match_parent"
android:layout_height="match_parent"> </TableLayout>
and make activity like below ..
public class ChartActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_layout);
TableLayout table = findViewById(R.id.tblChats);
for (int i = 0; i < 5; i++) {
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView textView = new TextView(this);
textView.setText("Name" + " " + i);
EditText editText = new EditText(this);
row.addView(textView);
table.addView(row, i);
}
}
}
According to Android Team's answer I realized I must change this line:
table.addView(row);
to this:
table.addView(row, 0);

setOrientation is not working for LinearLayout programatically

I am add dynamic data to the LinearLayout. default I declared as vertical orientation in xml. But I need to load horizontal data by programmatic way, Here set Orientation is not working.
LinearLayout llQuestionLayout = (LinearLayout) findViewById(R.id.ll_question_layout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llQuestionLayout.setLayoutParams(layoutParams);
llQuestionLayout.setOrientation(LinearLayout.HORIZONTAL);
private void populateLikert() {
if(llQuestionLayout.getChildCount() > 0) {
llQuestionLayout.removeAllViews();
}
RadioGroup radioGroup = new RadioGroup(this);
for(int i =0; i < 5; i++) {
RadioButton radioButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.likert_radio_button, null, false);
radioButton.setText("" + (i + 1));
radioGroup.addView(radioButton);
}
llQuestionLayout.addView(radioGroup);
}
<LinearLayout
android:id="#+id/ll_question_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:orientation="vertical"/>
Please suggest correct answer.

How to align these 2 buttons next to each other programmatically in Android?

I have written this piece of code. But it is not giving the proper result. Please let me know where is the mistake. And I don't want to use Linear Layout.
Here is the xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/custom_relativeLayout1"
android:orientation="horizontal"
android:background="#ffffff">
</RelativeLayout>
</LinearLayout>
String[] but = {"Hello", "Bye"};
int buttonCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customLayout = (RelativeLayout) findViewById(R.id.custom_relativeLayout1);
//customLayout is object of relativelayout.
buttonCount = but.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button [] butArray = new Button[buttonCount];
for (int i = 0; i < 2; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText(but[i]);
butArray[i].setId(i+1); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (butArray[i].getId() != 1)
{
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
}
else
{
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
}
}
RelativeLayout.RIGHT_OFhoryzontally or vertically align?
so for vertically
Btnparams.addRule(RelativeLayout.BELOW, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);
or for horizontally
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);
instead of
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
LinearLayout is very nice to use if you want do dynamically add Views and scale them the same. For example if you want to add buttons and have max 3 buttons per row, you can also use another LinearLayout to make a new row.
I 'abuse' the LinearLayout row and a Button for layoutparams templating.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_root"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/ll_row1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"/>
</LinearLayout>
</LinearLayout>
Example code:
// Get LL and template params
LinearLayout root = (LinearLayout) findViewById(R.id.ll_root);
LinearLayout row1 = (LinearLayout) findViewById(R.id.ll_row1);
ViewGroup.LayoutParams llRowParams = row1.getLayoutParams();
ViewGroup.LayoutParams btnParams = findViewById(R.id.btn1).getLayoutParams();
// Make new button
Button newBtn = new Button(context);
newBtn.setLayoutParams(btnParams);
newBtn.setText("Button 3");
// Add button to row1
row1.addView(newBtn);
// Add new row
LinearLayout row2 = new LinearLayout(context);
row2.setLayoutParams(llRowParams);
root.addView(row2);
// TODO: add buttons to new row
// TODO: some logic to decide between adding to row or creating new row and adding button
Note: code is not tested, but you should get the idea.
buttonCount = but.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button [] butArray = new Button[buttonCount];
for (int i = 0; i < 2; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText(but[i]);
butArray[i].setId(i+1); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (i != 0) {
Btnparams.addRule(RelativeLayout.RIGHT_OF, i-1);
}
customLayout.addView(butArray[i], Btnparams);
}

set two radio buttons in one row then next two radio is on second row and so on

i want to set 2 two radiobuttons one row , then next 2 radiobuttons on second row dynamically(Programmatically) I am new to android any body have idea or sample code of it. What i have do is:
RadioGroup radiogroup = (RadioGroup)findViewById(R.id.RadioGroup01);
for(int i=0;i<10;i++) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(i);
rdbtn.setText("test");
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(rdbtn);
radiogroup.addView(rdbtn);
ll.addView(radiogroup);
//i%2 == 0
}
You can use below code:
Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int row=0; row < 5; row++)
{
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
for(int i = 0; i < 2; i++) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId((row * 2) + i);
rdbtn.setText("test " + rdbtn.getId());
ll.addView(rdbtn);
}
((ViewGroup)findViewById(R.id.radiogroup)).addView(ll);
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RadioGroup
android:id="#+id/radiogroup"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Output on screen:
Hope this is what you were looking for. Cheers!

How to manage linear sub layout programmatically?

Issue about layout aliment.
i want to add view dynamically.
Logical code :
linear1 = (LinearLayout) findViewById(R.id.parent);
linear2 = (LinearLayout) findViewById(R.id.chiled);
int len = 4;
for (int i = 1; i <= len; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params1.gravity = Gravity.RIGHT;
final int id_txt;
ImageView iv = new ImageView(this);
iv.setId(i);
id_txt = iv.getId();
iv.setBackgroundResource(R.drawable.ic_launcher);
linear1.addView(iv, params);
iv = ((ImageView) findViewById(id_txt));
for (int j = 1; j < 2; j++) {
final int id_;
Button btn = new Button(this);
btn.setId(i);
id_ = btn.getId();
btn.setText("button " + id_);
linear2.addView(btn, params1);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(),
"Button clicked index = " + id_,
Toast.LENGTH_SHORT).show();
}
});
}
// btn.setBackgroundColor(Color.rgb(70, 80, 90));
// linear1.addView(txt, params);
// params.addRule(RelativeLayout.RIGHT_OF, txt.getId());
iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"text clicked index = " + id_txt,
Toast.LENGTH_SHORT).show();
}
});
}
}
Xml Code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/chiled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
I want to add image in parent view and two button in child view dynamical.
I inspires to make this type view form
Android heterogeneous gridview like pinterest?
It should be like as
current output as
I don't know where is the problem.
One strange issues i am facing now in my editor if i see layout in code then its shows me android:orientation="vertical" and if i see in outline that shows android:orientation="horizontal" for each layout. how is it possible ?
Help me to solve out.Thanks
EDIT:
First off, start by adding your buttons to the xml. Along with the image view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/extra_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/chiled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dislike" />
</LinearLayout>
</LinearLayout>
You can then remove any concept of adding views, as they already exist.
package com.example.yul;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class extra extends Activity {
Button like, dislike;
LinearLayout root, sub;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.extra);
like = (Button) findViewById(R.id.button1);
dislike = (Button) findViewById(R.id.button2);
// add button listeners here.
ImageView iv = (ImageView) findViewById(R.id.image);
iv.setBackgroundResource(R.drawable.ic_launcher);
}
}
}
This will only display the one image though, with buttons.
What you need to do is apply this xml, and code to a gridView or similar. As you will have id clashes otherwise.
Since the horizontal LinearLayout chiled is the first child of the vertical LinearLayout parent it is aligned as the first item. So adding Views to it will place them on top.
Try moving the chiled Layout to the root LinearLayout root.

Categories

Resources