I have a relative layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/ivLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/b2"
android:text="Button" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/ivLogo"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
which contains an imageview, textview, and two buttons, and what I want is to make a list of a info to be filled in this layout auto .. umm, like making in the java file "Heart.png", heart, www.heart.com, 69552874 .. then when I start the app the info I typed in the java class gets in the activity .. the "heart.png" gets in the ivLogo imageview, and the www.heart.com gets into b1 button, like this ..
I can achieve what i want using the xml but it will take a long of time and it'll be very slow because ill be many imageviews .. I want to make a list of things with their logo and name and num. and website , like can the java make a new relative layout for every info ..
like if i write
"Heart.png", heart1, www.heart.com, 69552874
"Heart.png", heart2, www.heart.com, 69552874
"Heart.png", heart3, www.heart.com, 69552874
I see in the app 3 relative layouts for each heart 1,2, and 3 ..
I want a method to do that not using the xml for each one make a new relative layout
isn't there something like makenewLayout etc ... ?
sorry for taking long, a two words hint will be nice to me <3
Yes, you can do this in code.
RelativeLayout layout = new RelativeLayout(this);
ImageView iv = new ImageView(this);
iv.setId(ivid);
....
....
TextView tv = new TextView(this);
tv.setId(tvid);
tv.setText("My text");
...
layout.addView(iv);
layout.addView(tv);
setContentView(layout);
Put this in a function, and call the function for various images, textviews, etc.
Related
I have something like "There are 200€" but I want to style the value (ex. textColor Red)
Is there any way of having two textViews to seem like an entire one?
A textView inside a textView or a textViewContainer or something.
I can achieve it on the run like this answer: Change text color of one word in a TextView
But I want to do from the layout file. Any chance?
Thanks
EDIT
<TextView
android:id="#+id/lastIncome"
android:text="Tu último ingreso fueron"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_font_size"/>
<TextView
android:id="#+id/lastIncomeValue"
android:text="200€"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_value_font_size"
android:textColor="#color/greensea"
android:layout_gravity="right"/>
You can achieve this with a horizontal LinearLayout. The LinearLayout is the container for the two side by side TextViews. This layout can be placed in any other container (LinearLayout, RelativeLayout, etc.) in your XML.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There are " />
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200€"
android:textColor="#ff0000"/>
</LinearLayout>
You can use a easy approach to do this within one textview using SpanableString Builder Click here to get this approach
i want to add a button on right of a textview and if textview grows,button would be still there.i used FrameLayout or RelativeLayout but they didn't work,please help me.
a layout like this image http://www.8pic.ir/images/97364937705465965477.png
Try with LinearLayout by giving orientation as horizontal.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="your text"
/>
<Button
android:id="#+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="button" />
</LinearLayout>
It is not possible directly and it is not good practice also for Android development. Though if it is your requirement, then you can count lines and number of character in last line at run time and decide the position of your button by using either frame or relative layout.
I'm creating a list view that I want to scroll smoothly.
I am currently designing the row in my XML file, and am wondering which layout view to choose. I want a row layout similar to this:
Mine will have an ImageView on the left, then two rows of TextView's on top of each other. (The orb on that picture isn't needed)
So what is the most efficient (when it comes to drawing) layout to use? Options I'm considering are TableLayout, RelativeLayout and LinearLayout (horizontal).
Extra info:
My list adapter is already very efficient and uses the viewholder pattern as well as pre-computation of TextView textand other optimisations to get maximum efficiency there. This question was specifically about the layout of the list row, though your help is appreciated!
I suggest you to use a RelativeLayout so you can choose where to place every single component. Basically, on the left side the imageview, then 2 textview one above the other. Optionally, the image on the right side.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:padding="3dip" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/icon" />
<TextView
android:id="#+id/first_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/image"
android:layout_toRightOf="#+id/image"
android:textSize="30dp"
android:textColor="#ffffff"
android:text="Medium Text" />
<TextView
android:id="#+id/second_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/first_txt"
android:layout_below="#+id/first_txt"
android:textSize="20dp"
android:textColor="#0481ab"
android:text="Small Text"/>
<ImageView
android:id="#+id/right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/icon" />
</RelativeLayout>
You can try to:
Use a Spannable for showing all text as markup in one TextView.
The image(?) on right can be set as a right CompoundDrawable for the above TextView.
Set android:drawingCacheQuality="low" if you don't have fancy gradients in background.
Don't do something heavy in Adapter's getView(), if you did Override it.
I have two text views(TV_1,TV_2).I want to set the TV_2 is immediate right of the TV_1.
It means
suppose TV1 occupy the one entire line in the screen then TV2 will set below the TV1.
Suppose Tv1 occupy the one and half line then tv2 will occupy the rest of the space. How to implement this? i tried but i am not getting. It is similar to one paragraph.
<RelativeLayout
android:id="#+id/xrays_dicom_alert_TnC"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="20dp">
<CheckBox
android:id="#+id/xrays_dicom_alert_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/xrays_dicom_alert_TnC_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/xrays_dicom_alert_cb"
android:text="I have read I agree to the following"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/xrays_dicom_alert_statement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/xrays_dicom_alert_cb"
android:layout_below="#id/xrays_dicom_alert_TnC_text"
android:layout_alignRight="#id/xrays_dicom_alert_TnC_text"
android:text="Statement"
android:textColor="#color/black"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Thats impossible act in Android.
A View occupies a rectangular area on the screen.
http://www.androidadb.com/class/android/view/View.java.html
Or you have to do something like this
To make clickable some text in TextView one can go with Spannable concept.
http://developer.android.com/reference/android/text/Spannable.html
use URLSpan http://www.androidadb.com/class/ur/URLSpan.html
I think it's not possible in the pattern what you have asked.
I am trying to build a UI, where in I have a linear layout which has been defined in the XML.
As per the user's input, I need to add some TextViews to this linear layout. I am able to do this.
My actual problem is, when I have more text views, they are stacked next to each other and some of text views text are hidden or stretched vertically as shown in the image below.
I would like to use the whole width of the linear layout and if the text view can not fit in this row, it should be put in a new row or below the first text view.. I would like the display to be as below.
Following is my Linear layout configuration in XML:
<RelativeLayout
android:id="#+id/RL1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingTop="5dip" >
<TextView
android:id="#+id/text1"
android:layout_width="85dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left"
android:text="Lable 1"
android:textColor="#999999" />
<LinearLayout
android:id="#+id/LL1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/button1"
android:layout_toRightOf="#+id/text1"
android:gravity="left|center_vertical"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="3dip" >
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginTop="2dip"
android:background="#drawable/plus"
android:clickable="true"
android:focusable="true"
android:paddingTop="5dip" />
</RelativeLayout>
Can any one please help me in how to realign the same in java code.
I would Like to suggest you about how you can provide equal sizes to all your views in Linear Layout,you can do that by using weight property in the XML file i.e., android:weight for that particular View and when you use this property you should give width=0dp or 0dip.I think this will solve your problem easily.
Please I suggest you first you take full Knowledge of how to use this property in the following Link:-Weight property with an example
Please see:
How to wrap Image buttons in a horizontal linear layout?
How can I do something like a FlowLayout in Android?
You also might search github for FlowLayout.java.
An alternative approach is given in:
Android - multi-line linear layout
In addition, there's a class that adds images into a TextView:
https://stackoverflow.com/a/21250752/755804
which is not the same as wrapping views in the general case, but sometimes may do the job.
i'm korean.
so i don't speak English well, but i'll help you.
first, Create 'item.xml' with four text boxes.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/set_checkbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text2"
android:text="0"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text3"
android:text="0"
android:layout_weight="4"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text4"
android:text="0"
android:layout_weight="4"
android:gravity="center"/>
</LinearLayout>
second, Create 'main.xml' where 'item.xml' will be dynamically generated.
'orientation' helps to create one line at a time.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Finally, You can create four TextView per line using the code below.
LinearLayout layout = (LinearLayout)findViewById(R.id.mainxml);
LinearLayout item = (LinearLayout)layout.inflate(this.getContext(), R.layout.itemxml, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
item.setLayoutParams(params);
CheckBox cb = item.findViewById(R.id.set_checkbox);
TextView text2 = item.findViewById(R.id.text2);
TextView text3 = item.findViewById(R.id.text3);
TextView text4 = item.findViewById(R.id.text4);
cb.setChecked(true);
text2.setText("text2");
text3.setText("text3");
text4.setText("text3");
layout.addView(item);
The 10th loop is shown in the following picture.
enter image description here