Make textview vertically scrollable - android

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());

Related

Android XML Layout - a picture with a TextView on it

I'm working on an XML layout in my app and what I want to do is to make an ImageView have a piece of text on it (I want to have a TextView on it). Anyway, less words and more examples. What I want is something like this:
I would like to know what is the best way to make it (with an opaque strip and preferably with text that will stretch or shrink depending on the length of name. Though I think I might figure the stretching out by myself).
You can create a RelativeLayout with the ImageView and TextView as its two children
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="150dp"
android:layout_height="75dp"
android:src="#drawable/myImage" />
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/myImageView"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/myImageView"
android:background="#drawable/myBackground"
android:gravity="center" />
</RelativeLayout>
Building on what Chris Stillwell said, set the Alpha property to less than 1:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="150dp"
android:layout_height="75dp"
android:src="#drawable/myImage" />
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/myImageView"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/myImageView"
android:background="#drawable/myBackground"
android:gravity="center" />
</RelativeLayout>
Then in your onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
ImageView imageView = (ImageView)findViewById(R.id.myImageView);
TextView textView = (TextView)findViewById(R.id.myTextView);
textView.setText("Your Text");
textView.setAlpha(.6f);
}

when adding views they go one below another even if width is wrap content

I have a Linear layout then programatically I'm adding some spinners and buttons and so on, but I have xml button Wrap content (width) and then on java I add spinner (or anything else) and it goes below this view even if both views are wrap content:
progBar = new ProgressBar(this);
pBarToca = new ProgressBar(this);
pBarToca.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linToca = (LinearLayout) findViewById(R.id.tetoca);
linToca.addView(pBarToca);
and it's placed under the button of xml:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:layout_marginTop="6dp"
android:id="#+id/tetoca">
<TextView style="#style/StylePartida"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/te_toca_jugar" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9" android:onClick="callJugar"
android:text="#string/jugar" />
</LinearLayout>
edit!!!!!!
I want textview on first line then on next line button + progressbar (for example)
You have android:orientation=vertical so the Views will be laid out starting at the top and going down.
If you want them to all be next to each other, remove that from your xml since the default orientation for a LinearLayout is horizontal. If you do this, you will obviously need to change the android:width to wrap_content for your TextView or else it will take up the entire screen.
After your comment, a RelativeLayout would work best here.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
style="#style/StylePartida"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/te_toca_jugar"
android:id="#+id/tvID" /> // give it an id
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:id="#+id/tetoca"
android:layout_below="#/id=tvID"> // place it below the TV
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9" android:onClick="callJugar"
android:text="#string/jugar" />
</LinearLayout>
</RelativeLayout>
Note the changes in the comments. Now when you add your progressbar to the LL, it should be next to the Button. You may need some changes but this should give you approximately what you want.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:id="#+id/tetoca">
<TextView style="#style/StylePartida"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/te_toca_jugar"
android:text="#string/te_toca_jugar" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9" android:onClick="callJugar"
android:text="#string/jugar" />
</LinearLayout>
In your textView you are matching the parent
android:layout_width="match_parent"
This will cause the textview to take up the entire width of the parent view.
android:layout_width="wrap_content"
and
android:orientation="horizontal"
android:orientation="vertical" will cause the elements to be stacked.
If you are using "horizontal" it's important not to have a child element with width matching parent.
EDIT:
After OPs change to question:
I have used a textview, two buttons and listview to give you an idea of how you can format it. There are many ways to achieve the same thing, this is one suggestion.
The internal linearlayout has a horizontal orientation (by default).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="te_toca_jugar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9"
android:text="jugar"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9"
android:text="jugar2"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv">
</ListView>
</LinearLayout>
</LinearLayout>

Error with relative layout. "is not a sibling"

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>`

Put TextView inside EditText

Please tell me, how i can to put text inside a EditText like this
And only right text can be edit.
Keep TextView and EditText inside Relative Layout and Adjust them according to your requirement.
Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/Edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="To my first character" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Edittext"
android:layout_alignBottom="#+id/Edittext"
android:layout_alignParentRight="true"
android:text="123123" />
</RelativeLayout>
Change it if you have any changes
Try this...
Layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/mobile"
android:inputType="phone"
android:background="#drawable/some_edittext_background"
android:labelFor="#+id/editText"
android:maxLength="9"
android:paddingBottom="8dp"
android:paddingStart="48dp"
android:paddingEnd="8dp"
android:paddingTop="8dp" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingStart="8dp"
android:text="+971" />
</FrameLayout>
Result:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("Количество", TextView.BufferType.EDITABLE);
You need to combine 3 components:
a container : use a RelativeLayout since it allows views overlapping.
a TextView align to the right edge of the RelativeLayout (width = wrap_content)
an EditText taking the full width of the RelativeLayout (width = match_parent)
To put a text in the EditText: myEditText.setText("myText")
why not just setting the text using setText() and keeping the left part constant?
Im assuming your asking how to put text inside a TextView, programatiacally it is done this way
textView.setText("anything you want..."); //enter a string into parameters
where textView is a instance of the TextView class.

Android: Layout with row span

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!

Categories

Resources