I would like to use viewSwitcher to switch between two layouts. It works with a button, but I would like to switch with touching the screen. I've found an onTouchListener example and i tried to implemet some kind of similar, but I did not manage to.
In my main.xml i got one Linear Layout which contains a ViewSwitcher and two other LinearLayout. These are the layouts i want to switch.
here it is:
<ViewSwitcher
android:id="#+id/viewSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg"
android:orientation="vertical" >
<ImageView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/titlebar" />
<Button
android:id="#+id/somebut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30px"
android:background="#drawable/but" />
</LinearLayout>
<LinearLayout
android:id="#+id/secondlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second layout" >
</TextView>
</LinearLayout>
</ViewSwitcher>
and my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;
public class SwitcherActivity extends Activity {
private float downXValue;
private View firstView;
private View SecondView;
private ViewSwitcher vs;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vs = (ViewSwitcher)findViewById(R.id.viewSwitcher);
firstView= findViewById(R.id.firstlayout);
secondView = findViewById(R.id.secondlayout);
LinearLayout first = (LinearLayout) findViewById(R.id.firstlayout);
first.setOnTouchListener((OnTouchListener) this);
}
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()){
case MotionEvent.ACTION_DOWN:{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
if (vs.getCurrentView() != myFirstView)vs.showPrevious();
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
if (vs.getCurrentView() != mySecondView)
vs.showNext();
}
break;
}
}
return true;
}
}
My problem is, that i got ClassCastException in the line which contains the following:
first.setOnTouchListener((OnTouchListener) this);
Your activity has to implement OnTouchListener so that "this" can be used as the listener. Right now it's trying to cast an Activity to an OnTouchListener which is what's throwing the ClassCastException.
You also need to override the OnTouchListener methods to customize the way the touches are handled.
Related
I want the fab to rotate each time i click on it. But after trying, i achieved the rotation effect. But the problem i'm having is that it only work once. i.e on app startup.
Please help, i'm a beginner in this.
Below is the complete code i used.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
android:orientation="horizontal"
tools:context=".MainActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:backgroundTint="#color/design_default_color_background"
app:fabCradleMargin="15dp"
app:menu="#menu/app_bar_layout"
app:fabCradleVerticalOffset="20dp"
app:fabCradleRoundedCornerRadius="40dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_generate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#1D6837"
android:src="#drawable/generate"
app:layout_anchor="#id/bottom_app_bar"
android:contentDescription="generate"
android:clickable="true"
app:fabCustomSize="80dp"
app:maxImageSize="45dp"
android:layout_marginBottom="20dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import static androidx.core.view.ViewCompat.animate;
public class MainActivity extends AppCompatActivity {
private FloatingActionButton generateFab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generateFab = findViewById(R.id.fab_generate);
generateFab.setOnClickListener ( new View.OnClickListener() {
#Override
public void onClick(View v) {
final OvershootInterpolator interpolator = new OvershootInterpolator();
animate(generateFab).
setInterpolator(interpolator).
setListener(null).
rotation(360f).
withLayer().
setDuration(300).
withStartAction(null).
start();
}
});
}
}
Basically, I added onClickListener to my floating action button inside the onCreate method
Try this:
animate(generateFab).
setInterpolator(interpolator).
setListener(null).
rotation(generateFab.getRotation() + 360f).
withLayer().
setDuration(300).
withStartAction(null).
start();
Notice the line rotation(generateFab.getRotation() + 360f).
You rotate the view 360 degrees on the first click so this changes the view elements rotation attribute to 360, and because its already 360 (after the first rotation) it wont rotate again. So instead you want to keep adding 360 degrees to the current views rotation and it will work.
Update:
As per comment by #AHoneyBustard you could instead of rotation(...) use:
rotationBy(360f)
This is my very first post here and i am very new to programming, ive started a project in android studio but i've become stuck on a couple of things because my lack of knowledge.
I have three different tabs and i want to make the calculation on the second one.
So my question is how do i calculate data in columns inside of the gridlayout?
My component tree looks like this in tab2:
RelativeLayout>Scrollview>Gridlayout.
I have inserted several textedits on column 2 and 3 and my goal is to make calculations based on their input and then insert them as results at the bottom of the tab.
2nd column includes numbers only and the 3rd column includes variables.
I want to make the variables the user enters on the 3rd column equal to numbers, for example A=20 B=17.5 etc
So when its calculating its automatically changing the variable to the
equal number.
How do i proceed?
Tab2 java class:
package com.example.minmerit;
/**
* Created by lukin on 2017-06-14.
*/
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab2Betyg extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab2betyg, container, false);
return rootView;
}
}
Tab2 layout xml:
<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="450dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.minmerit.MainActivity$PlaceholderFragment">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="600dp"
android:layout_below="#id/textview">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/POÄNG"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_row="1"
android:ems="10"
android:fontFamily="monospace"
android:inputType="number"
android:text="100"
android:textAlignment="center"
android:textColor="#android:color/black" />
First, give your EditTexts some proper IDs, like
<EditText
android:id="#+id/number_1"
...
Then in eg. onViewCreated make changeListeners:
EditText number_1 = (EditText) findViewById(R.id.number_1);
number_1.addTextChangedListener( changeListener );
// change listeners for other fields
TextWatcher changeListener = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
int a = Integer.parseInt(s);
// do something with "a"
}
});
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I wrote this code according to the video tutorial found here
about Android Fragments, but I can't understand how the flow of execution runs through these codes. As in the tutorial I have made two fragments alone with the Main Activity. The whole program works fine, but I can't understand how the execution flow goes. Please be kind enough to explain me. Here are my source codes, I am
really sorry as they are too long, I posted all of my code. This is the Main Activity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity implements top_layout_fragment.TopSelectionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//overiding the method from the interface
#Override
public void createMeme(String top, String bottom) {
bottom_layout_fragment bottomLayoutFragment = (bottom_layout_fragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
bottomLayoutFragment.setText(top, bottom);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
this is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.kasun.fragments.top_layout_fragment"
android:id="#+id/fragment"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:layout="#layout/top_layout_fragment" />
<fragment
android:layout_width="320dp"
android:layout_height="320dp"
android:name="com.example.kasun.fragments.bottom_layout_fragment"
android:id="#+id/fragment2"
tools:layout="#layout/bottum_layout_fragment"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
this is the top_layout_fragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Button;
import android.app.Activity;
import java.util.ServiceConfigurationError;
public class top_layout_fragment extends Fragment {
private static EditText firstText, secondText;
TopSelectionListener activityCommand;
public interface TopSelectionListener {
public void createMeme(String top, String bottom);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCommand = (TopSelectionListener) activity;
}
catch(ClassCastException e){
throw new ClassCastException (activity.toString());
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_layout_fragment, container, false);
firstText = (EditText) view.findViewById(R.id.firstText);
secondText = (EditText) view.findViewById(R.id.secondText);
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
}
);
return view;
}
public void buttonClicked(View view) {
activityCommand.createMeme(firstText.getText().toString(), secondText.getText().toString());
}
}
this is top_layout_fragment.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">
<EditText
android:id="#+id/firstText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="320dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
<EditText
android:id="#+id/secondText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="320dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="#id/firstText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/secondText"
android:layout_marginTop="20dp"
android:text="#string/button_Texg"
android:id="#+id/button" />
</RelativeLayout>
this is the bottom_layout_fragment.java
package com.example.kasun.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
public class bottom_layout_fragment extends Fragment {
private TextView topText,bottomText;
// need to overide the onCreateViewMethod
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.bottum_layout_fragment,container,false);
topText=(TextView)view.findViewById(R.id.topText);
bottomText=(TextView)view.findViewById(R.id.bottomText);
return view;
}
public void setText(String topText,String bottomText){
this.topText.setText(topText);
this.bottomText.setText(bottomText);
}
}
this is the bottom_layout_fragment.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:background="#drawable/image1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/topText"
android:layout_marginTop="34dp"
android:textColor="#FFF"
android:gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/bottomText"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/topText"
android:layout_alignStart="#+id/topText"
android:layout_marginBottom="35dp"
android:textColor="#FFF"
android:gravity="center_horizontal" />
</RelativeLayout>
This is set up so you have one activity displaying two fragments. You can see both fragments are included in the activities layout file.
The top fragment needs to communicate with the bottom one to update its text and does so through an interface called TopSelectionListener. You will notice that your activity implements that interface. The other important part is the top fragment keeps a reference to your activity using the interface. You can see that in the top fragment's onAttach method. The activity displaying this fragment must implement the TopSelectionListener interface or else you will get a ClassCastException and your application will crash.
activityCommand = (TopSelectionListener) activity;
When the button is clicked in the top fragment it will call activityCommand.createMeme(...) which calls the createMeme method in your activity.
The createMeme method then looks up a reference to your bottom Fragment and calls the setText method on it to update the text it shows.
There is another example how this works in the Android documentation.
I just signed up in order to ask for help.
I'm doing an application which displays an image gallery. When you click any of the images, it must open the image and show it.
There are two activities: 'Galeria' and 'Imagen'. 'Galeria' displays the image gallery and 'Imagen' displays the image clicked.
When an image is clicked, I create the second activity using an Intent as seen in the code below.
Galeria.java code:
package com.ejemplo.galeria;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
public class Galeria extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//Called when an image is clicked (I use ImageButtons)
public void openImage(View view){
Intent i = new Intent(this,Imagen.class);
i.putExtra("IMAGEN", view.getId());
startActivity(i);
}
}
The method 'openImage()' is called when any of the ImageButton is pushed (Whatever button you press, the same method is called). Here is the 'main.xml' code:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Galeria" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id = "#+id/scrollview">
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/imagen1"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:onClick="openImage"
android:src="#drawable/imagen1"></ImageButton>
<ImageButton
android:id="#+id/imagen2"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:onClick="openImage"
android:src="#drawable/imagen2"></ImageButton>
<ImageButton
android:id="#+id/imagen3"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:onClick="openImage"
android:src="#drawable/imagen3"></ImageButton>
<ImageButton
android:id="#+id/imagen4"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:onClick="openImage"
android:src="#drawable/imagen4"></ImageButton>
<ImageButton
android:id="#+id/imagen5"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:onClick="openImage"
android:src="#drawable/imagen5"></ImageButton>
<ImageButton
android:id="#+id/imagen6"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:onClick="openImage"
android:src="#drawable/petergriffin"></ImageButton>
<ImageButton
android:id="#+id/imagen7"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:onClick="openImage"
android:src="#drawable/gato"></ImageButton>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
So, this is the code of the 'Image' class:
package com.ejemplo.galeria;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class Imagen extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent i = getIntent();
int id = i.getIntExtra("IMAGEN", -1);
if(id != -1){
ImageView iv = (ImageView) findViewById(R.id.imageV1);
iv.setImageResource(id); //Error
setContentView(R.layout.imagen);
}
}
}
And this is the XML code of 'imagen.xml' file:
<?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" >
<ImageView
android:id="#+id/imageV1"
android:layout_height="wrap_content"
android:layout_width="wrap_content" ></ImageView>
</LinearLayout>
So, when I modify the source of the ImageView, my applications closes unexpectedly, and I have no idea why. I've been looking for help but I found nothing, so I wish anyone can help me, I'm getting mad.
Thank you!
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
OK I FOUND THE ISSUE
I changed the parameter in the method 'setImageResource(id)' for R.drawable.imagen1 and constructed the switch case statement.
But it kept closing unexpectedly!!!
So, in the method 'onCreate()' of 'Imagen' class, I just moved the line 'setContentView(R.layout.imagen)' from the end of the method to the beginning.
PROBLEM SOLVED :)
So here:
Intent i = new Intent(this,Imagen.class);
i.putExtra("IMAGEN", view.getId());
startActivity(i);
you just put view id in intent, but after:
int id = i.getIntExtra("IMAGEN", -1);
if(id != -1){
ImageView iv = (ImageView) findViewById(R.id.imageV1);
iv.setImageResource(id); //Error
setContentView(R.layout.imagen);
}
you try assign this R.id to image resource, this is wrong, because setImageResource expecting something like R.drawable.my_image. Please follow javadoc.
public void openImage(View view){
Intent i = new Intent(this,Imagen.class);
i.putExtra("IMAGEN", view.getId());
startActivity(i);
}
The above view.getId() will return the id of the particular imagebutton. This is no way related to the drawable resource. So when you setImage() in the other activity it will indeed through a error as you are trying to set a button to a image view.
Instead you can construct a switch case statement to send the corresponding drawable id to the other activity.
As others said - you are trying to set View id as ImageResource - those are different two things; so to fix your problem I would suggest something like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imagen); // before findViewById
Intent i = getIntent();
int id = i.getIntExtra("IMAGEN", -1);
if(id != -1){
ImageView iv = (ImageView) findViewById(R.id.imageV1);
switch (id) {
case R.id.imagen1: // first one
iv.setImageResource(R.drawable.imagen1);
break;
// repeat for others
} // switch close
} // if
} // onCreate
anyway it seems to me like you would benefit from using ListView / GridView together w/ some ArrayAdapter
I have a simple jokes application. When you press the button you get new joke. If the previous one was bigger than the screen you can scroll it down. If you go down to the bottom and you go to the next joke, you are transfered to the bottom of the newly generated joke, but i want it to go to the top, and automatically display the start of the joke. How can i do this ?
I assume it would be done via the java code.
Thank you for your time.
Use the scrollTo(int x, int y) method, i like to have ScrollView around my TextView but i think the same thing will work for a TextView only. hope you understand!
Rolf
Example
xml:
<?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:orientation="vertical" >
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="130dp" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="long\n\n\n\n\n\n\n\n long\n\n\n\n\n\n\n very text here!" />
</ScrollView>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="joke" />
</LinearLayout>
java:
package org.sample.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class AutoscrollActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button new_joke;
private TextView joke;
private ScrollView scroll;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new_joke = (Button) this.findViewById(R.id.button);
new_joke.setOnClickListener(this);
joke = (TextView) this.findViewById(R.id.text);
scroll = (ScrollView) this.findViewById(R.id.scroll);
}
#Override
public void onClick(View v) {
joke.setText("Long\n\n\n\n\n\n\n\n joke \n\n\n\n\n\n\n\nlong joke joke");
scroll.scrollTo(0, 0);
}
}