Android scrollTo(0,0) action on click - android

Hello I am really new on android development and I would like to ask a simple question.
I have a button (buttonR2C1).
When I click the buttonR2C1 I change the text of my textView (the textview is included inside a scrollView).The problem is that the position of the scrollView remains the same.
I would like to add an action that brings my ScrollView in position 0,0 by using the scrollTo action.
Can anyone help me achieve this?
.java
final TextView tv2 = (TextView) findViewById(R.id.mainL1R2);
final Button button1 = (Button) findViewById(R.id.buttonR2C1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
tv2.setText(getResources().getString(R.string.L1R2C1));
}
});
.xml
<Button
android:id="#+id/buttonR2C1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capitolo 1"
android:textSize="18sp" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical" >
<TextView
android:id="#+id/mainL1R2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fontFamily="CustomText"
android:gravity="left"
android:scrollHorizontally="true"
android:text="#string/L1R2C1"
android:textSize="20sp"
android:textStyle="normal" />

You have to use the scrollTo method of the ScrollView, not the TextView, like
final ScrollView scr = (ScrollView) findViewById(R.id.scrollView1);
scr.scrollTo(x, y);
To scroll exactly to your TextView, you'll have to get the position of the TextView.
Have a look at this answer.

Related

Listener not working on linearlayout which contain Edittext

I have an EditText contained in a LinearLayout. For My project I have set an OnclickListener on the LinearLayout. When I launch application nothing happened on click to layout. Maybe It due to the EditText but need that OnCliclistener to be work on LenearLayout.
MyCode:
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/lnlabelnameuniteholder"
android:id="#+id/choix_decategorie"
android:layout_marginTop="10dp" >
<EditText
android:text="#string/choix_de_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textSize="18sp"
android:textColor="#000" />
</LinearLayout>
CODE
maCategorie = (LinearLayout) findViewById(R.id.choix_decategorie);
maCategorie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
MyDB db = new MyDB(MyActivityCategorie.this);
ArrayList<Category> categoryList = db.getCategory();
ChoiceCategoryDialog categoryDialog = new ChoiceCategoryDialog(MyActivityCategorie.this, R.string.add_category, mCategoryTextView.getText().toString(), categoryList, MyActivityCategorie.this, true);
categoryDialog.show();
}
});
Thanks
In your code, you are referring to R.id.choix_decategorie (which is not shown in your code, is this a LinearLayout you want to set onClickListener on?
For a LinearLayout that has child elements (like yours does) - may need to prevent the child elements from receiving focus - you can set android:descendantFocusability="blocksDescendants" like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/choix_decategorie"
android:id="#+id/lnpriceholderlabel"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="10dp" >
For a good measure you may also set android:clickable="false" on each of the child elements. So your layout would look something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/choix_decategorie"
android:id="#+id/lnpriceholderlabel"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="10dp" >
<EditText
android:text="#string/choix_de_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:clickable="false"
android:textSize="18sp"
android:textColor="#000" />
</LinearLayout>
Give it a try and let us know if this works.
Try this example:
LinearLayout yourLayout = (LinearLayout) findViewById (R.id.yourID);
yourLayout.setOnClickListener(new View.OnClickListener() { // remember to use "View."
#Override
public void onClick(View v) {
Toast.makeText(this, "hello", Toast.LENGTH_LONG).show();
}
});
set LinearLayout attribute android:clickable="true"
if you have button or textview in layout set android:clickable="false" for all of them
You must have to set the LinearLayout clickable="true"
<LineartLayout....
android:clickable="true"...>
This will work
And you need to put some margins in EditText, because EditText comes upside on LinearLayout and space doesn't remain for LinearLayout to be clickable.
put
<EditText
android:layout_margin="5dp".../>
your click listener will work but your edit text hide the linear layout , if you remove the edit text it will work or you can set height and width not equal to linear layout.
As a workaround you could do something like this (kotlin code):
editText.inputType = InputType.TYPE_NULL
editText.setOnClickListener { linearLayout.performClick() }
In that case the editText won't pop up the keyboard and the linearLayout will receive the a click event.

Add a EditText with button

I want when I click add icon (+) as this image, it will add more a EditText to add another address. Number address can expand as user want. How I can do that ?
Add a EditText
First of all you need to take container in which you can addviews at runtime :
For example:
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"/>
<Button
android:id="#+id/addAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Now you need to add edit text at runtime.
final LinearLayout container = (LinearLayout) findViewById(R.id.container);
Button button = (Button) findViewById(R.id.addAddress);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = new EditText(HomeActivity.this);
container.addView(editText);
}
});
Please read more on how to add views at runtime.

Replace the content of Linear layout at onclick button android

I want to replace the content of the linear layout which has button and textview with an editText type="phone" after an onclick event of button. They're all in the located in the same page.
Is there a way to do that?
Use following code to remove all Views.
lauout.removeAllViews();
You'll have to make two separate fragments and use a fragmenttransaction to replace one with the other.
See fragmenttransactions:
http://developer.android.com/reference/android/app/FragmentTransaction.html
What I can understand from your question
1) There are 3 views Button, TextView and EditText under one LinearLayout
Like this:
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My TextView " />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" // with input type phone
android:visibility="gone" />
</LinearLayout>
And You want to remove the textview and replace it with Edit Text on Button Click
like this
tv=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setVisibility(View.GONE);
et.setVisibility(View.VISIBLE);
}
});
place all of your three elements in the layout in your xml_layout, and set the EditText visibility to "gone", then when clicking the Button you mentioned, just set the Button and the TextView visibility to "gone" and the EditText visibility to "visible":
<LinearLayout android:id="#+id/layout"
.
.
.>
<Button android:id="#+id/button"
.
.
.
android:visibility="visible"/>
<TextView android:id="#+id/text"
.
.
.
android:visibility="visible"/>
<EditText android:id="#+id/edit_text"
.
.
.
android:inputType="phone"
android:visibility="gone"/>
</LinearLayout>
when clicking the Button:
public OnClickListener onButClick = new OnClickListener() {
#Override
public void onClick(View v) {
button.setVisibility(View.GONE);
text.setVisibility(View.GONE);
text_view.setVisibility(View.VISIBLE);
}
};

how to use viewswitcher with same id's in both layouts

I am using view switcher in my project.
In my xml I have created 2 layouts with all same Id's.
After I switch my view I can not switch to previous view because I am using same ID's in both layouts.
Now how can I use one listener in java code for both layouts in view switcher.
I dont want to create an another id and create another listener to switch again.
My xml is as below.
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/switchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Switch" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/switchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Switch" />
</RelativeLayout>
</ViewSwitcher>
My java code is as follows
final ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
Button btn = (Button) findViewById(R.id.switchBtn);
btn.setOnClickListener(new OnClickListener() {
private boolean switchCheck;
public void onClick(View v) {
new AnimationUtils();
switcher.setAnimation(AnimationUtils.makeInAnimation(getApplicationContext(), true));
if (!switchCheck) {
switcher.showNext();
switchCheck = true;
} else {
switcher.showPrevious();
switchCheck = false;
}
}
});
Please help..
Crete different ids for parent relative layout in xml.
And then in java file check which relative layout button is used.
using method
step 1)
RelativeLayout r1 = RelativeLayout findviewbyId(R.id.rl1)
RelativeLayout r2 = RelativeLayout findviewbyId(R.id.rl2)
step2)
if button.getParent equals to R1 then ...
else .....
Just try to change ur text in any of the button or textView .may be ur view is switching but because the code for both the layouts is exactly same u r not able to differentiate.

Shake animation is "sliding underneath" other widgets android

I am new to animations. I have a spin Animation on a button. The problem is when that button animation is executed the button intersects other buttons in the layout. So during animation the moving button cannot be seen since it is being covered up by other buttons. Any idea on how to make my animation stay on the top layer of the layout? I would like to figure out a solution that is API 8 compatible.
//create shake animation
final Animation shakeIt = AnimationUtils.loadAnimation(this, R.anim.shake);
findViewById(R.id.button_spin).startAnimation(shakeIt);
// use shake animation
spinnerButton.startAnimation(shakeIt);
EDIT: (here is a lot more info)
So i have played with it over the past few hours and discovered some more things. Lecho intuition is correct, the z order is the driving factor in whether a widget goes "above" or "below" another one. The problem I am facing is could be because of two things:
The button that is at the front of view view (and i want to move it back) has text that is edited near the end of the activity. Does adding text forcing the widget to redraw change the Z order of that view???
The button text is changed in the onResume() and it seems that I'm not able to change the Z-order of the buttons in that method???
answers to these above 2 questions would solve my problem
Edit 2: XML in question
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight=".7"
android:layout_marginTop="5dp" >
<Button
android:id="#+id/categoryButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/drop"
android:prompt="#string/planet_prompt"
android:paddingRight="20dp"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="35dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1.5" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1" >
</LinearLayout>
<Button
android:id="#+id/button_spin"
android:layout_width="0dp"
android:layout_weight="2.35"
android:layout_height="fill_parent"
android:background="#drawable/letseat"
android:layout_gravity="center_horizontal"
android:gravity="center" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<Button
android:id="#+id/button_additems"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#drawable/add_rest"
android:gravity="center_horizontal" />
<Button
android:id="#+id/button_showrestaurant"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#drawable/edit_rest"
android:gravity="center_horizontal" />
<Button
android:id="#+id/button_nearbyplaces"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#drawable/whats_near"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
Just my idea. In android z-order is determined by order in which views are added to XML. Maybe animated button was added to xml before the other one so it is "deeper". Try to put animated button later in XML or use method bringChildToFront(buttonView) on your layout or add your button to layout from code.
Edit:
Ad 1. Adding text seems to not change the Z order. When I change text and call button.bringToFront() the Z order is changed but not when I call bringToFront() or setText() alone.
Ad 2. In onResume adding text doesn't change Z order but method bringToFront() seems to works as expected.
I am not able to test those methods together with view animation right now.
Test activity:
public class MainActivity extends Activity {
private int i = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn1 = (Button) findViewById(R.id.btn1);
final Button btn2 = (Button) findViewById(R.id.btn2);
btn2.bringToFront();
final Button btn3 = (Button) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ((i % 2) == 0) {
final Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setText("BUTTON_BLACK_NEW");
btn1.bringToFront();
} else {
final Button btn2 = (Button) findViewById(R.id.btn2);
btn2.setText("BUTTON_GREY_NEW");
btn2.bringToFront();
}
++i;
}
});
}
#Override
protected void onResume() {
super.onResume();
if ((i % 2) == 0) {
final Button btn1 = (Button) findViewById(R.id.btn1);
btn1.bringToFront();
} else {
final Button btn2 = (Button) findViewById(R.id.btn2);
btn2.bringToFront();
}
++i;
}
}
Layout:
<RelativeLayout 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"
tools:context=".MainActivity"
android:id="#+id/main_layout">
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:text="BUTTON_BLACK"
android:textColor="#android:color/white" />
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:text="BUTTON_GREY"
android:textColor="#android:color/black" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHANGE_Z"
android:layout_below="#+id/btn2" />
</RelativeLayout>

Categories

Resources