I have a tablelayout in Android like this:
1) Row == IMAGEVIEW | TEXTVIEW
2) Row == IMAGEVIEW | SPINNER
Now what I need to do is switch the TEXTVIEW/SPINNER. The one from row 2 goes to row 1 and the one from 1 goes to 2.
Would be awesome to have a little animation also. I've seen Viewswitcher and Viewflipper but This doesn't seem to be what I am looking for. Anyone got a good idea how to get this to work?
My layout (a piece of it) looks like this:
<TableRow>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/left_layout_controlls"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:stretchColumns="*"
android:id="#+id/top_controlls"
android:layout_height="wrap_content">
<TableRow>
<ImageView
android:id="#+id/fromCountry_img"
android:src="#drawable/tc_rt_from"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/fromCountry"
android:layout_marginTop="2px"
android:layout_marginLeft="2px"
android:background="#drawable/round_edges_main_controll"
android:layout_marginRight="2px"
android:layout_height="38px"
android:layout_width="160dip"/>
</TableRow>
</TableLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:stretchColumns="*"
android:layout_below="#id/top_controlls"
android:layout_height="wrap_content">
<TableRow>
<ImageView
android:src="#drawable/tc_rt_to"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="fill_parent"
android:layout_below="#id/fromCountry_img"
/>
<Spinner
android:id="#+id/toCountry"
android:layout_height="wrap_content"
android:layout_marginTop="2px"
android:layout_marginBottom="2px"
android:layout_marginRight="2px"
android:layout_width="160dip"
android:layout_weight="1"
android:drawSelectorOnTop="true"/>
</TableRow>
</TableLayout>
Update:
I've tried to switch the controlls like this, and the animation works, but when the animation ends, the controlls will jump back into their old position.
Any idea why?
LinearLayout layout1 = ((LinearLayout) DataHolder.activityHolder.findViewById(R.id.top_controll));
LinearLayout layout2 = ((LinearLayout) DataHolder.activityHolder.findViewById(R.id.bottom_controll));
DataHolder.activityHolder.findViewById(R.id.toCountry));
TranslateAnimation a = new TranslateAnimation(
Animation.ABSOLUTE,0,Animation.ABSOLUTE,0,
Animation.ABSOLUTE,0,Animation.ABSOLUTE, 40);
a.setDuration(1200);
TranslateAnimation b = new TranslateAnimation(
Animation.ABSOLUTE,0,Animation.ABSOLUTE,0,
Animation.ABSOLUTE,0,Animation.ABSOLUTE, -40);
b.setDuration(1200);
layout1.startAnimation(a);
layout2.startAnimation(b);
Simplest way to do this is to have Spinner with visibility GONE(which does not take place in the container) where you have the TextView and TextView where you have Spinner. Then just switch the visibilities. This solution is not practical one.
Honestly, I think using a TableLayout for this situation is impractical. Although the view you provided implies a TableLayout is the most practical, TableLayouts are designed to be fairly static. I do not know that there is a method to remove or animate TableRows within a TableLayout.
I would suggest using this following:
<LinearLayout android:orientation="vertical" ... >
<LinearLayout android:orientation="horizontal" ... >
CONTENTLAYOUTS
</LinearLayout>
... MORE LINEARLAYOUTS FOR MORE ROWS
</>
You could then manipulate the positions of these items.
For example, you could store the current positions as such:
LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
int position1 = layout1.getTop();
LinearLayout layout2 = (LinearLayout) findViewById(r.id.layout2);
int position2 = layout2.getTop();
AnimationSet animSet = new AnimationSet(...);
//define your animations here, or load them from resources
//to move each view to each of the two positions, changing the
//Y coordinate of the Views
Make sure to use an AnimationSet in order to have both animations be performed at the same time.
I hope this helps! Let me know if any of this was confusing, and I'll try to clear it up.
grmpf,
got it to work. thanks. the error was that I frogot this:
setFillAfter(true);
this will persist the change after the animation ends
Related
I want to change linearlayout that inside of ReletiveLayout.
But when I use settop it doesn't work!!!
and I can't find good tutorial
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:padding="5dp" >
<LinearLayout
android:id="#+id/remotebox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/remote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WWWWWWWWWWW"
android:textSize="30dp"
android:layout_gravity="left"
/>
<TextView
android:id="#+id/ver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TTTTTT"
android:layout_gravity="right"
/>
</LinearLayout>
i want change "#+id/remotebox" layout position and width and hight.
any suggestion about this problem or link of good tutorial?
sorry for poor english
try this
LinearLayout linearLayout= (LinearLayout)findViewById(R.id.remotebox);
linearLayout.getLayoutParams().width=50;//dimensions
linearLayout.requestLayout();
linearLayout.setTranslationY(800);//position
hope this will help you .
in my case, the following code works :
// step1. get the target view
LinearLayout thisView = (LinearLayout)rootView.findViewById(R.id.tab3_bind_device_page);
// step2. get the LayoutPrams instance.
// notice: in this case, the LinearLayout is wrapped by this RelativeLayout,
// so here we have to use the RelativeLayout.LayourParams
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
// change the margins
// in fact you should convert -25f from "px" to "dp", if possible.
lp.setMargins(0, -25f, 0, 0);
thisView.setLayoutParams(lp);
how to avoid overlapping of dynamically positioned views?
I have a RelativeLayout , and i am adding views dynamically(at runtime) at particular position(x,y coordinates) but the problem is the views are overlapping.
How to avoid this.
Thanks in advance.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rl_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/ll_mainBottom"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
javacode
ll_main = (RelativeLayout) findViewById(R.id.rl_main);
if (views[3].equals("textView")) {
TextView tv_new = new TextView(TenMinActivity.this);
// location
int x = Integer.parseInt(views[12]);
int y = Integer.parseInt(views[13]);
String bgColor = "#" + views[4];
String fgColor = "#" + Views[5];
tv_new.setBackgroundColor(Color.parseColor(bgColor)); // Bg Color
tv_new.setTextColor(Color.parseColor(fgColor)); // Text color
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = x;
params.topMargin = y;
ll_main.addView(tv_new, params);
}else if(views[3].equals("edittext")){
....
}
Give
android:paddingLeft=""
and to each element so that the ones to the left most of the screen will have bit of space. and the ones to the right,give
android:paddingBottom=""
First,check the first 2 elements that is the name text and your text field after that you can proceed to the rest.
I can guide you better,if you post your code
Check this,
<RelativeLayout 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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Name"
android:textSize="18sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10"
android:padding="10dp"
android:paddingl="10dp" >
<requestFocus />
</EditText>
</RelativeLayout>
You need to learn how layouts work, first try to make the same layout in xml. Then you will learn that this kind of layout can easily be made using LinearLayout.
You can use parent LinearLayout and add sublayouts to it. Now you say if you use LinearLayout all subviews are shown vertically. It shows vertically because you are adding them one by one. If you take a RelativeLayout and add your TextView and EditText to it and then add that RelativeLayout to your LinearLayout, you will get the desired result.
<LinearLayout>
<RelativeLayout>
<TextView />
<EditText />//use layout_margin or layout_toLeftOf for moving to to right
</RelativeLayout>
<RelativeLayout>
<TextView/>
<EditText/>
</RelativeLayout>
</LinearLayout>
Please use Layout params like layout_below layout_above toRightof and ToLeftof when you add a view to container relative layout
https://stackoverflow.com/a/5191159/1911784
you an use below code to add views in your layout
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView text2 = new TextView(context);
newParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newParams.addRule(RelativeLayout.ALIGN_BOTTOM, text1.getId());
text2.setLayoutParams(newParams);
layout.addView(text2);
where layout is your main layout.
you can use other params as well - like rightof, leftof etc
I am trying to make a day-calendar and for that I am using a tablelayout and I then want to define a custom row(layout xml) containing TextViews to be able to access data here is and example of my table layout:
<TableLayout
android:id="#+id/appointmentListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/unitNul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"/>
<CustomRow><\CustomRow> //this is where I imagined I would have to put my custom element
</LinearLayout>
</TableRow>
I would then proceed to put in appointments based on their time of the day in the correct rows (via. switch logic)
My questions are:
How do I add a Custom element to each tablerow?
Is this the best way to do so?
side note:
I have tried to make a custom view element, but this seems to be a bit too dificult for me to handle. (this is also an option if you know a good guide explaining this)
I have already tried: http://developer.android.com/guide/topics/ui/custom-components.html
create an Xml like this::
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/tablerows"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:stretchColumns="0,1" >
</TableLayout>
</LinearLayout>
</LinearLayout>
then in activity class dynamically add rows and the other views like this;;
TableRow row;
TextView t1, t2;
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) 1, getResources().getDisplayMetrics());
row = new TableRow(context);
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT));
t1 = new TextView(context);
t1.setTypeface(null, 1);
t1.setTextSize(15);
t1.setWidth(50 * dip);
t1.setPadding(20*dip, 0, 0, 0);
row.addView(t1);
then in ur table(here "myTable") add the row like this::
myTable.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I have been trying to get some data to show up in my dialog.
In the dialog I have a checkbox and two buttons that show up, so I know it is loading my layout file.
I am not certain what else to do, so why would the background on my dialog be completely transparent, and more importantly, why can't I see anything in the two views I have experimented with?
Here is my entire layout 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" >
<CheckBox
android:id="#+id/show_all_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Include books read" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/books_by_author_select_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Books" />
<Button
android:id="#+id/books_by_author_cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
<TableLayout android:id="#+id/books_by_author_list" 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>
</LinearLayout>
I have also tried this with a ListView but the same results. Directly below the two buttons the dialog is transparent.
The TableLayout has nine children when it finishes being initialized, and since it wasn't showing up, I then added the TableRow in the xml above, originally that block wasn't there.
this.mContext = context;
setContentView(R.layout.books_by_author);
final TableLayout view = (TableLayout) findViewById(R.id.books_by_author_list);
for(int position = 0; position < list.size(); position++) {
TableLayout table = (TableLayout) findViewById(R.id.books_by_author_list);
// create a new TableRow
TableRow row = new TableRow(mContext);
// create a new TextView
TextView t = new TextView(mContext);
// set the text to "text xx"
t.setText(list.get(position).mTitle);
// create a CheckBox
CheckBox c = new CheckBox(mContext);
// 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));
}
view.invalidate();
I have tried this with a ListView and just using an ArrayAdapter, and I have created a custom adapter extending ArrayAdapter and BaseAdapter.
I have also explicitly set the background of the ListView and TableLayout to be Color.YELLOW, and tried setting other colors, but nothing helps.
TableLayout doesn't work in many cases. Moreover, while running your app, TableLayout creates problem.
Always prefer using LinearLayout or FrameLayout.
Linearlayout can fit at any place where you are using TableLayout.
Solution: The problem was that the LinearLayout containing the buttons has fill_parent instead of wrap_content for the layout_height attribute.
I have the following main.xml file with a LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1" android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
I add an image to the LinearLayout via code at runtime like so
ImageView image = new ImageView(this);
image.setImageBitmap(bmp);
LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
ll.addView(image);
However, I want to add the ImageView between the 2 TextViews in my LinearLayout. I can't seem to find a way in the android docs to add a view before another view, or after. How can I do this?
NB I call
setContentView(R.layout.main);
Before I add the ImageView to the LinearLayout.
When adding a View to a ViewGroup, you can specify an index which sets the position of the view in the parent.
You have two views and so (counting from zero) you would want to add at the 1st position; just call ll.addView(image, 1); to have it placed in between the two TextViews.
The docs state you can use the index to insert it where you want. I see you are using the signature of the view only, did you try the signature with the index parameter?
public void addView(View child, int index)
I faced a similar problem. In my case I wanted to add a LinearLayout at last position of another LinearLayout. To accomplish it, I did:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// add others views to your linear layout
parentLayout.addView(layout, parentLayout.getChildCount());
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imagefield);
img.setImageResource(your_image_here);
and in the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<ImageView android:id="#+id/imagefield"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
Add an ImageView into the xml, and if its not being used, make it invisible (image.setVisibility(View.INVISIBLE)). It may not show anything anyway when no image is set.
To get position of view in a view group
val targetPosition = oldLL.indexOfChild(viewToAdd)
To add a view at a position in a view group
newLL.addView(viewToAdd,targetPosition)