how to show textview like this image?
my textview will look like this image
below is my code, it's not showing text centrally:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/imagelogo2"
android:orientation="horizontal" >
<ImageView
android:id="#+id/test_button_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="20dp"
android:src="#drawable/back" >
</ImageView>
<ImageView
android:id="#+id/test_button_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="5dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:src="#drawable/options1" />
</RelativeLayout>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="If your school is not in the list.Please check" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="back later or contact your district's Nutrition Services" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="department for more information" />
</LinearLayout>
The problem is all your 3 TextViews are different widths, so "center" is different for each of them.
Change them all to:
android:layout_width="match_parent"
You just need to replace
android:layout_width = "wrap_content"
with
android:layout_width="fill_parent"
for all the TextView. Try the layout below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/imagelogo2"
android:orientation="horizontal" >
<ImageView
android:id="#+id/test_button_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="20dp"
android:src="#drawable/back" >
</ImageView>
<ImageView
android:id="#+id/test_button_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="5dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:src="#drawable/options1" />
</RelativeLayout>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="If your school is not in the list.Please check" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="back later or contact your district's Nutrition Services" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="department for more information" />
Try this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="italic"
android:layout_margin="5dp"
android:text="If your school is not in the list.Please check"
/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="5dp"
android:textStyle="italic"
android:text="back later or contact your district's Nutrition Services"
/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="5dp"
android:textStyle="italic"
android:text="department for more information"
/>
Instead of using 3 textviews you should use one textview.
and the text that you want to show, had to convert it to HTML.
String code = "<p><b>First, </b><br/>" +
"Please press the <img src ='addbutton.png'> button beside the to insert a new event.</p>" +
"<p><b>Second,</b><br/>" +
"Please insert the details of the event.</p>"
"<p>The icon of the is show the level of the event.<br/>" +
"eg: <img src = 'tu1.png' > is easier to do.</p></td>";
message = (TextView) findViewById (R.id.message);
Spanned spanned = Html.fromHtml(code, this, null);
message.setText(spanned);
message.setTextSize(16);
and make textview gravity to center.
For Times New Roman Fonts you need to do this:
create one folder "fonts" in assets folder
copy times new roman.ttf file in "fonts" folder. (times fonts file
should be in small caps)
then add this class to your activity class:
public class Typefaces{
private final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public Typeface get(Context c, String name){
synchronized(cache){
if(!cache.containsKey(name)){
Typeface t = Typeface.createFromAsset(
c.getAssets(),
String.format("fonts/%s.ttf", name)
);
cache.put(name, t);
}
return cache.get(name);
}
}
}
then try this code:
Typefaces tf = new Typefaces();
tvMessage.setTypeface(tf.get(context, "timesnewroman"));
Hope it Helps!!
Related
As seen in picture below I need a layout where Textview containing contents 'Hello' and layout '2' are always visible and are between layout '1' and ']'
{1}Hello{2} ]
{1}Hello I..{2}]
If textview contents are too long they should ellipsize
Layout '2' shouldn't be right aligned to parent or ']', but be next to TextView
I have tried playing with wieghts and relative layout but do not seem to get a working solution.
Any ideas/pointers?
Here is slimdown version of my layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/simple_white_button"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/topLayout1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.85"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/photo"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="centerCrop"
android:src="#drawable/headshot_alt"
android:visibility="visible" />
<LinearLayout
android:id="#+id/myRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/photo" >
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:ellipsize="end"
android:singleLine="true"
android:text="New Jersey"
android:textColor="#color/grey_text"
android:textSize="16sp" />
<LinearLayout
android:id="#+id/weather"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:gravity="left" >
<!-- Weather -->
<ImageView
android:id="#+id/imgWeather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_notification_news"
android:visibility="visible" />
<TextView
android:id="#+id/txtWeather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="3dp"
android:gravity="center_vertical"
android:text="50/43"
android:textColor="#color/text_dark"
android:textSize="13sp"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/menu"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.15"
android:background="#drawable/simple_white_button"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/menu_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="#drawable/ic_quickaction_default" />
</LinearLayout>
</LinearLayout>
Where {1} would be the id = photo, ] is id=menu and id=myRow is the textview + weather layout i want in between photo and menu. I preferably do not want to set max width on my address text view, in worst case i want it ellipsize when weather touches the menu, But when address is small i want weather next to address and not close to menu
Hopefully its bit more clear now
In a relative layout, with fixed width for the textview, Layout'2' as layout_toRightOf the textview, something like this:
<TextView
android:id="#+id/firstText"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:text="Hello String"
android:lines="1"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/layoutTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/firstText"
android:background="#android:color/white"
android:text="Layout 2"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Provide the container layout as per your requirement. With a fill_parent width, you can adjust the "]" to be at the extreme right or xdp away after your Layout 2
Can you specify, precisely, what is required...i'l edit my answer
I've got some problem with positioning TextViews. Currently my layout looks like:
Text1: Text2
Text3: Text4
Ok, but when Text2 has 2 rows length or more I've got:
Text1: Text2:1
Text3: Text2:2/Text4
I don't know how to set up my layout to obtain view like:
Text1: Text2:1
Text2:2
Text3: Text4
Can somebody help me solve that problem?
XML code:
(...)
//Text1:
<TextView
android:id="#+id/tv_author_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_genre_label"
android:layout_toRightOf="#+id/img_Cover"
android:text="Author: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
//Text2:
<TextView
android:id="#+id/tv_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tv_author_label"
android:layout_alignBottom="#+id/tv_author_label"
android:layout_toRightOf="#+id/tv_author_label"
android:text="Anonymous Anonymous"
android:textAppearance="?android:attr/textAppearanceMedium" />
//Text3:
<TextView
android:id="#+id/tv_availability_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_author_label"
android:layout_toRightOf="#+id/img_Cover"
android:
android:textStyle="bold"
android:text="Availability: "
android:textAppearance="?android:attr/textAppearanceMedium" />
//Text4:
<TextView
android:id="#+id/tv_availability"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tv_availability_label"
android:layout_alignBottom="#+id/tv_availability_label"
android:layout_toRightOf="#+id/tv_availability_label"
android:layout_alignParentLeft="#+id/tv_availability_label"
android:layout_below="#+id/tv_author"
android:text="b/d"
android:textAppearance="?android:attr/textAppearanceMedium" />
(...)
you can reconstruct your code something like this,
<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"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dfsdjfkd dsfsdf sdfsdf sdfsdfsd fjlsdjf sdj; j; jlksdfjl sdjflkdsjflsdk jkldsjf lsjldkfj sdl" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
I hope it will be helpful !!
You can encapsulate the row of text views inside a linear layout with orientation - horiontal
I am new to Android and Have to Port a Windows Mobile Application to an Android Application.
Issue: I need GridView in Android similar to .net GridView which is able to scroll in both Horizontal and Vertical directions at the same time.
As there are many rows and many columns in my data. So, all the columns are not shown as screen width is small on mobile. Multiple rows are shown as vertical scroll is enabled. What i want is that this GridView is somehow scrollable in both directions at the same time. So that user could view data in tabular form.
I am using GridView in Android with the help of TableLayout and SimpleAdapter. Following is my code:
Main Activity Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- This Table Layout is header followed by the gridview -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" >
<TableRow android:layout_width="wrap_content" >
<TextView
android:id="#+id/schemeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Scheme"
android:textStyle="bold" />
<TextView
android:id="#+id/nameTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="#+id/productTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Product"
android:textStyle="bold" />
<TextView
android:id="#+id/channelTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Channel"
android:textStyle="bold" />
<TextView
android:id="#+id/typeTitle"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textStyle="bold" />
<TextView
android:id="#+id/customerSelectionTitle"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Customer Selection"
android:textStyle="bold" />
<TextView
android:id="#+id/brandTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Brand"
android:textStyle="bold" />
<TextView
android:id="#+id/wsTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="W/S"
android:textStyle="bold" />
<TextView
android:id="#+id/startDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Start Date"
android:textStyle="bold" />
<TextView
android:id="#+id/endDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="End Date"
android:textStyle="bold" />
<TextView
android:id="#+id/codeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Code"
android:textStyle="bold" />
<TextView
android:id="#+id/tsidTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="TSID"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<GridView
android:id="#+id/schemeGridView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:clickable="true"
android:numColumns="1"
android:columnWidth="900dp" >
</GridView>
</LinearLayout>
Grid Layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TableRow android:layout_width="wrap_content" >
<TextView
android:id="#+id/scheme"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/name"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/product"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/channel"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/type"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/customerSelection"
android:layout_width="150dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/brand"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/ws"
android:layout_width="50dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/startDate"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/endDate"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/code"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tsid"
android:layout_width="50dp"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
Source Code:
public class Schemes extends BaseMainActivity {
private String obID = "";
private String obName = "";
ArrayList<HashMap<String, Object>> dtSchemes = null;
GridView schemeGridView = null;
Activity thisAct = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schemes);
ProgressDialog d = ProgressDialog.show(this, "Inventory",
"Loading Inventory...");
OrderBookingDAL dl = new OrderBookingDAL();
obID = dl.getConfigValue("OBID");
obName = dl.getConfigValue("OBName");
dtSchemes = dl.getHHTSchemes(obID, obName, dtSchemes);
schemeGridView = (GridView) findViewById(R.id.schemeGridView);
SimpleAdapter sa = new SimpleAdapter(this, dtSchemes,
R.layout.schemes_grid, new String[] { "Scheme", "Name",
"Product", "Channel", "Type", "CustomerSelection",
"Brand", "[W/S Approved]", "[Start Date]",
"[End Date]", "Code", "TSID" }, new int[] {
R.id.scheme, R.id.name, R.id.product, R.id.channel,
R.id.type, R.id.customerSelection, R.id.brand, R.id.ws,
R.id.startDate, R.id.endDate, R.id.code, R.id.tsid });
schemeGridView.setAdapter(sa);
schemeGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d("Grid Item Click", "Clicked pos:"+position);
TextView txt = (TextView) view.findViewById(R.id.tsid);
if (txt != null && txt.getText() != null) {
String tsid = txt.getText().toString();
Intent intent = new Intent(thisAct,SchemeDetails.class);
intent.putExtra(TSID_PARAM, tsid);
thisAct.startActivity(intent);
}
}
});
d.dismiss();
}
}
I changed some code and finally able get tabular view.
Solution: I added HorizontalScrollView and now my LinearLayout is its child. Also I added android:stretchColumns="*" in TableLayout. Along with this i had to chnage android:layout_width and android:layout_height of different controls in following ways.
Updated code is below:
Main Activity Layout:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- This Table Layout is header followed by the gridview -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:stretchColumns="*" >
<TableRow android:layout_width="wrap_content" >
<TextView
android:id="#+id/schemeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Scheme"
android:textStyle="bold" />
<TextView
android:id="#+id/nameTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="#+id/productTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Product"
android:textStyle="bold" />
<TextView
android:id="#+id/channelTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Channel"
android:textStyle="bold" />
<TextView
android:id="#+id/typeTitle"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textStyle="bold" />
<TextView
android:id="#+id/customerSelectionTitle"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Customer Selection"
android:textStyle="bold" />
<TextView
android:id="#+id/brandTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Brand"
android:textStyle="bold" />
<TextView
android:id="#+id/wsTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="W/S"
android:textStyle="bold" />
<TextView
android:id="#+id/startDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Start Date"
android:textStyle="bold" />
<TextView
android:id="#+id/endDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="End Date"
android:textStyle="bold" />
<TextView
android:id="#+id/codeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Code"
android:textStyle="bold" />
<TextView
android:id="#+id/tsidTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="TSID"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<GridView
android:id="#+id/schemeGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:numColumns="1" >
</GridView>
</LinearLayout>
</HorizontalScrollView>
Grid Layout:
Only following tag is chnaged:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*">
Source Code:
No change in Java code.
I got idea from: How can I make my layout scroll both horizontally and vertically? and How to put a very long table layout inside the horizontal Scroll view in the CORRECT way?
The simplest solution is
Use a horizontal scroll view to contain the GridView
Then dynamically re-size the GridView width based on the number of columns that you set for the GridView
Lastly, multiply number of columns with the width of each columns
GridView gridView = (GridView) findViewById(R.id.grid_view);
int numberOfColumns = 3;
int sizeOfWidthPerColumn = 20;
gridView.setNumColumns(numberOfColumns);
ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
layoutParams.width = convertDpToPixels(numberOfColumns * sizeOfWidthPerColumn, this);
gridView.setLayoutParams(layoutParams);
Now you are able to scroll the GridView horizontally and vertically.
I am not sure, if it's possible to scroll in both direction at the same time, but here's a library that helps you to scroll a GridView horizontally.
https://github.com/jess-anders/two-way-gridview
Also you can have a look in here, How to make a 2-dimension image gallery with both horizontal and vertical scrolling?
When I declare 3 TextViews in RelativeLayout, 'textA', 'textB', and 'textC'. The textC should appear below textB. But when run, the textC displayed in improper position (at the top-left of screen).
I don't know can textC use android:layout_below to textB.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView
android:id="#+id/textA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:text="This is TextA : " />
<TextView
android:id="#+id/textB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/textA"
android:layout_alignBaseline="#id/textA"
android:text="this is textB" />
<TextView
android:id="#+id/textC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/textB"
android:text="This is TextC" />
</RelativeLayout>
You can also change
android:layout_below="#id/textA"
to textC
The complete Example would look:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView
android:id="#+id/textA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:text="This is TextA : " />
<TextView
android:id="#+id/textB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textA"
android:layout_alignBaseline="#+id/textA"
android:text="this is textB" />
<TextView
android:id="#+id/textC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textB"
android:layout_below="#+id/textA"
android:text="This is TextC" />
Just make below changes for the TextView3 :
<TextView
android:id="#+id/textC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textB"
android:layout_below="#+id/textA"
android:text="This is TextC" />
It cannot use vertical alignment to the elements that use alignBaseline to other elements. Then in this case in 'textB' change alignBaseline to be alignTop like in the code.
<TextView
android:id="#+id/textB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/textA"
android:layout_alignTop="#id/textA"
android:text="this is textB" />
Using this way it will support muti-line for textB.
I am getting a problem in putting imageview just after textview if textview becomes of two line.
I want to show this(Image just after text,text can be in one line,two line or three lines)
My imageview comes just after textview if text is short(one line).
But when text becomes longer(two lines) then image goes to next line like this
I know the reason of this problem but don't know how to solve?
After lot of googling, I found a good solution of my problem
TextView textView =new TextView(this);
SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's a smiley how are you " );
Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.movie_add );
ssb.setSpan( new ImageSpan( smiley ), ssb.length()-1, ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE );
textView.setText( ssb, BufferType.SPANNABLE );
With the help of above code, you can add image any where in Textview.
instead of ImageView try clickable="true" and drawableEnd
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/navigation_icon"
android:text="Text"
android:clickable="true"
/>
try to make your xml look like below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/my_icon" />
</LinearLayout>
</LinearLayout>
Use the following layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="2dp"
android:background="#android:color/transparent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="1dp"
android:layout_gravity="left"
android:orientation="horizontal" >
<TextView
android:id="#+id/contentText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium T"
android:gravity="center"
android:minLines="2"
android:layout_gravity="center"
android:textColor="#color/white"
android:padding="5dp"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/contentImage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="5dp"
android:visibility="invisible"
android:src="#drawable/browse_product_thumbnail" />
</LinearLayout>
</LinearLayout>
-Preeya