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.
Related
I am trying to fetch the database using php,i fetched and shown in dynamic table layout,the rows where created according to the number of rows in the database,but the table layout is in center of the screen but i need it to be fit in whole screen of the mobile/tab.
i tried to change the height and width of the all layout used in the particular screen but nothing is changed can anyone help me to fix this.
this is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".Colleagues_Schedule">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table_main"
android:layout_margin="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:onClick="Back"
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PREVIOUS" />
<Button
android:onClick="Home"
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HOME" />
<Button android:onClick="Next"
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
main activity
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.nex3z.notificationbadge.NotificationBadge;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import static java.util.Calendar.DATE;
public class Colleagues_Schedule extends AppCompatActivity {
int i;
String result;
int size=20;
Context context;
int count = 0;
NotificationBadge mBadge;
Button buttonname, btnlogout, btntoday, btnyesterday, btntomorrow;
JSONArray jsonArray_New = null;
String[] ScheduleDates, StartTimes, Endtimes,Names;
String Send_Yesterday, Send_Today, Send_Tomorrow;
String ScheduleDate, StartTime, Endtime, Today, Yesterday, Tomarrow, Date,Name;
java.util.Date date1, Todaydate, Yesterdaydate, Tomarrowdate,date2;
TextView yes_date, yes_ST, yes_ET, tod_date, tod_ST, tod_ET, tom_date, tom_ST, tom_ET;
private ArrayList<String> ScheduleDateArray = new ArrayList<>();
private ArrayList<String> StartTimeArray = new ArrayList<>();
private ArrayList<String> EndtimeArray = new ArrayList<>();
private ArrayList<String> NameArray = new ArrayList<>();
SharedPreferences mPrefs;
GridLayout mainGrid2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_colleagues__schedule);
result = getIntent().getStringExtra("rootJson");
init();
}
#SuppressLint("ResourceType")
public void init() {
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Name ");//date
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Date ");//name
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Start Time ");//starttime
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" End Time ");//endtime
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
stk.addView(tbrow0);
try {
JSONObject jsonObjects = new JSONObject(result);
jsonArray_New = jsonObjects.getJSONArray("VIEW_SCHEDULE");
//Fetching the data from the php file
try {
for (int i = 0; i < jsonArray_New.length(); i++) {
JSONObject jsonObject = jsonArray_New.getJSONObject(i);
Name = jsonObject.getString("Name");
NameArray.add(Name);
ScheduleDate = jsonObject.getString("ScheduleDate");
ScheduleDateArray.add(ScheduleDate);
StartTime = jsonObject.getString("StartTime");
StartTimeArray.add(StartTime);
Endtime = jsonObject.getString("Endtime");
EndtimeArray.add(Endtime);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Initilize the arraylist to array
Names = new String[NameArray.size()];
ScheduleDates = new String[ScheduleDateArray.size()];
StartTimes = new String[StartTimeArray.size()];
Endtimes = new String[EndtimeArray.size()];
for (int i = 0; i < ScheduleDateArray.size(); i++) {
Names[i] = NameArray.get(i);
ScheduleDates[i] = ScheduleDateArray.get(i);
StartTimes[i] = StartTimeArray.get(i);
Endtimes[i] = EndtimeArray.get(i);
}
//Search for Today schedule
for(int i=0;i<jsonArray_New.length();i++)
{
TableRow tbrow = new TableRow(this);
tbrow.setBackgroundResource(R.drawable.row_border);
TextView t1v = new TextView(this);
TextView t2v = new TextView(this);
TextView t3v = new TextView(this);
TextView t4v = new TextView(this);
tbrow.setBackgroundResource(R.drawable.row_border);
t1v.setText(Names[i]);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
t2v.setText(ScheduleDates[i] );
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
t3v.setText( StartTimes[i]);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
t4v.setText(Endtimes[i]);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
stk.addView(tbrow);
tbrow.setClickable(true); //allows you to select a specific row
tbrow.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow tablerow = (TableRow) view;
TextView sample = (TextView) tablerow.getChildAt(1);
String result=sample.getText().toString();
Toast toast = Toast.makeText(Colleagues_Schedule.this, result, Toast.LENGTH_LONG);
toast.show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
and this is my output scrren.
output screen
Try giving layout params to every row you add dynamically and update your init() function like below:
#SuppressLint("ResourceType")
public void init() {
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
//added layout params and weight
tbrow0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
TextView tv0 = new TextView(this);
tv0.setText(" Name ");//date
tv0.setTextColor(Color.WHITE);
//added layout params and weight
tv0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Date ");//name
tv1.setTextColor(Color.WHITE);
//added layout params and weight
tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Start Time ");//starttime
tv2.setTextColor(Color.WHITE);
//added layout params and weight
tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" End Time ");//endtime
tv3.setTextColor(Color.WHITE);
//added layout params and weight
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv3);
//added layout params and weight
stk.addView(tbrow0, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
try {
JSONObject jsonObjects = new JSONObject(result);
jsonArray_New = jsonObjects.getJSONArray("VIEW_SCHEDULE");
//Fetching the data from the php file
try {
for (int i = 0; i < jsonArray_New.length(); i++) {
JSONObject jsonObject = jsonArray_New.getJSONObject(i);
Name = jsonObject.getString("Name");
NameArray.add(Name);
ScheduleDate = jsonObject.getString("ScheduleDate");
ScheduleDateArray.add(ScheduleDate);
StartTime = jsonObject.getString("StartTime");
StartTimeArray.add(StartTime);
Endtime = jsonObject.getString("Endtime");
EndtimeArray.add(Endtime);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Initilize the arraylist to array
Names = new String[NameArray.size()];
ScheduleDates = new String[ScheduleDateArray.size()];
StartTimes = new String[StartTimeArray.size()];
Endtimes = new String[EndtimeArray.size()];
for (int i = 0; i < ScheduleDateArray.size(); i++) {
Names[i] = NameArray.get(i);
ScheduleDates[i] = ScheduleDateArray.get(i);
StartTimes[i] = StartTimeArray.get(i);
Endtimes[i] = EndtimeArray.get(i);
}
//Search for Today schedule
for (int i = 0; i < jsonArray_New.length(); i++) {
TableRow tbrow = new TableRow(this);
tbrow.setBackgroundResource(R.drawable.row_border);
//added layout params and weight
tbrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
TextView t1v = new TextView(this);
TextView t2v = new TextView(this);
TextView t3v = new TextView(this);
TextView t4v = new TextView(this);
tbrow.setBackgroundResource(R.drawable.row_border);
t1v.setText(Names[i]);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
//added layout params and weight
t1v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t1v);
t2v.setText(ScheduleDates[i]);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
//added layout params and weight
t2v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t2v);
t3v.setText(StartTimes[i]);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
//added layout params and weight
t3v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t3v);
t4v.setText(Endtimes[i]);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
//added layout params and weight
t4v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t4v);
//added layout params and weight
stk.addView(tbrow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
tbrow.setClickable(true); //allows you to select a specific row
tbrow.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow tablerow = (TableRow) view;
TextView sample = (TextView) tablerow.getChildAt(1);
String result = sample.getText().toString();
Toast toast = Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG);
toast.show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
And provide android:fillViewport="true" to your ScrollView with id=scrollView1 and HorizontalScrollView with id=hscrll1
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 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 have designed a layout as in the image below:
After entering text in the EditText, when I press the Add+ Button the TextView and Button will be added as shown in the image below:
I want to show the Button on the right side of the TextView. How should I do this?
Another question, how should I remove corresponding View when user clicks a button? The code:
public class ExampleActivity extends Activity {
private LinearLayout mLayout;
private EditText mEditText;
private Button mButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mEditText = (EditText) findViewById(R.id.editText);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText()
.toString()));
mLayout.addView(createNewButton());
}
});
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New text: " + text);
return textView;
}
private Button createNewButton() {
final LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(lparams);
button.setText(" - ");
return button;
}
}
The TextViews and Buttons are stacked because you probably use a LinearLayout with the orientation vertical. You could wrap your TextView + Button into a LinearLayout and then add this LinearLayout to your own layout or you could use a TableLayout like below(I've added some ids so you can delete the rows you want):
public class SomeActivity extends Activity {
private EditText mInput;
private TableLayout mTable;
private static int sCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addButton = (Button) findViewById(R.id.add);
mInput = (EditText) findViewById(R.id.editText1);
mTable = (TableLayout) findViewById(R.id.table1);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mTable.addView(addRow(mInput.getText().toString()));
}
});
}
private TableRow addRow(String s) {
TableRow tr = new TableRow(this);
tr.setId(1000 + sCount);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams tlparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
tr.addView(textView);
TableRow.LayoutParams blparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(blparams);
button.setText(" - ");
button.setId(2000 + sCount);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mTable.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(button);
sCount++;
return tr;
}
}
where the main layout file is:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TableLayout
android:id="#+id/table1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</LinearLayout>
</ScrollView>
If for some reason, you don't like the TableLayout use a LinearLayout to wrap you TextView and Button with the layout file above(and of course remove the TableLayout):
//...
ll = (LinearLayout) findViewById(R.id.parent);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//where ll is the LinearLayout with the id parent
ll.addView(addRow(mInput.getText().toString()));
}
});
}
private LinearLayout addRow(String s) {
LinearLayout tr = new LinearLayout(this);
tr.setId(1000 + sCount);
tr.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams tlparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
tr.addView(textView);
LinearLayout.LayoutParams blparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(blparams);
button.setText(" - ");
button.setId(2000 + sCount);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
ll.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(button);
sCount++;
return tr;
}
LinearLayout having the property Orientation to align control either Vertically/Horizontally
so just set Orientation of the same
http://developer.android.com/reference/android/widget/LinearLayout.html
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mLayout.setOrientation(LinearLayout.HORIZONTAL);
Updated
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="schemas.android.com/apk/res/android";
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText"
android:layout_width="293dp"
android:layout_height="wrap_content" >
<requestFocus /> </EditText>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add+" />
</LinearLayout>
I want to create inner tablelayout and add there 1 row with 2 columns, following code shows nothing, why?
Here's main activity:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
TextView tv2=new TextView(this);
tv1.setLayoutParams(params);
tv2.setLayoutParams(params);
tv1.setText("Hello1!");
tv2.setText("Hello2!");
TableLayout layoutINNER = new TableLayout(this);
layoutINNER.setLayoutParams(params);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
tr.addView(tv1);
tr.addView(tv2);
layoutINNER.addView(tr);
LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
main.addView(layoutINNER);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/android:main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
UPDATE:
Well, finally i solved this problem, it was just needed to use TableRow.LayoutParams with TextViews, not LinearLayout.LayoutParams or some other
Here is the working code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
TextView tv2=new TextView(this);
android.widget.TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(trparams);
tv2.setLayoutParams(trparams);
tv1.setText("Hello1!");
tv2.setText("Hello2!");
TableLayout layoutINNER = new TableLayout(this);
layoutINNER.setLayoutParams(params);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
tr.addView(tv1);
tr.addView(tv2);
layoutINNER.addView(tr);
LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
main.addView(layoutINNER);
}
}