I would like to know on how to randomly the button when activity start.
I have 5 button as bellow code:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom|center">
<Button
android:id="#+id/q1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#ffffff"
android:background="#drawable/ic_btn_q_normal" />
<Button
android:id="#+id/q2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#ffffff"
android:background="#drawable/ic_btn_q_normal" />
<Button
android:id="#+id/q3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#ffffff"
android:background="#drawable/ic_btn_q_normal" />
<Button
android:id="#+id/q4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#ffffff"
android:background="#drawable/ic_btn_q_normal" />
<Button
android:id="#+id/q5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#ffffff"
android:background="#drawable/ic_btn_q_normal" />
</LinearLayout>
So, how can I random it when activity start.
Let's say I start activity 3 times:
1st start activity, button will be list as horizontal like as q1/q2/q3/q4/q5
2st start activity, button will be list as horizontal like as q4/q2/q1/q3/q5
3st start activity, button will be list as horizontal like as q1/q5/q2/q4/q3
Like this.
Thanks you
LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. All children of a LinearLayout are stacked one after the other.
My guess is you want to display some buttons randomly on a layout. So I suggest you to work whether with RelativeLayout because you can move children easily or to build your Layout directly in the Java implementation (generate a Random or use Collection.shuffle(List).
Related
So, idea in this: I have two TextViews, first can expand whatever it wants, second always 5 chars (time). Problem is in that first TextView can easily push second out of the screen.
So, what I need is something like adjustable LinearLayout, or maybe some GridLayout that will move second TextView on some sort of second line if it doesn't fit parent.
For example you can watch at message bubbles in Viber and WhatsApp. Thanks for any advise.
Update 1
Here is XML that i have now (Only message part)
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/messageBox"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"
android:text='#{mess.message}'/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|end"
android:gravity="center_vertical|end"
android:paddingLeft="8dp"
android:textSize="12sp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:text='#{Utils.parseMillsToHoursAndMins(mess.date)}'/>
</LinearLayout>
Update 2
So I added layout_weight to first TextView, that helped with my first problem, but now I have new one. This two TextViews are in LinearLayout which is in another LinearLayout with another TextView. Parent LinearLayout have width set to wrap_content so if top TextView will be bigger than 2 TextViews it will cause child LinearLayout to be less than it's parent, and 2nd TextView (from that 2) wouldn't be in the end of parent. But when child LinearLayout is bigger, all appears to be OK. I know it's complicated, so this is XML
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:id="#+id/contentPanel"
app:bringToFront="#{true}"
android:orientation="vertical"
android:background="#{(mess.isMine?#drawable/chat_bubble_right:#drawable/chat_bubble_left)}">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='#{!mess.authorRole.equals("Client")?(mess.authorRole + " - " + mess.author):mess.author}'
android:textColor='#{mess.authorRole.equals("Lawyer")?#color/colorPrimary:mess.authorRole.equals("Admin")?#color/red:#color/green}'
android:textSize="12sp"
android:id="#+id/author"
android:fontFamily="sans-serif-medium"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/messageBox">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_weight="0.7"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"
android:text='#{mess.message}'/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:textSize="12sp"
android:gravity="bottom|end"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
app:checkFit="#{false}"
android:text='#{Utils.parseMillsToHoursAndMins(mess.date)}'/>
</LinearLayout>
</LinearLayout>
The new approach for achieving such behaviour is using ConstraintLayout with Flow. Here is an example of usage:
<androidx.constraintlayout.helper.widget.Flow
android:id="#+id/socialsButtonsFlow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
app:flow_horizontalGap="8dp"
app:flow_verticalGap="4dp"
app:flow_wrapMode="aligned"
app:flow_horizontalStyle="spread_inside"
app:constraint_referenced_ids="vkButton,twitterButton,facebookButton,youtubeButton,instagramButton,odnoklassnikiButton,tiktokButton"
app:layout_constraintEnd_toEndOf="#id/socialsLabel"
app:layout_constraintStart_toStartOf="#+id/socialsLabel"
app:layout_constraintTop_toBottomOf="#+id/socialsLabel" />
For small screens it looks like this:
I am not sure this will help you or not but:
Use: https://github.com/ApmeM/android-flowlayout
<org.apmem.tools.layouts.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</org.apmem.tools.layouts.FlowLayout>
Inside FlowLayout you can put your view's and it will auto move to next line if not fit.
if your textviews in linearlayout you can add weightSum method
I want to place text in a button on the extreme edges that is
Click on the left part of the button and Here on the right part of the button. How this can be achieved.
i have used the TAGandroid:text="Click Here", as the button is on the full screen i want these words to be at extreme edges
Use the below RelativeLayout. You can customize the values. Here you can make all of the RelativeLayout and TextViews act like a button by setting them OnClickListeners.
<RelativeLayout
android:id="#+id/mybutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:background="#3983B1" >
<TextView
android:id="#+id/leftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Click"
android:textColor="#ffffff" />
<TextView
android:id="#+id/rightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Here"
android:textColor="#ffffff" />
</RelativeLayout>
The layout looks like:
So in my layout, I've got three buttons on the same line, one left, center and right aligned. I also have TextViews below the buttons to serve as labels, but they too are aligned left, center and right respectively. I'd like them to instead be centered below the buttons and on the same line as one another, but I can't figure out how to do this without explicitly setting coordinates, which will then break on some phones. I tried setting various weights and layout options, but it just doesn't work how I'd like. Is there a way to do this in a RelativeLayout? Or maybe it's just not possible. Finally, I've got more three TextViews on the same line and one button on the bottom. I would like to align as shown bellow:
http://imgur.com/vYuqk
Thanks in advance.
You can't center one element below another with RelativeLayout. You could try a horizontal LinearLayout with three RelativeLayouts (one for each column). You could contain the whole thing in a vertical LinearLayout to get the bottom button.
TableLayout or GridLayout are possibilities as well.
As people suggested, there is no way to center a View below another View in RelativeLayout. You could maybe use this hack if you can confirm that the TextView width need not be more the Button width. The solution is: you make the TextView align its left and right edge to the Button above. Then set the gravity of the TextView to center so that the text inside TextView is centered.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="130dp"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_centerHorizontal="true"
android:text="Button2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_alignParentRight="true"
android:text="Button3" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/button1"
android:layout_alignRight="#id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="Txt1" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/button2"
android:layout_alignRight="#id/button2"
android:layout_below="#+id/button2"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="Txt2" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/button3"
android:layout_alignRight="#id/button3"
android:layout_below="#+id/button3"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="Txt3" />
</RelativeLayout>
Hi I have developed the UI like the screen below.Now I want to add the last row in the UI on last plus button click event.
I know how to add Button or EditText dynamically but I am not getting how can I add and delete row which is having multiple column dynamically.
Edit:- One Important thing about last row is each column is editable.That means I need the reference of each column for getting it's content.When I am adding row dynamically plus button should get placed in new row.Similarly if I remove row row plus button should get shifted to the upper row.
How can I do this.Any guideline or any approach will be appreciated.
This is my XML file.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/navi_bar" />
<Button
android:id="#+id/title_bar_btnBack"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:background="#drawable/back_button_image" />
<Button
android:id="#+id/title_bar_btnExport"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/title_bar_btnBack"
android:layout_marginRight="10dp"
android:background="#drawable/export_button_normal" />
<!-- Create PDF Part 2 -->
<ImageView
android:id="#+id/pdf_Upper_Image"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title_bar_btnBack"
android:background="#drawable/grid_bg_part1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pdf_Upper_Image"
android:layout_alignParentLeft="true"
android:text="Kassenbuch"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#3EC7F9"
android:textSize="23dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="#+id/textView1"
android:text="Name :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignTop="#+id/textView2"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/textView2"
android:text="Akshay"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<!-- Custom Pdf Part 2 -->
<ImageView
android:id="#+id/pdf_Middle_Image"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/pdf_Upper_Image"
android:background="#drawable/grid_bg_part2"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginLeft="2dp"
android:text="Einnahmen"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtName"
android:layout_alignTop="#+id/txtName"
android:layout_marginLeft="82dp"
android:layout_toRightOf="#+id/txtName"
android:text="Month :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignLeft="#+id/textView4"
android:layout_marginBottom="2dp"
android:text="Mand Nr. :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/txtMand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView5"
android:layout_alignTop="#+id/textView5"
android:layout_toRightOf="#+id/textView5"
android:text="Mand"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/txtMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtName"
android:layout_alignTop="#+id/textView4"
android:layout_toRightOf="#+id/textView5"
android:text="05"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp"
/>
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtYear"
android:layout_alignTop="#+id/txtMonth"
android:layout_toLeftOf="#+id/title_bar_btnExport"
android:text="Year :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView5"
android:layout_alignBottom="#+id/textView5"
android:layout_alignLeft="#+id/textView8"
android:text="Blatt :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/txtYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtMonth"
android:layout_alignLeft="#+id/title_bar_btnExport"
android:layout_alignTop="#+id/txtMonth"
android:text="2012"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/textView3"
android:text="Ausgabne"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" >
</TextView>
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/textView6"
android:text="Bestand"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_toLeftOf="#+id/txtMand"
android:text="Datum"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<TextView
android:id="#+id/textView13"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_toLeftOf="#+id/textView5"
android:text="Beleg Konto"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="8dp" />
<TextView
android:id="#+id/textView14"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView13"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginRight="03dp"
android:layout_toLeftOf="#+id/textView13"
android:text="Gegen Konto"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="08dp" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView14"
android:text="Anfangsbestand/Ubertrag"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="9dp" />
<TextView
android:id="#+id/textView11"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView13"
android:layout_alignTop="#+id/pdf_Middle_Image"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/textView12"
android:text="USt satz."
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="09dp" />
<TextView
android:id="#+id/txtBlatt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView9"
android:layout_alignBottom="#+id/textView9"
android:layout_toRightOf="#+id/textView9"
android:text="Blatt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView11"
android:layout_alignLeft="#+id/textView9"
android:text="Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<include android:id="#+id/firstRow"
layout="#layout/custom_pdf3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pdf_Middle_Image"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"/>
</RelativeLayout>
Thanks
Create a layout that represents a complete row,then you can use:
public View inflate (int resource, ViewGroup root) from LayoutInflater(a class in android.view package) to Inflate a new view hierarchy from the specified XML resource(for example your new row).
Now you can add this view(your row) dynamically to your main layout with a specific tag(like adding a button dynamically to the layout).
Create an XML layout file, which will represent a 'row in the list view.
Implement a custom Adapter, using base Adapter.
In the getView() method, Inflate the Xml layout, Reference the inner items (using outerLayout.findViewById(), set Listeners right there, and Finally return the view.
Write the implementation for your listener.
Simple enough? :-)
Try creating the whole layout at runtime. Create a single row containgin all ur columns and set visibility of the last one to gone. Change the visibility when the plus button is pressed.
Creating a row of type Relative layout will be good i think. When u want to remove a row Then you will have to count the Relative layouts in the main layout and then remove the index which u want to remove.
I think you will understand best through an example. Lets simplify things a little bit. Lets say your main layout is this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout" >
</TableLayout>
It's a simple TableLayout without any views yet (You could have pre-baked rows in it). Now you want to create TableRows wich you want to add dinamically, and handle it's subviews separately, right? You could create your whole TableRow in xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tablerow_textview"
android:text="Hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tablerow_button"
android:text="World" />
</TableRow>
Then the java part. Lets say you want to add 10 of those rows to your TableLayout, and handle each rows button and separately. You can do it like this (You must have access to a Context object):
// get the main TableLayout from xml
TableLayout layout = (TableLayout) findViewById(R.id.main_layout);
// get a LayoutInflater object. 'this' is the current Activity
LayoutInflater inflater = LayoutInflater.from(this);
for(int i=0; i<10; i++)
{
//Create an inflated instance of that layout
TableRow row = (TableRow) inflater.inflate(R.layout.table_row, null);
//Notice, that we call the findViewById() function on the previously
//inflated layout, which means, that we can only access its subviews
Button rowButton = (Button) row.findViewById(R.id.tablerow_button);
TextView rowText = (TextView) row.findViewById(R.id.tablerow_textview);
//Now that you have those Views, you can do whatever you want with them, (set
an onClickListener, change the text, etc)
//finally we need to add the row to the table
layout.addView(row);
}
It would also be a good idea if you wrapper created a class for your inflated layout, with its own getter-setter functions, and you could just instantiate it with the new operator, and all the inflation would be done in the background.
You need to work with custom views. Then each view can be a complex view and you can do what ever you wish, this task should not be easy.
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="#+id/textView1"
android:text="Name :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<com.sample.MyCustomView
android:id="#+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And then MyCustomView can be Linear Layout or what ever.
In above xml
you did all the things in Textview and adjust that
instead of using that you have to create a Tablelayout in your xml file.
onButtonClick event you have to create a new tablerow dynamically then add components by using tablerow.addview(componentsname) and finally add tablerow to tablelayout
Simple TableLayout
After trying lot of different things I solved my problem like below.I made certain changes in my xml.I used LinearLayout with ScrollView.
And I created one xml file which is having complete row.Now my task is to add that xml in the LinearLayout which I did in the following manner.I am calling this method on button click event at each time.
public void AddDynamicView() {
linearLayout = (LinearLayout) findViewById(R.id.show_pdfTableLayout);
layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.custom_pdf3, null);
linearLayout.addView(view);
list.add(view); // Here I have created list of View so that I can get the id of specific view.
}
I have tried this with TableLayout but I was having some issue with occupying whole row.That's why I had used LinearLayout.
Probably I didn't understand well!!!
You can do everything mentioned in the question using ListView and Adapter.
You can provide an array to hold fields, feed an adapter with the array and feed listView with adapter.
If you need a new Row just add an element to the array.
If you want to delete a Row, just remove it from array.
If you want to sort items, just sort the array.
If you want to select prev/next, just use listview selection method.
I was wondered you checked your answer that is not absolutely correct approach. It's true but if your target ui is complex, the page will need a lot of time to render and scroll than ListViews that is dynamic and optimized.
If you think my answer sounds good, Let me know to update it with some code.
I am using a TableLayout with TableRows as my main activity.
Inside the TableLayout is a Radio Group containing 2 Radio Buttons inside the activity (the Radio Group being inside a table row). I want to be able to align the rightmost radio button to the right edge screen, so the "gap" is between the left radio button and right button (instead of after the right radio button). i.e.
So instead of having
| (x) (x) gap |
I will have
|(x) gap (x)|
where (x) are the Radio Buttons and | are the edges of the screen
I can use gravity (center_horizontal) to put both the buttons in the middle (i.e. | gap (x)(x) gap|) however I can't seem to be able to split them the way I want as said before
All you need to evenly space an arbitrary number of buttons horizontally across the screen:
RadioGroup has to have
android:orientation="horizontal" &
android:layout_width="fill_parent"
Each radio button has to have
android:layout_weight="1", except
the rightmost button (to make it
line up on the right edge of the
screen)!
This took me hours to figure out.
Here is some example code, with a bonus of two text labels and the right and left edges of the screen, for a survey app.
<RadioGroup
android:id="#+id/radio_group"
android:orientation="horizontal"
android:layout_below="#id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
>
<RadioButton
android:id="#+id/strong_disagree_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
/>
<RadioButton
android:id="#+id/disagree_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/disagree"
/>
<RadioButton
android:id="#+id/neutral_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/neutral"
/>
<RadioButton
android:id="#+id/agree_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/agree"
/>
<RadioButton
android:id="#+id/strong_agree_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
/>
</RadioGroup>
<TextView
android:id="#+id/disagree_label"
android:text="#string/strongly_disagree_txt"
android:layout_below="#id/radio_group"
style="#style/TextAppearance"
android:visibility="gone"
/>
<TextView
android:id="#+id/agree_label"
android:text="#string/strongly_agree_txt"
android:layout_below="#id/radio_group"
android:layout_alignParentRight="true"
style="#style/TextAppearance"
android:layout_width="wrap_content"
android:visibility="gone"
/>
Found my solution. A combination of weights and gravity and removing the margins. The radio group can have layout_width="fill_parent". Each radio button should have layout_weight="1", but the layout_width for each radiobutton must still be specified to override the default of "37" which I guess is the default radio button image width.
<RadioGroup android:id="#+id/overall"
android:layout_width="fill_parent" android:layout_height="80dp"
android:gravity="center"
android:orientation="horizontal">
First button should have:
android:layout_weight="1"
android:layout_gravity="center|left"
Last button should have:
android:layout_gravity="center|right"
Note: all buttons have layout_gravioty set, but no layout_weight setting for the right most button!
Did you ever find a solution to this? My intermediate solution might help, but it's not the full picture for me. I've been able to do this by setting specific widths, but this does not allow the view to resize to fit the screen width:
<RelativeLayout
android:id="#+id/overall_layout"
android:layout_width="290dp" android:layout_height="wrap_content">
<RadioGroup android:id="#+id/overall"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton android:id="#+id/overall_1" android:tag="1"
android:button="#drawable/radio_1"
android:layout_width="50dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"></RadioButton>
<RadioButton android:id="#+id/overall_2" android:tag="2"
android:button="#drawable/radio_2"
android:layout_width="50dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"></RadioButton>
<RadioButton android:id="#+id/overall_3" android:tag="3"
android:button="#drawable/radio_3"
android:layout_width="50dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"></RadioButton>
<RadioButton android:id="#+id/overall_4" android:tag="4"
android:button="#drawable/radio_4"
android:layout_width="50dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"></RadioButton>
<RadioButton android:id="#+id/overall_5" android:tag="5"
android:button="#drawable/radio_5"
android:layout_width="50dp"
android:layout_height="wrap_content"></RadioButton>
</RadioGroup>
<TextView android:text="left" android:id="#+id/left"
android:layout_below="#id/overall"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="right" android:id="#+id/right"
android:layout_below="#id/overall"
android:layout_alignParentRight="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</RelativeLayout>
Note there is no margin on the last button.
I am trying to have 5 custom buttons in a radio group, with the 1st left-justified and the last right-justified. The buttons are exactly 50 pixels wide and I don't want any text associated with the buttons individually. The 2 texts below are left and right-justfied in the relative layout. Using "layout_gravity" has no effect and "layout_weight" adds padding to the right of each button, which causes the right-most button to no longer be justified.
Much hair-pulling on this.
If i understand you question right, you can also try next code inside RadioGroup:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:id="#+id/view1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />