how to scroll tableview in android - android

I have an tableview which i want to scroll, because the data is not shown complete.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1,2,3,4"
android:id="#+id/maintable" >
</TableLayout>
this is my tablelayout, and if i wrap it in an <ScrollView> </Scrollview> the Application crashs if i open the activity. How to do that?

You should really put the code you tried that crashed your application and the reason for your crash. There's no valuable information in your original post.
Ehhh... did you try something as simple as this? Example .xml that I use a few places:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="#+id/score_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="#+id/header"/>
</TableLayout>
</ScrollView>
Works absolutely fine for me. You don't have to include the TableRow if you have no use for it, obviously.

Create your rows dynamically
Here I am putting a small example,
main.xml
<Button android:id="#+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add row"></Button>
<ScrollView android:id="#+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableLayout android:id="#+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0">
<TableRow android:id="#+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="textfield 1-1"></TextView>
<CheckBox android:id="#+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</TableRow>
</TableLayout>
</ScrollView>
Activity is
public class tablelayout extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//initialize a button and a counter
Button btn;
int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setup the layout
setContentView(R.layout.main);
// add a click-listener on the button
btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(this);
}
// run when the button is clicked
public void onClick(View view) {
// get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
// create a new TableRow
TableRow row = new TableRow(this);
// count the counter up by one
counter++;
// create a new TextView
TextView t = new TextView(this);
// set the text to "text xx"
t.setText("text " + counter);
// create a CheckBox
CheckBox c = new CheckBox(this);
// add the TextView and the CheckBox to the new TableRow
row.addView(t);
row.addView(c);
// add the TableRow to the TableLayout
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
You can create your table row, when in your code require. I assumed it is on button click.
Hope this will help.
And here one more example.

Related

How to let click event pass to container in android?

I have a table of FrameLayout where each frame contains either an ImageView or a TextView. Regardless of content in the frame I want the onClick event to be detected by the OnClickListener set on the frame.
How can I achieve this?
This is some of my layout, I got a total of 5 rows (Here's only shown 1).
As described above I have 5 FrameLayouts in each row, each containing a TextView. I am not able to put my OnClickListener on the TextView since this may be changed to an ImageView at runtime. Therefore i want the OnClickListener on the FrameLayout.
It seems that the content of the FrameLayout is preventing it from detecting the click event.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/gateContainer"
android:stretchColumns="*">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/door1"
android:clickable="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/door2" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView5"
android:layout_gravity="center" />
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/door3" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView4"
android:layout_gravity="center"/>
</FrameLayout>
</TableLayout>
Here is an example of how i set the OnClickListeners:
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v){
// do something
}
};
TableLayout tableLayout = (TableLayout) findViewById(R.id.gateContainer);
// Go through all TableRows
for (int i = 0; i < tableLayout.getChildCount(); i++){
TableRow tableRow = (TableRow) tableLayout.getChildAt(i);
// Set listener for each FrameView
for (int j = 0; j < tableRow.getChildCount(); j++){
FrameLayout frame = (FrameLayout) tableRow.getChildAt(j);
frame.setOnClickListener(clickListener);
}
}
You could also set android:clickable="false" to the TextView/ImageView layouts and android:clickable="true" on the background, that way the background view always catches the clicks.
Additionally, the following answer might be helpful for people considering this issue:
SO: Android: How to propagate click event to LinearLayout childs and change their drawable
Just implement, OnTouchListener over ImageView and TextView components and duck them, in the sense pass them.
<your_view>.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return true;
}
});
this will make sure, your container handles that.
You can define the click event on your TextView or ImageView as below:
<yourView>.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your logic here.
}
});

Android imageview not displaying in TableRow

I am trying to display a simple ImageView in a TableRow but for some reason it won't display. If I add another control to another row the imageView does display, so it seems like it is just not forcing the size correctly. My xml is as follows:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#4B088A"
android:id="#+id/ImageTable">
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/ImageTableRow"/>
</TableLayout>
The code that I am using to add the ImageView is:
TableLayout tableLayout = (TableLayout) findViewById(R.id.ImageTable);
TableRow tr = new TableRow(this);
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT ,TableRow.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
m_imageView = new MyImageView(getApplicationContext());
m_imageView.setBackgroundColor(Color.GRAY);
m_imageView.setImageBitmap(charty);
tr.addView(m_imageView);
tableLayout.addView(tr);
try this code
write your xml "activity_main" as
<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" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_search" />
</TableRow>
</TableLayout>
</LinearLayout>
ic_action_search is your image in drawable folder and write main activity as
public class MainActivity extends Activity
{
ImageView image;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Creating Views using both XML and code - elements not showing

I am making a simple soundboard app - buttons should appear in two columns, pressing a button plays a sound. I made it and had all the buttons defined using XML and it worked fine, but since the app was already attaching a listener to play a sound using a loop, I decided to define the buttons using only code.
So the code has no errors and the app launches but no buttons appear. There is a similar problem here: Content not showing in dynamically created android TableLayout although if I understand it correctly, the proposed solution (ensure Im using TableLayout.LayoutParams) is already applied here.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_table2);
int[] sounds = { ... };
String[] labels = { ... };
declareButtons(sounds, labels);
}
private void declareButtons(int[] sounds, String[] labels)
{
TableLayout tbl = (TableLayout) this.findViewById(R.id.tblMain);
TableRow row = null;
Button cell;
for (int i=0; i<sounds.length; i++)
{
cell = new Button(this);
cell.setText(labels[i]);
cell.setOnClickListener(this.getStartListener(sounds[i]));
cell.setBackgroundDrawable(getResources().getDrawable(R.drawable.soundbutton));
cell.setTextColor(color.white);
TableLayout.LayoutParams cellParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, (float) 1);
cellParams.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
cellParams.topMargin = cellParams.leftMargin;
if (i%2==0) // left column
{
row = new TableRow(this);
cell.setLayoutParams(cellParams);
row.addView(cell);
}
else // right column
{
cellParams.rightMargin = cellParams.leftMargin;
cell.setLayoutParams(cellParams);
row.addView(cell);
row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
tbl.addView(row);
}
}
if (sounds.length%2 > 0) // handle uneven amount of buttons
tbl.addView(row);
}
And here is the layout:
<?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"
android:background="#drawable/leonbg2"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/tblMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</LinearLayout>
Here is how table rows were defined previously, which worked fine:
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonaw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="#drawable/soundbutton"
android:text="A what?"
android:textColor="#android:color/white" />
<Button
android:id="#+id/buttona"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="#drawable/soundbutton"
android:text="Argh!"
android:textColor="#android:color/white" />
</TableRow>
Well, I managed to fix it myself by getting rid of TableLayout and going for a bunch of LinearLayouts (one for each row). The code is more or less the same with the addition of row.setOrientation(LinearLayout.HORIZONTAL); before cell.setLayoutParams(cellParams); in the left column part of the if statement. The moral of the story is; if you think you need to use TableLayout, you probably dont. Peace.

Add different TextViews to ScrollView

I Have activity that get some data from the internet, and shows it to the screen.
I'm using scroll view cause it's long text, I also want different text style for a different data,so I use few textViews with a different style and to show it on the Activity screen,
my problem is that scroll view can handle only one view, so how can I use scrolling to show different style of Text view, I tried to add LinearLayout to the scrollView and add all the textViews dynamically in code to this LinearLayout ,but I'm getting exception - scroll view can host only one direct child.
The code below:
/** this is the function, which called from the onClick method.
wanted data object contains 2 strings title message and the message itself.
When debug the code i can see that there's two String values in each loop.
but i cant add the linearLayout to my scrollView - exception ScrollView can host only one direct child */
private void showResult(ArrayList<WantedData> result) {
// TODO Auto-generated method stub
TextView title;
TextView data;
scrollLayout = (LinearLayout) findViewById(R.id.LlScrollView);
for (WantedData curr : result) {
if (curr.getTitle() == null) {
break;
}
title = new TextView(this);
title.setText(curr.getTitle());
scrollLayout.addView(title, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
data = new TextView(this);
data.setText(curr.getData());
scrollLayout.addView(data, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
scroll.addView(scrollLayout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//at the onCreate method - scroll = (ScrollView) findViewById(R.id.SvShowTextFromServer);
}
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"
android:orientation="vertical" >
<include
android:id="#+id/layout_reffernce"
layout="#layout/explore" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter City" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/EtCity"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:orientation="vertical" >
<requestFocus />
</EditText>
<Button
android:id="#+id/bSearchCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter State" />
<EditText
android:id="#+id/EtState"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
<ScrollView
android:id="#+id/SvShowTextFromServer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/LlScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/backround"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
The problem is double creating of container in ScrollView. You should not create it in activity, but take already defined from xml:
LinearLayout scrollContainer = (LinearLayout)findViewById(R.id.LlScrollView);
for (...) {
//create here some text
scrollLayout.addView(text);
}
If you have defined a LinearLayout in XML you don't have to create a new LinearLayout in your code but you have to retrieve the existing one in this way
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LlScrollView);
Otherwise you have to remove the LinearLayout in your XML and add all by code.

ChangeBackground of TableLayout with xml file when I click on a button on the same layout

I have created a linearlayout having 2 table layouts. In the first table I have a button. Now I want to display in the second table layout edittext and views that I have written in another XML file when I click on the button. Not the whole layout, only the second tablelayout will show the XML contents. Please help me with some sample code.
/>
<View
android:layout_width="wrap_content"
android:layout_height="1dip"
android:background="#000000"
/>
<View
android:layout_width="fill_parent"
android:layout_height="20dip"
android:background="#ffffff"
/>
<TableLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
>
<TableLayout
android:layout_marginRight="5dip"
android:stretchColumns="1"
android:layout_weight="0.5"
>
</TableLayout>
<TableLayout
android:id="#+id/change"
android:layout_width="fill_parent"
android:stretchColumns="1"
android:layout_weight="1">
</TableLayout>
</TableRow>
</TableLayout>
Now I want to set this XML file when I click on the button on the (TableLayout-android:id="#+id/change" )
Here I place two Table layout in single layout and assign id to them in first Table layout I place Button and second Table layout i place Edit text with View.In program I remove the second Table layout. When we click on button then I remove the First Table layout and add second Table layout to show the Edit text and view in same layout without changing the activity.
code:
public class tabletext extends Activity
{
LinearLayout linear;
TableLayout table1;
TableLayout table2;
Button bt;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linear=(LinearLayout)findViewById(R.id.linear1);
table1=(TableLayout)findViewById(R.id.table1);
table2=(TableLayout)findViewById(R.id.table2);
bt=(Button)findViewById(R.id.button1);
linear.removeView(table2);
bt.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
linear.removeView(table1);
linear.addView(table2);
}
});
}
}

Categories

Resources