I defined a textview with auto-scrolling to bottom enabled and it works.
But when i start the app, the textview content automatically scrolls to the top on the first touch, which appears very strange to me. From then on, it behaves normally, after I scrolls it manually to bottom. I mean, no auto-scrolling to top happens again regardless of the subsequent touchs. And it always scroll to bottom as defined when texts are appended.
How may I identify the cause oh this behavior, in order to circumvent it?.
Here is the the textview layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorScreenBackground"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="xxx.xxxxx.xxxx.MainActivity"
tools:showIn="#layout/activity_main"
android:orientation="vertical"
android:weightSum="100">
<TextView
android:id="#+id/result"
android:scrollbars = "vertical"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:elevation="8dp"
android:textIsSelectable="true"
android:gravity="bottom"
android:background="#color/white"
android:padding="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium" />
<androidx.core.widget.NestedScrollView
android:id="#+id/nested_scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60"
android:background="#color/gray_background"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:listitem="#layout/recyclerview_item"
android:background="#color/gray_background"
/>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
And this is how I use it in code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPref = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
setContentView(R.layout.activity_main);
final Runnable r = new Runnable() {
#Override
public void run() {
doubleClick = false;
}
};
final TextView textView = findViewById(R.id.result);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setTextIsSelectable(true);
textView.setText(deviceLog.getResult());
textView.setOnLongClickListener(this);
recyclerView.setOnDragListener(this);
final NestedScrollView nestedScrollView = findViewById(R.id.nested_scroll);
if (textView.getText().length() == 0) {
LinearLayout.LayoutParams nestedScrolliewLayoutParams = (LinearLayout.LayoutParams) nestedScrollView.getLayoutParams();
nestedScrolliewLayoutParams.weight = 100;
textView.setVisibility(View.GONE);
nestedScrollView.setLayoutParams(nestedScrolliewLayoutParams);
}
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (doubleClick) {
LinearLayout.LayoutParams nestedScrolliewLayoutParams = (LinearLayout.LayoutParams) nestedScrollView.getLayoutParams();
LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
if (textViewEnlarged) {
nestedScrolliewLayoutParams.weight = 60;
linearLayoutParams.weight = 40;
textViewEnlarged = false;
} else {
nestedScrolliewLayoutParams.weight = 20;
linearLayoutParams.weight = 80;
textViewEnlarged = true;
}
nestedScrollView.setLayoutParams(nestedScrolliewLayoutParams);
textView.setLayoutParams(linearLayoutParams);
doubleClick = false;
} else {
doubleClick = true;
Handler doubleClikHandler = new Handler();
doubleClikHandler.postDelayed(r, 500);
}
}
});
}
I have a solution that have done on my Project.
Try to call textView.requestFocus() after set the text value:
itemView.text_view_item_description?.text = "..."
itemView.text_view_item_description?.requestFocus()
Hope It works for you.
Related
I have a screen ButtonPress that shows a button on random positions, it is working for most of the iterations but in some, it is going out of the screen and the button is not visible. I tried to follow this example but that does not seem to help as the button is still invisible for some iterations in the ButtonPress activity. Here's how I am generating the random positions:
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_press);
button = findViewById(R.id.imageView5);
button.setClickable(true);
button.setOnClickListener(this);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
params.leftMargin = button.getWidth()
+ new Random().nextInt(metrics.widthPixels - 2*button.getWidth());
params.topMargin = button.getHeight()
+ new Random().nextInt(metrics.heightPixels - 3*button.getHeight());
button.setLayoutParams(params);
}
And here's the layout file for the button:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#03B0F5"
>
<TextView
android:id="#+id/button_press"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:gravity="center"
android:background="#FFFFFF"
android:text="#string/button_press"
android:textSize="50sp"
android:textColor="#03B0F5"
android:layout_marginStart="0dp"
/>
<ImageView
android:id="#+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="#drawable/ic_button"
tools:ignore="VectorDrawableCompat" />
</LinearLayout>
I removed all the padding and margins from this resource file but it's still not working.
Try with the below code it will help you.
new Handler(this.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
int width = (getResources().getDisplayMetrics().widthPixels)/2;
int height = (getResources().getDisplayMetrics().heightPixels);
Random random = new Random();
int randomXPos = random.nextInt(width-(button.getWidth())/3);
int randomYPos = random.nextInt(height-(button.getHeight())/3);
button.animate().x(randomXPos).y(randomYPos).start();
button.setLayoutParams(params);
}
},100);
How to add info icon when floating label is raised just beside hint.
Like this http://prntscr.com/nlipdn
I have tried with below code:
when focus change on edittext.I added info icon.But I don't know how to set info icon beside floating label. I have added but it's not positioning proper.
public void OnFocusChange(View v, bool hasFocus)
{
ImageView _infoView=null;
if (_infoView != null)
{
_songTitleLayout.RemoveView(_infoView);
_infoView = null;
}
if (v==_eSongTitle)
{
if(hasFocus)
{
int length= _songTitleLayout.Hint.Length;
_infoView = new ImageView(this);
_infoView.SetImageResource(Resource.Drawable.MoreInfo);
_songTitleLayout.AddView(_infoView);
}
}
}
Solution:
I add a margin before you add the _inforView by using code below, you can change the values to adjust the margin and size of image:
ViewGroup.MarginLayoutParams paramsw = new ViewGroup.MarginLayoutParams(40,40);
paramsw.SetMargins(200,0,0,0);
CollapsingToolbarLayout.LayoutParams lp = new
CollapsingToolbarLayout.LayoutParams(paramsw);
txtlayoutusername.AddView(_infoView,0,lp);
Here is all the codes I tested:
This is the code in MainActivity:
public class MainActivity : AppCompatActivity
{
TextInputLayout txtlayoutusername;
EditText txtusername;
ImageView _infoView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
txtlayoutusername = FindViewById<TextInputLayout>(Resource.Id.textuernameInputLayout);
txtusername = FindViewById<EditText>(Resource.Id.txtusername);
txtusername.FocusChange += (object sender, View.FocusChangeEventArgs e) =>
{
if (_infoView != null)
{
txtlayoutusername.RemoveView(_infoView);
_infoView = null;
}
if (sender == txtusername)
{
if (txtusername.IsFocused)
{
int length = txtlayoutusername.Hint.Length;
_infoView = new ImageView(this);
_infoView.SetImageResource(Resource.Drawable.ttt);
ViewGroup.MarginLayoutParams paramsw = new ViewGroup.MarginLayoutParams(40,40);
paramsw.SetMargins(200,0,0,0);
CollapsingToolbarLayout.LayoutParams lp = new CollapsingToolbarLayout.LayoutParams(paramsw);
txtlayoutusername.AddView(_infoView,0,lp);
}
}
};
}
}
Here is code in Xaml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_marginTop="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textuernameInputLayout">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtusername"
android:hint="User Name"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation = "horizontal"
android:id="#+id/textpasswordInputLayout">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtpassword"
android:inputType="textPassword"
android:hint="Password"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
The result:
How to get value from dynamically created EditText in Android?
This is my dynamic view (XML file)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/background"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
And I am using LayoutInflater because I want perfect Designed layout which is not accomplish by me via setLayoutParams so this is the reason I am using LayoutInflater.
And here is my code:
LayoutInflater l = getLayoutInflater();
final LinearLayout mainActivity = (LinearLayout) findViewById(R.id.dynamic);
final View view = l.inflate(R.layout.dynamic_layout_waypoints, null);
buttonWayPoints = (Button) view.findViewById(R.id.btn_waypoint);
editTextWayPoints = (EditText) view.findViewById(R.id.et_waypoint);
mainActivity.addView(editTextWayPoints);
I am new to Android. Most of people suggest this solution but it's not worked for me, the issue is when I implement this code my ADD NEW EDIT TEXT BUTTON not respond me.
This is how I am trying to get inserted value but it returns value of only last created edit text:
public Cursor getPerson(String Query) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(Query, null);
return c;
}
sb is StringBuilder sb;, w is String w;
String queryWayPoints = "select * from route_table where trip_id = " + t;
Cursor s = myDb.getPerson(queryWayPoints);
int count3 = s.getCount();
for (int j = 0; j < count3; j++) {
s.moveToNext();
w = s.getString(s.getColumnIndex("waypoints"));
// se.append(w + "." + "00");
}
trip_name.setText(n);
trip_date.setText(d);
sb.append(w);
}
trip_route.setText(sb.toString());
Although, I didn't get what exactly your question is, still I'll try to help.
What exactly this code does is, on clicking the button on top an new layout(one that you wanted) will add to the activity screen at runtime, i.e an edittext with a button, and when you click on that button, A toast with the value of respective edittext will be shown. Hence solving the problem of getting the value of dynamically added edittext
MainActivity
public class MainActivity extends AppCompatActivity {
Button generateET;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout myLinearLay = (LinearLayout) findViewById(R.id.dynamic);
generateET = (Button) findViewById(R.id.generateBtn);
generateET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater l = getLayoutInflater();
final View viewToAdd = l.inflate(R.layout.to_add, null);
Button buttonWayPoints = (Button) viewToAdd.findViewById(R.id.btn_waypoint);
final EditText editTextWayPoints = (EditText) viewToAdd.findViewById(R.id.et_waypoint);
buttonWayPoints.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editTextWayPoints.getText().toString() != null) {
Toast.makeText(MainActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No text found", Toast.LENGTH_SHORT).show();
}
}
});
myLinearLay.addView(viewToAdd);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.techmahindra.stackques.MainActivity">
<Button
android:id="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="add editText To layout" />
<ScrollView
android:layout_below="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
></LinearLayout>
</ScrollView>
</RelativeLayout>
to_add.xml(layout which will be inflated at runtime)
<LinearLayout 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="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
There are some problems with your code. You're trying to add editTextWayPoints to mainActivity, but editTextWayPoints already has a parent, which is view. I think you should be adding view to mainActivity (mainActivity.addView(view)). Finally, to access the value of editTextWayPoints, you simply do editTextWayPoints.getText().toString();
In this code i just wanted to change the margin when i click on button. but the problem is that this code is working until this line ((TextView)findViewById(R.id.textView)).setText("Changed at runtime!"); is presented in code. if I remove this line the margin is not increasing when i am pressing the button.
This example is from a book "Android Application Development Cookbook", but there is no explanation for this and I didn't find any any thing much helpful about LinearLayout.LinearParams on android website. so any help is appreciated.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((TextView)findViewById(R.id.textView)).setText("Changed at runtime!");
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
params.leftMargin += 5;
}
});
}
}
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">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/button"/>
</LinearLayout>
You should set the LayoutParams again to the view like this,
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
params.leftMargin += 5;
view.setLayoutParams(params);
I am looking to slide/up down a nested layout on its parent layout click.
Ie the parent layout will have a hidden child. On click I would like the parents height to animate down (slide down) to fit the child layout. On click again I would like the child to animate up (slide up). Basically just animating the parents height to show/hide the child.
I have found this which looks to work but seems like a lot of code:
http://gmariotti.blogspot.com/2013/09/expand-and-collapse-animation.html
I have seen a lot of things using 'animateLayoutChanges' to animate things however I cannot get that to work.
I have tried this:
<LinearLayout android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout android:id="#+id/child"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:animateLayoutChanges="true">
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text to show/hide"/>
</LinearLayout>
</LinearLayout>
Then in code:
LinearLayout parent = (LinearLayout)findViewById(R.id.parent);
parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout child = (LinearLayout)findViewById(R.id.child);
child.setVisibility(child.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
That sets the visibility of the child view correctly but there is absolutely no animation.
Well, first of all android:animateLayoutChanges effects the child elements. So, obviously, if you are changing the properties of the element itself, it will not be animated.
I think you can accomplish what you are trying to in API 16 by enabling the LayoutTransition.CHANGING animation.
<LinearLayout android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:animateLayoutChanges="true">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Some text to show/hide"/>
</LinearLayout>
LinearLayout parent = (LinearLayout)findViewById(R.id.parent);
parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
View text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View message = findViewById(R.id.message);
ViewGroup.LayoutParams params = message.getLayoutParams();
params.height = params.height == 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : 0;
message.setLayoutParams(params);
}
});
Try to use SlidingDrawer
use this code
xml layout sideupdown.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bopen"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="open" />
<Button
android:id="#+id/btogg"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="change" />
<Button
android:id="#+id/bclose"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
<SlidingDrawer
android:id="#+id/slidingDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="#+id/chkbok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="On/Off" />
</LinearLayout>
</SlidingDrawer>
</FrameLayout>
java class SlideUpDown.java
public class SlideUpDown extends Activity implements OnClickListener,
OnCheckedChangeListener, OnDrawerOpenListener, OnDrawerCloseListener {
Button opn, cloz, chng, hndl;
CheckBox chkbox;
SlidingDrawer sd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sideupdown);
refference();
opn.setOnClickListener(this);
cloz.setOnClickListener(this);
chng.setOnClickListener(this);
chkbox.setOnCheckedChangeListener(this);
sd.setOnDrawerOpenListener(this);
sd.setOnDrawerCloseListener(this);
}
private void refference() {
opn = (Button) findViewById(R.id.bopen);
cloz = (Button) findViewById(R.id.bclose);
chng = (Button) findViewById(R.id.btogg);
chkbox = (CheckBox) findViewById(R.id.chkbok);
sd = (SlidingDrawer) findViewById(R.id.slidingDrawer);
hndl = (Button) findViewById(R.id.handle);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bopen:
sd.open();
break;
case R.id.bclose:
sd.close();
break;
case R.id.btogg:
sd.toggle();
break;
}
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkbox.isChecked()) {
sd.lock();
hndl.setEnabled(false);
} else {
sd.unlock();
hndl.setEnabled(true);
}
}
#Override
public void onDrawerOpened() {
}
#Override
public void onDrawerClosed() {
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
In one of our applications we hide and show a header and footer. We do this by changing the height and visibility of the view.
It looks something like this
ValueAnimator va;
if (show)
va = ValueAnimator.ofInt(0, px);
else
va = ValueAnimator.ofInt(px, 0);
va.setDuration(400);
va.setInterpolator(new DecelerateInterpolator(2));
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
topicNavBar.getLayoutParams().height = value;
topicNavBar.requestLayout();
if (value == 0) {
if (show)
topicNavBar.setVisibility(View.VISIBLE);
else
topicNavBar.setVisibility(View.GONE);
}
if (show && value == px) {
animationInProgress = false;
}
if (!show && value == 0) {
animationInProgress = false;
}
}
});
va.start();
Hope this helps