I am trying to figure out how to create a layout that looks like this.
--------------------------------
Sep Text here
--------------------------------
text 1 here | IMG
text 2 Here |
--------------------------------
I have tried to do this with a table layout inside of a table layout but than I am having trouble accessing the "text 1" and text 2" to set their values. Any thoughts on how I should accomplish this? Or maybe you can tell me how to access elements that are down two levels of layouts.
This can be easily accomplished with a RelativeLayout. It would be implemented in the lines of the following:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/img"
android:src="#drawable/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text1"
android:layout_alignParentLeft="true" />
</RelativeLayout>
Accessing text1 and text2 by code could be done by:
TextView text1 = (TextView) findViewById(R.id.text1);
TextView text2 = (TextView) findViewById(R.id.text2);
// set text
text1.setText("foo");
text2.setText("bar");
Hope it helps!
Related
How to make multiple textview vertically scrollable in a certain different portions of the screen ?
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#drawable/girl1" />
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/textview"
android:maxLines="8"
android:scrollbars="vertical"
android:layout_alignTop="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:layout_toRightOf="#+id/imageView"></TextView> </RelativeLayout>
TextView intro;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meet_our_team);
TextView txtview=(TextView)findViewById(R.id.textview);
String text="I like programming in Android. This tip shows you how to make a TextView scrollable in Android.";
txtview.setText(text);
intro.setMovementMethod(new ScrollingMovementMethod());
} }
I am getting following error :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference
at com.example.cepl_pc.trial.MeetOurTeam.onCreate
Your problem lies on line intro.setMovementMethod(new ScrollingMovementMethod());
You haven't assigned any id to TextView intro , replace it with your txtView.
Also,you have android:maxLines = "8" and your text string is not long enough to get a scroll view, use below code you will see the scrolling effect.
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#drawable/girl1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textview"
android:maxLines = "3"
android:scrollbars="vertical"
android:layout_alignTop="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:layout_toRightOf="#+id/imageView"></TextView>
In java,
TextView txtview=(TextView)findViewById(R.id.textview);
String text="I like programming in Android. This tip shows you how to make a TextView scrollable in Android.";
txtview.setText(text);
txtview.setMovementMethod(new ScrollingMovementMethod());
You can even
<ScrollView
android:layout_width="200dp"
android:layout_height="200dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text"/>
</ScrollView>
If u want all of your layout to scroll.Set ScrollView as your root layout and set fillview port to true.like this:
<Scroll View
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"/>
.
.
//rest of your xml code.
.
.
<ScrollView/>
if this works let me know in comments section.
The best way to get around to it would be to enclose it in ScrollView in your .xml file as following.
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#drawable/girl1" />
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/textview"
android:maxLines="8"
android:scrollbars="vertical"
android:layout_alignTop="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:layout_toRightOf="#+id/imageView"></TextView>
</ScrollView>
declare textView
textView1 = (TextView) findViewById(R.id.textView1);
in a method add following code
textView1.setMovementMethod(new ScrollingMovementMethod());
I have two Textviews in a RelativeLayout like this:
<RelativeLayout>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
(*) android:layout_above="#+id/message"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1">
<TextView
android:id="#+id/message"
android:layout_width="302dp"
android:layout_height="wrap_content"
android:layout_above="#+id/editText1"/>
</ScrollView>
I want the Textview id'd "message" to be scrollable. So I added it within a ScrollView but where I have put a star (android:layout_above) I am getting the error: #+id/message is not a sibling in the same RelativeLayout
How do I fix this? Any help is appreciated
First of all you have to remove "+" from
This
android:layout_above="#+id/message"
To
android:layout_above="#id/message"
And use TextView's scrolling method rather than scrollview
<TextView
android:scrollbars="vertical" // Add this to your TextView in .xml file
android:maxLines="ANY_INTEGER" // also add the maximum line
android:id="#+id/message"
android:layout_width="302dp"
android:layout_height="wrap_content"
android:layout_above="#id/editText1"/>
Then after in your .java file add following methods for your textview above.
TextView message = (TextView) findViewById(R.id.message);
message.setMovementMethod(new ScrollingMovementMethod());
It is giving this error because your TextView's parent is not relative layout,it's ScrollView. Instead give an id to your ScrollView and then use:
android:layout_above="ScrollView id here"
Give id to your ScrollView and align relative to the ScrollView
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
(*) android:layout_above="#+id/scroll"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:id="#+id/scroll">
<TextView
android:id="#+id/message"
android:layout_width="302dp"
android:layout_height="wrap_content"
android:layout_above="#+id/editText1"/>
</ScrollView>
Do not use scrollview to make your TextView Scrollable. Rather use scrolling movement method like this way.
TextView message = (TextView) findViewById(R.id.message);
message.setMovementMethod(new ScrollingMovementMethod());
Move your TextView id to the ScrollView so that above refers to a sibling.
<RelativeLayout> <TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" (*)
android:layout_above="#+id/message"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true " />
<ScrollView
android:layout_width="wrap_content"
android:id="#+id/message
android:layout_height="wrap_content"
android:layout_weight="1"> <TextView
android:id="#+id/message"
android:layout_width="302dp"/>
</ScrollView>`
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
I'm making a custom XML layout to display a cocktail recipe. Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/drinkname" android:layout_height="wrap_content"
android:textSize="30sp" android:text="#string/drink_name"
android:layout_width="wrap_content" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView android:layout_width="fill_parent"
android:layout_height="2dp" android:background="#FFFFFFFF"
android:layout_below="#id/drinkname" />
<TextView android:layout_height="wrap_content" android:id="#+id/ingredient_label"
android:text="Ingredients: " android:layout_width="wrap_content"
android:layout_below="#+id/drinkname" android:layout_marginLeft="10dp"
android:layout_marginTop="16dp" />
<TableLayout android:layout_height="wrap_content"
android:id="#+id/ingredient_table" android:layout_width="wrap_content"
android:layout_alignTop="#+id/ingredient_label"
android:layout_alignLeft="#+id/drinkname"
android:layout_alignParentRight="true">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 2 oz. Gin (Boodles)"android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="#+id/tableRow2" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 2 oz. Vodka (Stolichnaya)"
android:id="#+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="#+id/tableRow3" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 1/2 oz. Vermouth, dry" android:id="#+id/textView3"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="#+id/tableRow4" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 3 pieces Olive" android:id="#+id/textView4"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<TextView android:id="#+id/instruction_label"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_below="#id/ingredient_table" android:layout_alignLeft="#id/ingredient_label"
android:text="Instructions: " android:layout_marginTop="25dp" />
<TextView android:id="#+id/instructions"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignTop="#id/instruction_label"
android:layout_alignLeft="#id/drinkname"
android:layout_alignParentRight="true" android:text="#string/drink_instructions" />
<TextView android:id="#+id/glass_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="#id/instructions"
android:layout_alignLeft="#id/ingredient_label" android:text="Glass:"
android:layout_marginTop="25dp" />
<TextView android:id="#+id/glass_type"
android:layout_toRightOf="#id/glass_label" android:text="#string/drink_glass"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignTop="#id/glass_label" android:layout_alignLeft="#+id/drinkname" />
As you can see all the content displayed is written into the layout itself or is taken from a string resource. My question is: what would I have to do to keep the layout I created below, but be able to fill in the content programmatically?
You would have to do something like this...
(TextView) this.findViewById(R.id.drinkname).setText("Water");
Use findView to get a reference to a particular view on the activity. Pass it the Resource identifier you gave in the XML (the id property). Cast it to the correct View type, and then set it's properties.
The resource id's are created automatically from the id attribute in XML, and added to the R class inside R.id..
As an example from the application I'm currently writing, I usually define the views I'm going to be modifying as class level fields near the beginning of the class like so:
TextView tvGameDate;
TextView tvGameTime;
TextView tvHomeTeam;
TextView tvAwayTeam;
TextView tvHomePitcher;
Then near the start of onCreate I will populate them all with the references to the actial views like this:
tvGameDate = (TextView) this.findViewById(R.id.tvGameDate);
tvGameTime = (TextView) this.findViewById(R.id.tvGameTime);
tvHomeTeam = (TextView) this.findViewById(R.id.tvHomeTeam);
tvAwayTeam = (TextView) this.findViewById(R.id.tvAwayTeam);
tvHomePitcher = (TextView) this.findViewById(R.id.tvHomePitcher);
Then I can just refer to the fields I defined whenever I need to access them like this:
tvHomeTeam.setText(attributes.getNamedItem("home_long").getNodeValue());
tvHomePitcher.setText(" (" + attributes.getNamedItem("home_pitcher").getNodeValue() + ")");
In your XML it looks like you may actually need to dynamically create additional text views on the fly based on the recipe. You can do this like so (where llIngredients maps to some LinearLayout in your XML):
TextView tv = new TextView(context);
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Some text for the new view");
llIngredients.addView(tv);
Not sure I follow you. I'm guessing this resource represents an activity. You can get a reference to your text view, lets say glass_type by saying -
TextView glassTypeTxtView = findViewById(R.id.glass_type) and set its content dynamically by saying glassTypeTxtView.setText("new text").
Please correct me if I misunderstood your question completely.
I am using ellipseize in my text view. From here, it said it should truncated.
But I don't see it get truncted. Basically, I want 2 textfields next to each other.
txt1 on the right and txt2 on the left. I want the txt2 truncated when it is too long. But what I am seeing now is it is overlapping the text of txt1.
http://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize
<RelativeLayout
android:id="#+id/one"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:singleLine="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorPrimary"/>
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toRightOf="#id/txt1"
android:ellipsize="end"
android:singleLine="true"
android:layout_weight="1"
android:layout_centerVertical="true"
/>
</RelativeLayout>
I think you want android:layout_toLeftOf="#id/txt1" in txt2, meaning txt2 is to the left of txt1. Looks like you are trying to layout to the right of a view which is aligned with the right of the parent currently.