dynamically creating tablelayout (android) - android

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);
}
}

Related

how to set the dynamic table layout to full screen of the particular layout?

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

How to display data in the tableLayout

Based on the source code from this webpage, it doesn't work because the data isn't displayed in the table. I also have tried a similar code but it didn't work.
What part am I missing?
The result don't display without any error message.
Today, I'm a newbie in Android.
Info:
*using Android studio with API 25
Thank you!
XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jfdimarzio.myapplication.MainActivity">
</TableLayout>
Java
package com.jfdimarzio.myapplication;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
TableLayout stk = (TableLayout) findViewById(R.id.activity_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Sl.No ");
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Product ");
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Unit Price ");
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Stock Remaining ");
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
stk.addView(tbrow0);
for (int i = 0; i < 25; i++) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText("" + i);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("Product " + i);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText("Rs." + i);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText("" + i * 15 / 32 * 10);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
stk.addView(tbrow);
}
}
}

how to create button dynamically and add click event to button in android

I need to add a button to the activity in android.
And when I click this button I need to get which checkbox is checked and unchecked.My code is
package com.example.a;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(this);
cb.setText("I'm dynamic!");
ll.addView(cb);
}
this.setContentView(sv);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(this);
btn.setText("Submit");
btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(btn);
sv.addView(ll);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//code
}
});
Better way of doing and you can easily customize it in better way.
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScrollView sv = (ScrollView) findViewById(R.id.myscroll);
LinearLayout ll = (LinearLayout) findViewById(R.id.mylinearLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 20; i++) {
View view = inflater.inflate(R.layout.coustom_check_box_design, null);
CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox);
cb.setText("I'm dynamic!" + i);
//cb.isChecked();
ll.addView(view);
}
}
}
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myscroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/mylinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
coustom_check_box_design.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:orientation="vertical">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox" />
</LinearLayout>
See the below one it may help you:-
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(this);
btn.setText("Submit");
btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(btn);
sv.addView(ll);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// implement the loop to get checked CheckBoxes
}
});
Try this to get the THe selected Checkbox using Arraylist
public class MainActivity extends AppCompatActivity {
private ArrayList<String> mCheckList=new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(int i = 0; i < 20; i++) {
final CheckBox cb = new CheckBox(this);
cb.setText("check"+i);
ll.addView(cb);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
mCheckList.add(cb.getText().toString());
}
else
{
mCheckList.remove(cb.getText().toString());
}
}
});
}
Button button=new Button(this);
button.setText("Submit");
ll.addView(button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,mCheckList.toString() +"CheckBox Select",Toast.LENGTH_SHORT).show();
}
});
this.setContentView(sv);
}

add layout programmatically in android button listener

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>

extend tablerow view

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.

Categories

Resources