How to show scrollbars on a TextView programmatically - android

I have searched and searched to find an answer to how to add a vertical (or horizontal) scrollbar to a TextView without having to use the XML just to add the line: android:scrollbars="vertical".
There has to be a way to do this programmatically that doesn't require sticking this within another ScrollView.
I've just found out how and because I am way to excited about this and want to help anyone else who is stuck with the same question, here it is:

Rusian Yanchyshyn, posted the key in his answer at Android: Enable Scrollbars on Canvas-Based View
With the help of an anonmous class & an initializer block we can now do the following:
TextView textViewWithScrollBars = new TextView(context)
{
{
setVerticalScrollBarEnabled(true);
setMovementMethod(ScrollingMovementMethod.getInstance());
setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
// Force scrollbars to be displayed.
TypedArray a = this.getContext().getTheme().obtainStyledAttributes(new int[0]);
initializeScrollbars(a);
a.recycle();
}
}

// try this
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
textView.setText("demotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotext");
textView.setVerticalScrollBarEnabled(true);
textView.setLines(3);
textView.setMovementMethod(new ScrollingMovementMethod());
addContentView(textView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}

Show only when scroll
Xml (in textView element):
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
OnCreate:
textView.movementMethod = ScrollingMovementMethod()

first - Make a style.xml
<style name="VerticalScrollableTextView">
<item name="android:scrollbars">vertical</item>
<item name="android:scrollbarFadeDuration">0</item>
</style>
second - use ContextWrapper
val textView = TextView(
ContextThemeWrapper(context, R.style.VerticalScrollableTextView)
)
textView.movementMethod = ScrollingMovementMethod.getInstance()
That's All!

Related

How to get layout object

I'm following the accepted answer to this question:
ArrayList<EditText> myEditTextList = new ArrayList<EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) myLayout.getChildAt( i ) );
How do you get myLayout, what does it actually represent? In my app I define linear_layout in activitymain.xml but can't seem to get it in MainActivity.java (I tried using R but it didn't work). I'm new to Android development and could really use some high level guidance.
Let Linear Layout be the root of your xml:
<LinearLayout android:id="#+id/my_layout" >
//your views goes here
</LinearLayout>
Then in your Activity you'll have to initialize the view once your layout gets inflated like this
LinearLayout myLayout = (LinearLayout) findViewById(R.id.my_layout);
Hope this helps.
You need to use myLayout as your root layout in your layout.xml file
the myLayout maybe a FrameLayout, LinearLayout, RelativeLayout or ConstraintLayout layout
SAMPLE CODE
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/myLayout"
android:orientation="vertical">
<!-- add your view here-->
</LinearLayout>
Now do findViewById in side your actvity
public class MyActivity extends AppCompatActivity {
LinearLayout myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
myLayout = findViewById(R.id.myLayout);
}
}
You can try below code to get a reference of your LinearLayout in Java code.
LinearLayout myLayout=(LinearLayout) findViewById(R.id.your_linear_layout_id)
assuming your actiivtymain.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<SomeLayout
android:id="#+id/some_id"
...>
<EditText
...
/>
<EditText
...
/>
.
.
.
</SomeLayout>
mLayout you are referring is the parent of all your edit texts. It may not be the root of your layout but if you want to get a Java instance of it, you need to give it an id. and get the instance by using the code given by #Nilesh Rathod. One more thing to remember is to call the method findViewById(R.id.my_layout); only after setContentView(R.layout.activitymain);.

setMargrin in Relativelayout Programmatically

I want to add a text view with margins into a Relativelayout by clicking a button, but whenever I add the setMargins to the LayoutParams, it make the TextView disappeared. Here is my code
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/test11">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/test"
android:onClick="onClick"
android:text="asdf"/>
</RelativeLayout>
Here is my code:
public class tet extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
public void onClick(View v){
TextView textView = new TextView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 30, 30, 30);
textView.setText("asdf");
textView.setLayoutParams(layoutParams);
((RelativeLayout)findViewById(R.id.test11)).addView(textView);
}
}
I'm 99% sure your TextView is behind your Button. There are several problems:
layoutParams.setMargins(30, 30, 30, 30);
This sets the margins in pixels! If you set the margins in a xml Layout they are often set as dp (you can choose the unit there). If you want dp in your code you have to convert px => dp (here you can find an example).
Secondly: You are using an RelativeLayout, hence all Views are arranged relatively in your layout. You didn't provide any infromation to you TextView so it will be placed at the left/top of its parent (so behind your Button).
Try swap your RelativeLayout for a LinearLayout or provide additional arrangement information.
For the future: These kind of problems ("my view is missing") is easily solved by the Hierarchyviewer provided by the Android SDK.

Flow Layout in JAVA File

This is my Project XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flowLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- enter code here -->
</LinearLayout>
I need to access it in the JAVA file which should contain 3 sentences in 3 text views ...
public class MainActivity extends Activity {
private View layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
//enter code here
}
private void findViewById() {
View layout = findViewById(R.id.flowLayout);
//enter code here
}
}
To add a TextView to your layout do this:
LinearLayout layout = (LinearLayout)findViewById(R.id.flowLayout); //depending on which layout is it.
Then, Create a TextView, set it text and add it to the layout:
TextView tvText = new TextView(this);
tvText.setText("your desired sentence");
layout.addView(tvText);
and do this for all 3 of your TextViews, for setting the height and width use LayoutParams (example):
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 2, 0);
As you can see this is the way you set Mergins as well, in the end set those params to the textView:
tvText.setLayoutParams(layoutParams);

Still Can't Add Button to Layout in Android

I just started android and I tried below questions to get this answer before posting it here:
Android - Adding layout at runtime to main layout
Add button to a layout programmatically
Dynamically adding a child to LinearLayout with getting each child's position
And I am still not able to add a button to linear layout :(
Below is the code for activity, please let me know where I am wrong:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.activity_main, null);
Button btn = new Button(this);
btn.setId(123);
btn.setText("Welcome to WI FI World");
layout.addView(btn);
}
And xml looks like below:
<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" >
</LinearLayout>
Try assigning an id to your layout then add the button to the layout.
Im pretty sure those 2 layouts are not the same, so you are infact adding a button to a layout that is never displayed.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.lnr_main);
Button btn = new Button(this);
btn.setId(123);
btn.setText("Welcome to WI FI World");
layout.addView(btn);
}
With Layout assigned an id
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lnr_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
Take the Id for LinearLayout in XML and in jave code use that id of LinearLayout from XML
<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:id="#+id/linear" >
</LinearLayout>
In onCreate():
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
//Select widgets
linear.addView()
Give an id to your linearLayout.
Then in your code
LinearLayout layout = (LinearLayout)findViewById(R.id.given_id);
Keep the rest the same, should work.

Android:Drawing Many Text Views

I'm new to android platform , I'd like to settext using textviews , I tried to write set text into two textviews but its just draws one textview why?
I can't draw two textviews
TextView tv1;
TextView tv2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
tv1 = new TextView(this);
tv2 = new TextView(this);
tv1.setText("Hello");
tv2.setText("How are you?");
}
On Android, the user interface normally should be created using XML-files, instead of Java code. You should read up on the tutorials on android.com, especially:
http://developer.android.com/guide/topics/ui/declaring-layout.html
An example:
In your res/layout/main.xml, you define the text TextView's:
<?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"
>
<TextView android:id="#+id/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 1"/>
<TextView android:id="#+id/TextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 2"/>
</LinearLayout>
Then if you use setContentView in the activity to display this, the app will show to TextView's:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
If you want to programmatically set the text in the Activity, just use findViewById():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
((TextView)this.findViewById(R.id.TextView1)).setText("Setting text of TextView1");
}
I definitely second TuomasR's suggestion to use XML layouts. However, if you're wanting to add new TextViews dynamically (i.e. you don't know how many you will need until runtime), you need to do a couple of other steps to what you are doing:
First, define your LinearLayout in main.xml (it's just easier that way than LayoutParams, IMO):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
Now, you can go to your code, and try the following:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//This inflates your XML file to the view to be displayed.
//Nothing exists on-screen at this point
setContentView(R.layout.main);
//This finds the LinearLayout in main.xml that you gave an ID to
LinearLayout layout = (LinearLayout)findViewById(R.id.my_linear_layout);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
t1.setText("Hello.");
t2.setText("How are you?");
//Here, you have to add these TextViews to the LinearLayout
layout.addView(t1);
layout.addView(t2);
//Both TextViews should display at this point
}
Again, if you know ahead of time how many views that you need, USE XML.

Categories

Resources