I am new in java and android programing. I want to add rows(include some text box) to table layout by code and delete some of them.and finaly get their text box valus.how can i do it?
Here is a simple example to do what you want:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
Activity:
public class TableLayoutActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
final TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
for (int i = 0; i < 5; i++) {
// Creation row
final TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
// Creation textView
final TextView text = new TextView(this);
text.setText("Test" + i);
text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
// Creation button
final Button button = new Button(this);
button.setText("Delete");
button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final TableRow parent = (TableRow) v.getParent();
tableLayout.removeView(parent);
}
});
tableRow.addView(text);
tableRow.addView(button);
tableLayout.addView(tableRow);
}
}
}
I recently got the same problem. I fixed it like this.
Suppose your TableLayout is called table and als has this id in the xml layout.
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
Suppose you have a list of Person objects, this is the way to populate the table:
ArrayList<Person> persons = getPersonList(); // --> suppose getting the list from this function
TableLayout table = (TableLayout) findViewById(R.id.table);
for(Person person : persons) {
TableRow row = new TableRow(this);
TextView tvName = new TextView(this);
TextView tvAge = new TextView(this);
TextView tvMail = new TextView(this);
tvName.setText(person.getName());
tvAge.setText(String.valueOf(person.getAge());
tvMail.setText(person.getMail());
row.addView(tvName);
row.addView(tvAge);
row.addView(tvMail);
table.addView(row);
}
This basically means that each person has its own row. For every property of your person, 1 column is used.
Regards
Related
I'm trying to create programmatically parent LinearLayout
and 3 TextViews.
One TextView must be aligned in top left, the second one in the center of parent
and the third one to the right and bottom of my screen. Everything must be done in code.
I'm almost done, but there is still some problem.
All my 3 views are on the top
My code:
public class ActivityFour extends AppCompatActivity {
private LinearLayout mLinearLayout;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private static final int TV_ID1 = 101;
private static final int TV_ID2 = 102;
private static final int TV_ID3 = 103;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams llParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(mLinearLayout, llParams);
LinearLayout.LayoutParams linlayout_params1 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params3 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linlayout_params1.setMargins(16,16,16,16);
tv1 = new TextView(this);
tv1.setId(TV_ID1);
linlayout_params1.gravity = Gravity.START;
linlayout_params1.gravity = Gravity.TOP;
mLinearLayout.addView(tv1, linlayout_params1);
tv1.setText("TextView number ONE");
linlayout_params2.setMargins(16,16,16,16);
tv2 = new TextView(this);
tv2.setId(TV_ID2);
linlayout_params2.gravity = Gravity.CENTER;
mLinearLayout.addView(tv2, linlayout_params2);
tv2.setText("TextView number TWO");
linlayout_params3.setMargins(16,16,16,16);
tv3 = new TextView(this);
tv3.setId(TV_ID3);
linlayout_params3.gravity = Gravity.END;
mLinearLayout.addView(tv3, linlayout_params3);
tv3.setText("TextView number THREE");
}
}
After adding the weight property to all view.params it looks like this
In order to make the LinearLayout fill all the available space, you need to set the android:layout_weight attribute on the middle child View. You can do it programmatically by using a Constructor which takes the weight as third parameter:
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
First of all no need to create liner layout for all textview you can do like this:
in xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/test"
tools:context=".activity.TestActivity"/>
and in activity file :
LinearLayout layout = findViewById(R.id.test);
TextView product_1 = new TextView(this);
product_1.setText("Product 1");
product_1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
layout.addView(product_1);
TextView product_2 = new TextView(this);
product_2.setText("Product 2");
product_2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_2.setGravity(Gravity.CENTER);
layout.addView(product_2);
TextView product_3 = new TextView(this);
product_3.setText("Product 3");
product_3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_3.setGravity(Gravity.END|Gravity.BOTTOM);
layout.addView(product_3);
I am getting a json array through my api.
I want to display the data in my view, but I do not know how to generate for example textviews and put the data inside each views. (My wish would be to show the data in something like a html table, but at the moment I am fine to understand how to put the data in dynamic textviews
)
String id = jsonobject.getString("id");
String category = jsonobject.getString("category");
String content = jsonobject.getString("content");
Do hold the correct data.
Following method is in my Async task:
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result != "error") {
JSONArray jsonArray;
TextView textview = (TextView) findViewById(R.id.jokeContent);
try {
jsonArray = new JSONArray(result);
for(int i=0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String id = jsonobject.getString("id");
String category = jsonobject.getString("category");
String content = jsonobject.getString("content");
}
} catch (Exception e) {
Toasty.error(context, "Catch , test1!", Toast.LENGTH_SHORT, true).show();
}
} else {
Toasty.error(context, "Else , test2!", Toast.LENGTH_SHORT, true).show();
}
}
you can start by adding a table layout to your Layout, next create a row on your layout files something like this should work:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row">
<TextView
android:id="#+id/ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="20"
android:text="TextView" />
<TextView
android:id="#+id/Category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="20"
android:text="TextView" />
<TextView
android:id="#+id/Content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="20"
android:text="TextView" />
</TableRow>
then you can add the rows and fill them with info like this on your activity by using an inflater
for(int i=0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String id = jsonobject.getString("id");
String category = jsonobject.getString("category");
String content = jsonobject.getString("content");
TableLayout tableLayout = (TableLayout)findViewById(R.id.table);
TableRow tablerow = (TableRow)getLayoutInflater().inflate(R.layout.row, tableLayout,false);
((TextView)tablerow.findViewById(R.id.ID)).setText(id);
((TextView)tablerow.findViewById(R.id.Category)).setText(category);
((TextView)tablerow.findViewById(R.id.Content)).setText(content);
tableLayout.addView(tablerow);
}
hope it helps
If I understood your question correctly you want to create a textView and update it after getting a response of your Asynk task.
The idea is..
Create Activity (e.x. Main Activity), create AsynkTask
Create layout and TextView inside
Find TextView in Activity and not in Asynk task. In your case
TextView textview = (TextView) findViewById(R.id.jokeContent);
Put data to some common resource from AsyncTask and get them in Activity.
E.x. (Kotlin) through interface
interface AsyncResponse {
fun finishProcess(result: String)
}
...
class AsyncExecuter constructor(val asyncResp: AsyncResponse)
...
override fun onPostExecute(result: String) { //AsynkTask
super.onPostExecute(result)
asyncResp.finishProcess(result) //asynkResp is interfase which is put to AsynkTask constructor
}
Activity implements my own AsynkResp interface and calls AsynkTask
override fun onResume() {
super.onResume()
AsyncExecuter(this).execute()
}
And implement method from interface in activity where update TextView
override fun finishProcess(output: String) {
textview?.text = output
}
The main idea is manipulation with UI should be done from Activity and not from parallel threads.
In this example, i have created a list for a prefixed array list. Instead of XML i use java to create dynamic views.
public class MainActivity extends AppCompatActivity { String[] wordlist = new String[] { "Shahi Paneer", "Shahi Thali","Shahi Parantha","Kadai Paneer","Mix Parantha", }; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); ListView list = new ListView(this); list.setBackgroundColor(Color.rgb(240,255,255) ); list.setAdapter(new MyAdapter(this, wordlist)); setContentView(list); } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, String[] strings) { super(context, -1, -1, strings); } #RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) #Override public View getView(int position, View convertView, ViewGroup parent) { Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/BLESD___ .ttf"); Typeface rupeeSymbol = Typeface.createFromAsset(getAssets(), "fonts/IndianRupee.ttf"); Typeface maze = Typeface.createFromAsset(getAssets(), "fonts/The Heart Maze Demo.ttf"); Typeface epicselfie = Typeface.createFromAsset(getAssets(), "fonts/My Epic Selfie Demo.ttf"); Typeface novaoval = Typeface.createFromAsset(getAssets(), "fonts/NovaOval.ttf"); LinearLayout listLayout = new LinearLayout(MainActivity.this); listLayout.setLayoutParams(new AbsListView.LayoutParams( AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT)); listLayout.setId(View.generateViewId()); listLayout.setOrientation(LinearLayout.VERTICAL); LinearLayout layout1 = new LinearLayout(MainActivity.this); layout1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); layout1.setOrientation(LinearLayout.HORIZONTAL); listLayout.addView(layout1); ImageView imgView = new ImageView(MainActivity.this); imgView.setImageResource(R.drawable.thali); imgView.setPadding(20,20,20,10); imgView.setAdjustViewBounds(true); layout1.addView(imgView); LinearLayout layout2 = new LinearLayout(MainActivity.this); layout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); layout2.setOrientation(LinearLayout.HORIZONTAL); listLayout.addView(layout2); TextView listText = new TextView(MainActivity.this); listText.setTextColor(Color.BLACK); listText.setPadding(30,0,0,0); listText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40); listText.setTypeface(maze,maze.BOLD); listText.setId(View.generateViewId()); layout2.addView(listText); LinearLayout cartlayout = new LinearLayout(MainActivity.this); cartlayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); cartlayout.setOrientation(LinearLayout.HORIZONTAL); cartlayout.setPadding(0,0,30,0); cartlayout.setGravity(Gravity.RIGHT); // layout3.setPadding(0,10,0,20); layout2.addView(cartlayout); ImageView addCartView = new ImageView(MainActivity.this); addCartView.setImageResource(R.drawable.add); // addCartView.setPadding(200,0,0,0); cartlayout.addView(addCartView); TextView NOFProd = new TextView(MainActivity.this); NOFProd.setId(View.generateViewId()); NOFProd.setTypeface(rupeeSymbol); NOFProd.setPadding(10,20,10,0); NOFProd.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25); // NOFProd.s NOFProd.setText("0"); cartlayout.addView(NOFProd); ImageView removeCartView = new ImageView(MainActivity.this); removeCartView.setImageResource(R.drawable.remove); // removeCartView.setPadding(20,5,20,0); cartlayout.addView(removeCartView); TextView descText = new TextView(MainActivity.this); descText.setTextColor(Color.DKGRAY); descText.setPadding(30,0,0,0); descText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); descText.setTypeface(epicselfie); descText.setText("Swadist Bhojan"); descText.setId(View.generateViewId()); listLayout.addView(descText); // price and others Textview LinearLayout layout3 = new LinearLayout(MainActivity.this); layout3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); layout3.setOrientation(LinearLayout.HORIZONTAL); layout3.setPadding(0,10,0,20); listLayout.addView(layout3); TextView pricesymbol = new TextView(MainActivity.this); pricesymbol.setId(View.generateViewId()); pricesymbol.setTypeface(rupeeSymbol); pricesymbol.setPadding(30,10,0,0); pricesymbol.setText("`"); layout3.addView(pricesymbol); TextView priceText = new TextView(MainActivity.this); priceText.setId(View.generateViewId()); priceText.setTypeface(novaoval); priceText.setPadding(10,10,0,0); priceText.setText(": 200"); layout3.addView(priceText); TextView rating = new TextView(MainActivity.this); rating.setId(View.generateViewId()); rating.setPadding(50,0,0,0); rating.setTypeface(novaoval); rating.setText("Rating : 3.5"); layout3.addView(rating); TextView any = new TextView(MainActivity.this); any.setPadding(50,0,0,0); any.setTypeface(novaoval); any.setId(View.generateViewId()); any.setText("anything "); layout3.addView(any); listText.setText(super.getItem(position)); return listLayout; } } }
I'm trying to create a dynamic TableLayout with data from database. I store the data in an ArrayList and use it to setText for the TextView. I used debug to check and the ArrayList had the correct number of values as in the database. It's not empty. But still the screen is blank. I tried to setTextColor for the TextView but it still was empty. The first 5 TextViews are for the heading. Later I use for loop for setting the text. I'm trying to do this in a Fragment btw.
Edit: I even checked the value of the bookingHistoryList.get(0). The value is correct. I can't display it!
Here's the code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
sessionManager = new SessionManager(getActivity());
databaseHelper = new DatabaseHelper(getActivity());
bookingHistoryList = new ArrayList();
view = inflater.inflate(R.layout.fragment_preferences, container, false);
tableLayout = (TableLayout)view.findViewById(R.id.hTableLayout);
TableRow pickDate = new TableRow(getActivity());
TextView pickDateTxt = new TextView(getActivity());
pickDateTxt.setText("Pick up Date");
pickDate.addView(pickDateTxt);
TableRow pickTime = new TableRow(getActivity());
TextView pickTimeTxt = new TextView(getActivity());
pickTimeTxt.setText("Pick up Time");
pickTime.addView(pickTimeTxt);
TableRow pickUpLocation = new TableRow(getActivity());
TextView pickUpLocationTxt = new TextView(getActivity());
pickUpLocationTxt.setText("Pick up Location");
pickUpLocation.addView(pickUpLocationTxt);
TableRow destination = new TableRow(getActivity());
TextView destinationTxt = new TextView(getActivity());
destinationTxt.setText("Destination");
destination.addView(destinationTxt);
TableRow vehicleType = new TableRow(getActivity());
TextView vehicleTypeTxt = new TextView(getActivity());
vehicleTypeTxt.setText("Vehicle Type");
vehicleType.addView(vehicleTypeTxt);
// databaseHelper.delete();
String getUserEmail = sessionManager.getUserEmail();
bookingHistoryList = databaseHelper.getBookingHistory(sessionManager.getUserEmail());
rowCount = DatabaseUtils.queryNumEntries(databaseHelper.getReadableDatabase(),"bookingDetails");
n = (int) rowCount*5;
for(int i = 0; i<n; i++) {
TableRow tableRow = new TableRow(getActivity());
TextView textView = new TextView(getActivity());
textView.setText(bookingHistoryList.get(i).toString());
textView.setTextColor(Color.BLACK);
tableRow.addView(textView);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TableRow tableRow1 = new TableRow(getActivity());
TextView textView1 = new TextView(getActivity());
textView1.setText(bookingHistoryList.get(i+1).toString());
tableRow1.addView(textView1);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TableRow tableRow2 = new TableRow(getActivity());
TextView textView2 = new TextView(getActivity());
textView2.setText(bookingHistoryList.get(i+2).toString());
tableRow2.addView(textView2);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TableRow tableRow3 = new TableRow(getActivity());
TextView textView3 = new TextView(getActivity());
textView2.setText(bookingHistoryList.get(i+3).toString());
tableRow2.addView(textView3);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TableRow tableRow4 = new TableRow(getActivity());
TextView textView4 = new TextView(getActivity());
textView2.setText(bookingHistoryList.get(i+4).toString());
tableRow2.addView(textView4);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
i += 4;
}
return view;
}
Here's the code to get from database. Its return value is not empty.
public ArrayList getBookingHistory(String email) {
ArrayList bookingHistoryList = new ArrayList();
String e = email;
Cursor c = db.rawQuery("select * from bookingDetails where email = '"+ email + "'", null);
int cou = c.getCount();
c.moveToFirst();
do {
bookingHistoryList.add(c.getString(c.getColumnIndex("date")));
bookingHistoryList.add(c.getString(c.getColumnIndex("time")));
bookingHistoryList.add(c.getString(c.getColumnIndex("fromLocation")));
bookingHistoryList.add(c.getString(c.getColumnIndex("destination")));
bookingHistoryList.add(c.getString(c.getColumnIndex("vehicle")));
} while (c.moveToNext());
return bookingHistoryList;
}
Stacktrace:
java.lang.NullPointerException
at com.prematixsofs.taxiapp.PreferencesFragment.onCreateView(PreferencesFragment.java:75)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:45)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.looper.loop.PreferencesFragment">
<LinearLayout
android:id="#+id/fragmentPreferencesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/hBookingHistoryTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
</LinearLayout>
</FrameLayout>
Two things :
One:
Your TableLayout id is hBookingHistoryTable. But in code, you are using a different id.
tableLayout = (TableLayout)view.findViewById(R.id.hTableLayout);
Change it to,
tableLayout = (TableLayout)view.findViewById(R.id.hBookingHistoryTable);
Two:
You are creating TableRows but not adding them to TableLayout. Add this line to all TableRows,
tableLayout.addView(<table_row_name>, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Correct steps for dynamically add rows to a TableView :
TableLayout table = (TableLayout) findViewById(R.id.main_table);
TableRow row = new TableRow(this);
TextView item = new TextView(this);
item.setId(200+count);
item.setText(date);
row.addView(item);
table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
I tried creating a scroll view in my relative layout to make my table layout scrollable but i keep getting this error:
02-02 19:29:10.116: E/AndroidRuntime(9400):
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.com.classmanagertest/test.com.classmanagertest.StudentsMasterList}: java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.
This is my Table Layout that display's fields in a database:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.students_masterlist);
db=openOrCreateDatabase("ClassManager",MODE_WORLD_WRITEABLE, null);
Cursor c=db.rawQuery("SELECT StudPic, StudentID, LastName FROM MasterStudents", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView, textView1;
ImageView StudImageView;
RelativeLayout rl=(RelativeLayout)findViewById(R.id.layout);
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
rl.addView(sv);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
StudImageView = new ImageView(getApplicationContext());
StudImageView.setPadding(20, 20, 20, 20);
StudImage=c.getBlob(c.getColumnIndex("StudPic"));
Bitmap b1= BitmapFactory.decodeByteArray(StudImage, 0, StudImage.length);
StudImageView.setImageBitmap(b1);
tableRow.addView(StudImageView);
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("StudentID")));
textView1.setPadding(20, 20, 20, 20);
textView1.setTextColor(getResources().getColor(R.color.blueactionbar));
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView1.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView1);
textView = new TextView(getApplicationContext());
textView.setText(c.getString(c.getColumnIndex("LastName")));
textView.setPadding(20, 20, 20, 20);
textView.setTextColor(getResources().getColor(R.color.blueactionbar));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
setContentView(tableLayout);
db.close();
}
This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_gravity="center_horizontal"
android:scrollbars="vertical|horizontal"
android:id="#+id/layout">
</RelativeLayout>
How do I fix this error?
Any help is appreciated! Thank you so much!
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.students_masterlist);
db=openOrCreateDatabase("ClassManager",MODE_WORLD_WRITEABLE, null);
Cursor c=db.rawQuery("SELECT StudPic, StudentID, LastName FROM MasterStudents", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView, textView1;
ImageView StudImageView;
RelativeLayout rl=(RelativeLayout)findViewById(R.id.layout);
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
rl.addView(sv);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
StudImageView = new ImageView(getApplicationContext());
StudImageView.setPadding(20, 20, 20, 20);
StudImage=c.getBlob(c.getColumnIndex("StudPic"));
Bitmap b1= BitmapFactory.decodeByteArray(StudImage, 0, StudImage.length);
StudImageView.setImageBitmap(b1);
tableRow.addView(StudImageView);
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("StudentID")));
textView1.setPadding(20, 20, 20, 20);
textView1.setTextColor(getResources().getColor(R.color.blueactionbar));
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView1.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView1);
textView = new TextView(getApplicationContext());
textView.setText(c.getString(c.getColumnIndex("LastName")));
textView.setPadding(20, 20, 20, 20);
textView.setTextColor(getResources().getColor(R.color.blueactionbar));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
db.close();
rl.requestLayout();
}
The First setContentView is called with the parenet relative layout which contains the table layout that has your rows.Therefore you are trying to setContentView to a view which is contained in the parent that you have already setContentView to ie.
rl contains sv contains tableLayout.
but you are setting the activity view first to rl then to tableLayout.
Instead you should set the content view to rl.Then fill your tableLayout like you did and refresh the layout.ie call requestLayout() on the parent view.
Hi to all,
i have written something like this in the xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/tl_splash_screen" >
</TableLayout>
</LinearLayout>
and in java file
TableLayout tl_splash_screen;
int int_scr_wd;
TableRow tr_test;
TextView txt_test;
TableRow tr_test1;
TextView txt_test1;
TableRow tr_test2;
TextView txt_test2;
TableRow tr_test3;
TextView txt_test3;
TableRow tr_test4;
TextView txt_test4;
TableRow tr_test5;
TextView txt_test5;
int int_tb_bk_col;
int int_black;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.x_splash_screen);
try
{
txt_test=new TextView(this);
txt_test1=new TextView(this);
txt_test2=new TextView(this);
txt_test3=new TextView(this);
txt_test4=new TextView(this);
txt_test5=new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(int_scr_wd+300, LayoutParams.FILL_PARENT);
tr_test = (TableRow) new TableRow(this);
tr_test1 = (TableRow) new TableRow(this);
tr_test2 = (TableRow) new TableRow(this);
tr_test3 = (TableRow) new TableRow(this);
tr_test4 = (TableRow) new TableRow(this);
tr_test5 = (TableRow) new TableRow(this);
tl_splash_screen = (TableLayout)findViewById(R.id.tl_splash_screen);
int_tb_bk_col = Color.rgb(211,211,211);
int_black = Color.rgb(0,0,0);
tl_splash_screen.setBackgroundColor(int_tb_bk_col);
txt_test.setTextColor(int_black);
txt_test1.setTextColor(int_black);
txt_test2.setTextColor(int_black);
txt_test3.setTextColor(int_black);
txt_test4.setTextColor(int_black);
txt_test5.setTextColor(int_black);
Display display = getWindowManager().getDefaultDisplay();
int_scr_wd= display.getWidth();
int as = display.getHeight();
Log.i("", String.valueOf(int_scr_wd));
Log.i("", String.valueOf(as));
txt_test1.setHeight(120);
txt_test2.setHeight(120);
txt_test3.setHeight(120);
txt_test4.setHeight(120);
txt_test5.setHeight(120);
txt_test.setText("TextViews");
txt_test1.setText("- TextView 1");
txt_test2.setText("- TextView 2");
txt_test3.setText("- TextView 3");
txt_test4.setText("- TextView 4");
txt_test5.setText("- TextView 5");
tr_test.addView(txt_test);
tr_test1.addView(txt_test1);
tr_test2.addView(txt_test2);
tr_test3.addView(txt_test3);
tr_test4.addView(txt_test4);
tr_test5.addView(txt_test5);
tl_splash_screen.addView(tr_test,new TableLayout.LayoutParams(layoutParams));
tl_splash_screen.addView(tr_test1,new TableLayout.LayoutParams(layoutParams));
tl_splash_screen.addView(tr_test2,new TableLayout.LayoutParams(layoutParams));
tl_splash_screen.addView(tr_test3,new TableLayout.LayoutParams(layoutParams));
tl_splash_screen.addView(tr_test4,new TableLayout.LayoutParams(layoutParams));
tl_splash_screen.addView(tr_test5,new TableLayout.LayoutParams(layoutParams));
}
catch(Exception ex)
{
Log.i("caught error","caught while loading main page");
}
}
Just to demonstrate i have posted this code. You all can see that i have mentioned textview 5 in which "textview - 5" text is written which is not visible on the screen because i have not taken tablelayout under scrollable view.
all i want to ask that why table layout is covering some area of the screen even though i have defined it fill_parent.
Please suggest something.
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="false" />
Add this to your manifest file.