My layout file has a structure like:
<TableRow>
<LinearLayout >
<LinearLayout >
<IconTextView />
</LinearLayout>
<TextView />
</LinearLayout>
<!-- The above linear layout is repeated 4 times in a table row -->
</TableRow>
None of the elements inside the table row have IDs for some reason. So how do i access the IconTextView and TextView inside every TableRow?
I access each child (outer LinearLayout) inside every TableRow with the help of getChildCount() and getChildAt() functions.
Now, I want a similar way to access different elements inside the child (for example IconTextView and TextView)
Set the id's and use them to identify the views, as you are creating your Views programatically you should be able to assign id's to them easily.
A solution would be to set the IconTextViews id's to 1 and the TextViews id's to 2, you can retreive them with ease by calling View.findViewById(int id) on the direct parent container - in this case each TableRow. So to retreive each IconTextView, call currentTableRow.findViewById(1).
Preferably you would use getChildCount() and getChildAt() to loop through your TableRows. Of course you will have duplicate id's in your view hierarchy, but if you call findViewById(id) relatively to your TableRow, it won't be a problem.
u can use in xml
android:id="#+id/diplay"
then in main java file u should write
public class Arithmatic2 extends ActionBarActivity {
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arithmatic2);
display=(TextView) findViewById(R.id.diplay);
}
Related
I want to show a table row with three columns (TextView) but i want to divide the table row in three equal parts.
This is easily possible by setting a layoutWeightSum in TableRow in XML and making layout_weight=1 for all theww TextView.
But i adding the table at run time through java and not by xml.
All i know is the TableRow.LayoutParameter do not provide any thing for weightSum. How can i do this?
TableRow.LayoutParams pmRow = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
You can achieve this by following code.
TableLayout layout = //...findViewbyid
layout.setStretchAllColumns(true);
And make width 0dp to all the children (views) of TableRow like below.
<TextView
android:id="#+id/txt_no1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="hi" />
Note
Above is XML layout design, but you can create run time code also. Its just for demonstration.
I've got you covered, mate!
myTableRow.setWeightSum(float weightSum);
use pmRow.span=int value
like pmRow.span=3;
Create a LinearLayout xml (horizontal) with the three required Items ( TextViews). To add the row item at runtime, inflate the above said layout, and set text values and add it.
I am trying to add rows to a TableLayout that I define in an XML file. The XML file contains a header row for the table.
I can add new rows quite well using info from various tutorials but the code required for setting up the layout for the new rows is a horrendous mess and it seems like a pain in the ass to maintain whenever the layout for the header row changes.
Is it possible to create new rows to a TableLayout while still defining the row layout in XML? For example define a template row in XML, obtain a handle to it in code and then clone the template whenever I need it.
Or is the right way to do this somehow completely different?
Your proposed approach will work fine and it more or less matches the common pattern used when populating ListView items.
Define a layout that contains a single row. Obtain a LayoutInflater by using LayoutInflater.from(myActivity). Use this inflater to create new rows using your layout like a template. Generally you will want to use the 3-argument form of LayoutInflater#inflate passing false for the third attachToRoot parameter.
Let's say you wanted to use a template layout with a label and a button in each item. It might look something like this: (Though yours would define your table rows instead.)
res/layout/item.xml:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/my_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Then at the point where you inflate:
// Inflate the layout and find the component views to configure
final View item = inflater.inflate(R.layout.item, parentView, false);
final TextView label = (TextView) item.findViewById(R.id.my_label);
final Button button = (Button) item.findViewById(R.id.my_button);
// Configure component views
label.setText(labelText);
button.setText(buttonText);
button.setOnClickListener(buttonClickListener);
// Add to parent
parentView.addView(item);
scrollview = (ScrollView)findViewById(R.id.detailedScrollView);
for (Quotation quotation : object.quotes){
TextView quote = new TextView(this);
quote.setText(quotation.getQuote());
quote.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
scrollview.addView(quote);
}
Let's say there are three quotes, then I want to have three textViews. However, the code above crashes my app. Any obvious mistakes? Here's the error I'm getting:
11-06 17:35:53.214: E/AndroidRuntime(1430): java.lang.IllegalStateException: ScrollView can host only one direct child
You can't add views directly inside a scrollview. A scrollview can only contain a single layout object. What you have to do is to add a linearlayout in your scrollview, then add the textview to the linearlayout
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container. Please more detail
With best regards,
Psycho
You need to add a "LinearLayout" (or "RelativeLayout") inside ScrollView.
Say you have the layout xml as follows:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
And now you want to add the 'TextView' programmatically, which is as follows:
LinearLayout linearLayout =(LinearLayout) this.findViewById(R.id.linearlayout1);
for (Quotation quotation : object.quotes){
TextView quote = new TextView(this);
quote.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
quote.setPadding(4, 0, 4, 0); //left,top,right,bottom
quote.setText(quotation.getQuote());
linearLayout.addView(quote);
}
Hey guys please help me out I am new to android application development
Here is the scenerio: This is my layout declaring xml file:
<LinearLayout xxx
<Textview aaa>
</TextView>
</LinearLayout>
//The below LinearLayout I need to display when it meets some condition in java class
i.e if(true) then display the following layout else dont. I can check this condition only after user provides some input.
<LinearLayout xxx
<Textview aaa>
To be displayed after the condition is checked
</TextView>
</LinearLayout>
//following layout is displayed with the first one.
<LinearLayout xxx
<Textview aaa>
</TextView>
</LinearLayout>
Any idea how to do it?
Take a few moments to read the android dev guide. It is worth the time: http://developer.android.com/guide/topics/ui/index.html
Basically, you want to use IDs to refer to the xml layout:
android:id="#+id/myxmlid"
and in your java file:
LinearLayout ll = findViewById(R.id.myxmlid);
if (yourCondition)
mainLayout.add(ll);
I'm assuming that you want to add a widgets to the current layout, rather than just change the text of the current TextView.
Also, this assumes that you want to add more than just a new TextView. If you only need that, you don't need to wrap it in a LinearLayout, which is used to add rows or columns of widgets.
You don't replace your entire layout programmatically just to change the text in one TextView. The way this kind of thing is handled in android, is to include a field in your Activity class for your textview, then instantiate it in your onCreate() method with findViewById() after you've called setContentView() to load the layout so that you can access that TextView's fields and methods.
First, you TextView needs an id in the layout xml.
<TextView android:id="#+id/sometext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Then in your Activity...
TextView mTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.sometext);
}
Somewhere else in the program...
public void myMethod(){
mTextView.setText("Text says this now");
}
Hopefully that gets the idea across. Good luck!
Thank Aleadam for suggesting me to read the link. Follwoing was my approach to get the output.
What I did was I assigned my LinearLayout Visibility to "GONE" (android:Visibility="GONE") when declarning the XML, and in the program after the condition is met, changed the visibility to "VISIBLE". (by using layout.setVisibility(View.VISIBLE))
is it possible/advisable to have a nested listview?
i.e. a listView that's contained within a row of another listview?
an example would be where my main list is displaying blog posts, and then in each row, you'd have another list view for the comments for each post (that would be collapsible)
I had the same problem today, so this is what I did to solve it:
I have a ListView, with a CustomAdapter, and on the getView of the customAdapter, I have something like this:
LinearLayout list = (LinearLayout) myView.findViewById(R.id.list_musics);
list.removeAllViews();
for (Music music : albums.get(position).musics) {
View line = li.inflate(R.layout.inside_row, null);
/* nested list's stuff */
list.addView(line);
}
So, resuming, It's not possible to nest to ListViews, but you can create a list inside a row using LinearLayout and populating it with code.
Is what you're looking for the ExpandableListView? Of course, that's limited to only two levels of listings (but that sounds like it would work for your needs).
This sound like what you're looking for? If you're not, or if this doesn't work, I would suggest having two list views: one of, say, blog posts, and the second of comments, and an action on a blog post item takes you to the second view, populated with the relevant comments.
you can do it like this :
inside the parent listview row xml layout add the following table layout
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table_show"
android:background="#beb4b4">
</TableLayout>
then you have to make a layout for the child list with name reply_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tv_reply_row"
android:textColor="#000"/>
</TableRow>
in your parent listview adapter getview method add the following code :
TableLayout replyContainer = (TableLayout)
// vi is your parent listview inflated view
vi.findViewById(R.id.table_show);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//child listview contents list
String [] replys = {"a","b","c","d"};
for (int i=0;i<replys.length;i++)
{
final View comments = inflater.inflate(R.layout.reply_row, null);
TextView reply_row = (TextView) comments.findViewById(R.id.tv_reply_row) ;
reply_row.setText(replys[i]);
//for changing your tablelayout parameters
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=3;
int topMargin=2;
int rightMargin=3;
int bottomMargin=2;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
comments.setLayoutParams(tableRowParams);
TableRow tr = (TableRow) comments;
replyContainer.addView(tr);
}
You'd better use one ListView, not nested. Nesting ListView is an inefficient way. Your ListView may not scroll smoothly and take up more memory.
You could organize your data structure to show nested data in one ListView. Or you can use this project PreOrderTreeAdapter.
It is convenient to show nested data in ListView or RecyclerView. It can be used to make ListView or RecyclerView collapsible, just change the way you provide your data than notify the adapter.