I have an Android app and I am trying to create the following layout in code:
This is the code I have so far:
public class MyLayout extends LinearLayout {
private Context mContext;
public MyLayout(Context context) {
super(context);
mContext = context;
this.build();
}
private void build() {
LinearLayout parent = new LinearLayout(mContext);
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
GridView gridView = new GridView(this.mContext);
gridView.setNumColumns(2);
ViewGroup.LayoutParams params = new ViewGroup.MarginLayoutParams(
MarginLayoutParams.FILL_PARENT,
MarginLayoutParams.WRAP_CONTENT);
gridView.setLayoutParams(params);
parent.addView(gridView);
addView(parent);
}
}
How can I add the ImageView so that it is docked at the bottom of the LinearLayout?
You could use an xml layout like the following, and then load it with setContentView(R.layout.your_layout_name) inside your onCreate method.
<?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"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="10" >
<GridView
android:id="#+id/yourGridViewID"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="8" />
<ImageView
android:id="#+id/yourImageViewID"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="2"/>
</LinearLayout>
Refer this code :
public class MyLayout extends LinearLayout {
private Context mContext;
private LinearLayout parent;
public MyLayout(Context context) {
super(context);
mContext = context;
parent = new LinearLayout(mContext);
this.build();
}
private void build() {
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
GridView gridView = new GridView(this.mContext);
gridView.setNumColumns(2);
ViewGroup.LayoutParams params = new ViewGroup.MarginLayoutParams(
MarginLayoutParams.FILL_PARENT,
MarginLayoutParams.WRAP_CONTENT);
gridView.setLayoutParams(params);
ImageView imageview = new ImageView(this.mContext);
ViewGroup.LayoutParams params2 = new ViewGroup.MarginLayoutParams(
MarginLayoutParams.FILL_PARENT,
MarginLayoutParams.WRAP_CONTENT);
params2.gravity = Gravity.BOTTOM;
imageview.setLayout(params2);
parent.addView(gridView);
parent.addView(imageview);
addView(parent);
}
}
Related
i have some layout file and i try to add new relativelayout with programmatically
this is a my xml layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/rot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff0000" >
<ImageView
android:id="#+id/btn_categorry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
and this is java code
public class MainActivity extends Activity {
RelativeLayout myImage, mainlayout;
private ImageView img;
RelativeLayout staticlayout;
RelativeLayout.LayoutParams parms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (RelativeLayout) findViewById(R.id.rot);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
img = (ImageView) findViewById(R.id.btn_categorry);
parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new listener());
staticlayout.setId(10);
staticlayout.addView(add_btn);
parms.addRule(RelativeLayout.BELOW, R.id.rot);
staticlayout.setLayoutParams(parms);
mainlayout.addView(staticlayout);
}
});
}
class listener implements OnClickListener {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams costomparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout relativelayout = new RelativeLayout(
getApplicationContext());
relativelayout.setBackgroundColor(Color.parseColor("#AB3232"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams imagelayoutparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imagelayoutparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imagelayoutparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setOnClickListener(new listener());
add_btn.setLayoutParams(imagelayoutparam);
relativelayout.addView(add_btn);
costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());
relativelayout.setLayoutParams(costomparam);
mainlayout.addView(relativelayout);
}
}
}
i try to explain my problem. i can add new layout and imageview inside new layout(which i created
programmatically) and also i can add new layout in button click with a button i also created programmatically.
and now i want to write code to create new layout each time.i want recursion function.how i can solve my problem ?
if anyone knows solution problem please help me
Check if this solution fulfil your needs:
Activity code:
package com.example.abc;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private RelativeLayout myImage, mainlayout;
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (RelativeLayout) findViewById(R.id.rot);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
img = (ImageView) findViewById(R.id.btn_categorry);
img.setOnClickListener(new RecursionOnClickListener());
}
private int layoutId = 1;
class RecursionOnClickListener implements OnClickListener {
#Override
public void onClick(View v) {
RelativeLayout staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setId(layoutId++);
if(layoutId % 2 == 0) {
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
}
else {
staticlayout.setBackgroundColor(Color.parseColor("#FF0000"));
}
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new RecursionOnClickListener());
staticlayout.addView(add_btn);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms.addRule(RelativeLayout.BELOW, mainlayout.getChildAt(mainlayout.getChildCount()-1).getId());
staticlayout.setLayoutParams(parms);
mainlayout.addView(staticlayout);
}
}
}
Layout XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00" >
<RelativeLayout
android:id="#+id/rot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff0000" >
<ImageView
android:id="#+id/btn_categorry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
I have an Xml that add LinearLayout and RelativeLayout in ScrollView by programmatically.When i add Text with OnclickButton for first time show me message but for 2nd time get me crash :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1" >
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="Text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnSend"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Click" />
</LinearLayout>
</LinearLayout>
My code :
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
LinearLayout rilative;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
ScrollView sclView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
sclView = (ScrollView) findViewById(R.id.scrollID);
Button btn = (Button) findViewById(R.id.btnSend);
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUser.setText(Medtconversation);
relativeLayout = new RelativeLayout(context);
firstLinearLayout= new LinearLayout(context);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(txtviewUser,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
firstLinearLayout.addView(relativeLayout);
Crash here now======>sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
edtconversation.setText("");
}
});
}
}
I need that when i click on Button and send message for 2nd time create a new RelativeLayout in LinearLayout for show.(In scrollView)
Error :
AndroidRuntime
MainActivity$1.onClick(MainActivity.java:54)
mmmmm. I recommend that you using from table and you can add programmatically your TextView and etc. simply :
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
TextView txtviewUserT;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
TableRow tr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
Button btn = (Button) findViewById(R.id.btnSend);
final TableLayout tl = (TableLayout) findViewById(R.id.tbl);
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUserT = new TextView(MainActivity.this);
txtviewUserT.setText("OK");
txtviewUser.setText(Medtconversation);
tr = new TableRow(context);
tr.addView(txtviewUser);
tr.addView(txtviewUserT);
tl.addView(tr);
edtconversation.setText("");
}
});
}
}
Your XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xxx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<TableLayout
android:id="#+id/tbl"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1" >
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="Text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnSend"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Click" />
</LinearLayout>
</LinearLayout>
First you need to add the LinearLayout to ScrollView
Then add the relative layout to the LinearLayout
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
LinearLayout rilative;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
ScrollView sclView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
Button btn = (Button) findViewById(R.id.btnSend);
sclView = (ScrollView) findViewById(R.id.scrollID);
firstLinearLayout= new LinearLayout(this);
relativeLayout = new RelativeLayout(this);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUser.setText(Medtconversation);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(txtviewUser,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
sclView.addView(firstLinearLayout);
firstLinearLayout.addView(relativeLayout);
sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
edtconversation.setText("");
}
});
}
}
You are creating a new LinearLayout and RelativeLayout only once (inside onCreate), however you add them multiple times. Instead create new instances from inside of the onClick method:
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
LinearLayout firstLinearLayout= new LinearLayout(context);
RelativeLayout relativeLayout = new RelativeLayout(context);
...
firstLinearLayout.addView(relativeLayout);
...
}
});
How can we convert the below xml progrmatically
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/list_fragment"
android:name="com.aaa.bbb.activity.FragmentedListActivity$MyListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#27577f" />
<fragment
android:id="#+id/detail_fragment"
android:name="com.aaa.bbb.activity.FragmentedListActivity$MyDetailFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
this may help you...
private static final int ID_MY_FRAGMENT = 0xDEAF1;
private static final int ID_MY_DETAIL_FRAGMENT = 0xDEAF2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = getView();
setContentView(contentView);
getSupportFragmentManager().beginTransaction()
.replace(ID_MY_FRAGMENT, new MyListFragment())
.replace(ID_MY_DETAIL_FRAGMENT, new MyDetailFragment()).commit();
}
private View getView() {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
FrameLayout frameLayout = new FrameLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
frameLayout.setLayoutParams(layoutParams);
frameLayout.setId(ID_MY_FRAGMENT);
layout.addView(frameLayout);
View view = new View(this);
layoutParams = new LinearLayout.LayoutParams((int) (1 * getResources()
.getDisplayMetrics().density),
LinearLayout.LayoutParams.MATCH_PARENT);
view.setLayoutParams(layoutParams);
view.setBackgroundColor(27555);
layout.addView(view);
frameLayout = new FrameLayout(this);
layoutParams = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 2);
frameLayout.setLayoutParams(layoutParams);
frameLayout.setId(ID_MY_DETAIL_FRAGMENT);
layout.addView(frameLayout);
return layout;
}
You can change your fragment tags to FrameLayout ones, remove the attribute name in each tag, and then add your fragments programmatically like:
MyListFragment fragment = new MyListFragment();
MyDetailFragment fragment2 = new MyDetailFragment();
fragmentTransaction.add(R.id.list_fragment, fragment);
fragmentTransaction.add(R.id.detail_fragment, fragment2);
fragmentTransaction.commit();
i need add a new button and textedit to my LinearLayout.. if I create a new param, the buttons are created over each other..
my mainactivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyTouchEventView tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
addContentView(tv.btnSave, tv.params2);
}
and MyTouchEventView:
public class MyTouchEventView extends View {
public Button btnReset;
public Button btnSave;
public LinearLayout.LayoutParams params;
public LinearLayout.LayoutParams params2;
public MyTouchEventView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Vymazat platno");
btnSave = new Button(context);
btnSave.setText("Ulozit");
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
btnReset.setLayoutParams(params);
params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(40, 20, 40, 30);
btnSave.setLayoutParams(params2);
http://i.stack.imgur.com/kvY1T.png
Why don't you create a simple layout for your activity?
main_layout.xml
<?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" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="myText" />
<Button
android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="myText" />
</LinearLayout>
Then in the onCreate method:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
Also see http://developer.android.com/guide/topics/ui/layout/linear.html
I am trying to programmatically create this xml view by extending tablerow view:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:weightSum="1.0" >
<Button
android:id="#+id/tester_button"
style="?android:attr/buttonStyleSmall"
android:layout_weight=".2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="A" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_weight=".7"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_weight=".1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
</TableRow>
This is my class of the extended row:
public class CategoryRow extends TableRow {
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
LayoutParams innerParams = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
CheckBox check;
Button arrowButton;
TextView categoryTitle;
public CategoryRow(Context context) {
super(context);
this.setLayoutParams(rowParams);
this.setGravity(Gravity.CENTER);
this.setWeightSum(1);
check = new CheckBox(context);
innerParams.weight = (float) 0.1;
check.setLayoutParams(innerParams);
arrowButton = new Button(context);
innerParams.weight = (float) 0.2;
arrowButton.setLayoutParams(innerParams);
arrowButton.setText("A");
categoryTitle = new TextView(context);
innerParams.weight = (float) 0.7;
categoryTitle.setLayoutParams(innerParams);
categoryTitle.setTextAppearance(context, android.R.attr.textAppearanceLarge);
//add views to row
this.addView(arrowButton);
this.addView(categoryTitle);
this.addView(check);
}
public void setCategoryTitle(String title){
categoryTitle.setText(title);
}
public void checkCategory(){
check.setChecked(true);
}
public void unCheckCategory(){
check.setChecked(false);
}
}
in my activity I am adding the CategoryRow view to the TableLayout by addView function.
public class NotificationCategorise extends Activity {
TableLayout categoryConteiner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_categorise);
categoryConteiner = (TableLayout) findViewById(R.id.categories_conteiner);
CategoryRow categoryRow = new CategoryRow(this);
categoryRow.setCategoryTitle("bla");
categoryConteiner.addView(categoryRow);
}
My problem is that it the CategoryRow not added to the activity sceen.
I have changed the extended class to this code, and it worked fine.
import android.content.Context;
import android.view.Gravity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableRow;
import android.widget.TextView;
public class CategoryRow extends TableRow {
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams innerParams1 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
LayoutParams innerParams2 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
LayoutParams innerParams3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
CheckBox check;
Button arrowButton;
TextView categoryTitle;
public CategoryRow(Context context) {
super(context);
this.setLayoutParams(rowParams);
this.setWeightSum(1f);
check = new CheckBox(context);
innerParams1.weight = 0.1f;
check.setLayoutParams(innerParams1);
arrowButton = new Button(context);
innerParams2.weight = 0.2f;
arrowButton.setLayoutParams(innerParams2);
arrowButton.setText("A");
categoryTitle = new TextView(context);
categoryTitle.setGravity(Gravity.CENTER);
innerParams3.weight = 0.7f;
categoryTitle.setLayoutParams(innerParams3);
categoryTitle.setTextAppearance(context, android.R.attr.textAppearanceLarge);
//add views to row
this.addView(arrowButton);
this.addView(categoryTitle);
this.addView(check);
}
public void setCategoryTitle(String title){
categoryTitle.setText(title);
}
public void checkCategory(){
check.setChecked(true);
}
public void unCheckCategory(){
check.setChecked(false);
}
}
Ok, I don't think you can pass in the GRAVITY.CENTER as an argument for the LayoutParams constructor for the row. You change it for each individual View added to the row, but not for the row itself. Change:
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
to
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
See if that works.