Android TextView should scroll vertically automatically - java

I need a TextView which scrolls automatically vertically. This is my layout code:
activity_info.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:contentDescription="#string/info"
android:src="#mipmap/infoImage" />
<TextView
android:id="#+id/tView"
android:layout_below="#+id/iView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:maxLines="500"
android:scrollbars="vertical" />
<RelativeLayout
android:layout_below="#+id/tView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center">
<Button
android:id="#+id/buttonYes"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="YES" />
<Button
android:id="#+id/buttonNo"
android:layout_toEndOf="#id/buttonYes"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="NO" />
</RelativeLayout>
</RelativeLayout>
I read that I have to input this line:
tView.setMovementMethod(new ScrollingMovementMethod())
into my onCreate method in my activity:
InfoActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
TextView tView = (TextView) findViewById(R.id.tView);
tView.setMovementMethod(new ScrollingMovementMethod());
}
If I do that, the TextView does not scroll automatically, if some new input (text) is inserted into the View. I know there are some other questions concerning the problem. But I've tried most of them.
How can I solve this problem?

Try to set the textView to android:gravity="bottom"
and use textView.append("\n"+"New Input Text."); for new input text;

You are almost there. Just add a few more things to achieve this,
First of all add those properties in your TextView
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
android:gravity="bottom"
Secondly as usual use,
tView.setMovementMethod(new ScrollingMovementMethod());
This should solve your problem.

I would suggest wrapping your TextView in a ScrollView
<ScrollView
android:id="#+id/sView"
android:layout_below="#+id/iView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/tView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="500"
android:scrollbars="vertical"
android:gravity="bottom" />
</ScrollView>
You can append new text by doing
public void appendAndScrollDown(String someText) {
tView.append("\n" + someText);
sView.fullScroll(View.FOCUS_DOWN);
}

Related

Avoid clicking of full layout

I only want my textview with id clickable_txtView to be clickable in relative layout. Here is the code -
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#android:id/empty"
android:clickable="false">
<ImageView
android:layout_width="120sp"
android:layout_height="130sp"
android:layout_centerHorizontal="true"
android:src="#drawable/flower"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="120sp"
android:text="Dont click me />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/clickable_txtView"
android:gravity="center"
android:paddingTop="140sp"
android:clickable="true"
android:text="Click me" />
</RelativeLayout>
#OnClick(R.id.clickable_txtView)
public void txtViewClick() {
//Some event
}
Thr problem is I am able to click whole relative layout and some event happens after onclicking relative layout.
How do I avoid this and make it limited to text view only.
Your textview has an attribute android:paddingTop="140sp" which basically wraps over the entire relative layout, because of which you are having the problem with onClick behaviour.
Use this RelativeLayout structure (provide ID to the "don't click me" textview, and place the "clickable textview" below it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="120sp"
android:layout_height="130sp"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/do_not_click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="120sp"
android:enabled="false"
android:text="Dont click me" />
<TextView
android:id="#+id/clickable_txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:layout_below="#id/do_not_click"
android:text="Click me" />
</RelativeLayout>
In your activity where you are inflating this view try this:
first initialize your textview
TextView textView = (TextView) findViewById(R.id.clickable_txtView);
then apply clickListner on it
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your work here
}
});
I'm not sure but you can try adding below line to your parent layout
android:touchscreenBlocksFocus="true"
like
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#android:id/empty"
android:touchscreenBlocksFocus="true">

Issue with trying to get an EditText, because of a casting issue

I'm working on a gallery type app. The issue that I have at the moment is trying to fetch the typed text within an EditText. The error that I am getting is a casting exception because the code fetches the EditText block from the layout as a TextView block. This only occurs after I swipe, which causes the some of the LinearLayouts imbeded in the second GridLayout to be removed.
The LinearLayouts are put in at runtime into the Gridlayout (with the id product_details), the amount Layouts is calculated according to which picture is pressed on.
This part of the code fetches the current LinearLayouts that are imbeded in the GridLayout:
for (int i = 0; i<codes.size(); i++) {
//Sets the different elements to te correct texts
ViewGroup linearLayout = (ViewGroup) gridLayout.getChildAt(i);
TextView textCode = (TextView) linearLayout.getChildAt(0);
TextView textPrice = (TextView) linearLayout.getChildAt(1);
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
commentsAddList.add(editTextC.getText().toString());
quantityAddList.add(editTextQ.getText().toString());
priceAddList.add(textPrice.getText().toString().substring(1));
codeAddList.add(textCode.getText().toString());
productModelAddList.add(new ProductModel());
productModelAddList.get(i).setCode(codeAddList.get(i));
if(!quantityAddList.get(i).equals("")) {
productModelAddList.get(i).setQuantity(Integer.parseInt(quantityAddList.get(i)));
}else{
productModelAddList.get(i).setQuantity(0);
}
productModelAddList.get(i).setComment(commentsAddList.get(i));
if(!priceAddList.get(i).equals("")) {
productModelAddList.get(i).setPrice(Double.parseDouble(priceAddList.get(i)));
}else{
productModelAddList.get(i).setPrice(0);
}
mRequestPermissions();
adaptToText.addToList(productModelAddList.get(i));
}
The main picture xml Layout:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageLayout"
android:columnCount="2"
android:verticalSpacing="0dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:fitsSystemWindows="true">
<ImageView
android:layout_columnSpan="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"/>
<GridLayout
android:id="#+id/product_details"
android:columnCount="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</GridLayout>
</GridLayout>
The LinearLayout that is inserted into the GridLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_columnSpan="1"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:id="#+id/orderSideLayout">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productCode"
android:text=""
android:padding="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productPrice"
android:text=""
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editText1"
android:hint="Quantity goes here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="number"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/comments"
android:text="Comments: "
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editTextComments"
android:hint="Comments here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="text"/>
</LinearLayout>
I'm not sure where the casting exception is caused or why exactly it happened. If anyone is able to help, I would appreciate it a lot! Thanks in advance!
Instead of getting the view by position getting it by id would be easier.
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
Try this instead:
EditText editTextQ = (EditText) linearLayout.findViewById(R.id.editText1);
EditText editTextC = (EditText) linearLayout.findViewById(R.id.editTextComments);
This will be better because if change the xml, it could change view positions, and find views by id you wouldn't need to change the code.

TextView created programmatically not shown

I am trying to create a TextView programmatically which will be shown in another layout that doesn't belong to the same activity but the TextView isn't shown. Following is the code:
Activity:
public class LogActivity extends AppCompatActivity {
LinearLayout textLayout;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
LayoutInflater layoutInflater=getLayoutInflater();
View textEntryLayout=layoutInflater.inflate(R.layout.layout_two, null);
textLayout=(LinearLayout) textEntryLayout.findViewById(R.id.layout);
textView=new TextView(this);
textView.setText("TextView");
textView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
textLayout.addView(textView);
}
Layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
android:background="#color/editTextBG">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
That's because your container layout, has other children whose heights are set to match_parent. You won't be able to see TextView you add in Java code since the other children added in XML fill all the screen.
You need to add textEntryLayout to your layout. You are modifying that inflated view without adding it to (e.g.) a layout in your layout_one.
Try:
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_id_in_layout1);
layout.addView(textEntryLayout);

how to get a layout on click of imageview?

Hello.. I have a layout in which I have ImageView and EditText view. I want to add a different layout on click of ImageView of arrow.
It should look like this after click event of ImageView. How can I get this??
Pleas help...
For perfect answer please add your xml file and java file source code.
Example
MainActivity.java
public class MainActivity extends Activity {
LinearLayout linear1,linear2;
ImageView imgArrow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
imgArrow = (ImageView) findViewById(R.id.imgArrow);
linear1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linear2.getVisibility() == View.GONE){
linear2.setVisibility(View.VISIBLE);
imgArrow.setImageResource(R.drawable.up);
}else{
linear2.setVisibility(View.GONE);
imgArrow.setImageResource(R.drawable.down);
}
}
});
}
}
activity_main.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:orientation="vertical" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btnClick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:text="Additional Contact" />
<ImageView
android:id="#+id/imgArrow"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.2"
android:src="#drawable/down" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:visibility="gone"
android:orientation="vertical" >
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name here" />
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Phone number here" />
<Button
android:id="#+id/btnone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Click Here" />
</LinearLayout>
</LinearLayout>
Here down and up is image file for arrow.
so put that both arrow file in drawable folder.
You could just set an OnClickListener to the ImageView, which contains the arrow. Then assign a method, which handles the click and produces your different layout

Unable to dynamically add EditText to LinearLayout

I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.
<LinearLayout
android:id="#+id/layout_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator"
android:layout_margin="5dp"
android:background="#color/yellow_background"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_check_empty" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tv_add_team_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="Add a team member"
android:textColor="#color/text_green"
android:textSize="15dp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_add_team_member"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/iv_create_assignment_attach"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_flag_alt" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="15"
android:text="Due"
android:textSize="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="65"
android:hint="None"
android:selectAllOnFocus="true" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_calendar_empty" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_create_assignment_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Trash" />
<Button
android:id="#+id/btn_create_assignment_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
Here is the Java code, the onClickListener implementation of the TextView.
tvAddTeamMember.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
EditText et = new EditText(getActivity());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Text");
ll.addView(et);
}
You may proceed this way ! This is not what you're looking for. But your problem will get resolved.
1) By default add the EditText view in your XML and make it invisible.
2) On Clicking the TextView, make it visible.
enjoy !
Hi #Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
Try to change the line :
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
With this:
android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Then do your work
If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.
Try with this code.
replace ur line ll.addView(et);
by::
ll.addView(et,p);
and delete ur line et.setLayoutParams(p);
A simple way to do this.
Button add = (Button) findViewById(R.id.add_checkpoint);
final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);
// layout has assigned weight = "1.0" in xml & it's a child of ScrollView
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater oLayoutInflater = getLayoutInflater();
// oView is declared globally & Layout "row_checkpoint" contains a EditText
oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);
final EditText speciesEditText = (EditText) oView
.findViewById(R.id.checkpoint);
speciesEditText.setId(layout.getChildCount());
// Perform whatever required over EditText, same way you can add more elements.
layout.addView(oView);
}
});
Hope it will help.

Categories

Resources