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);
}
}
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)
I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible
This is activty_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<EditText
android:hint="ADD ITEMS"
android:layout_margin="10dp"
android:id="#+id/etItems"
android:textSize="24sp"
android:textColor="#android:color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
<Button
android:layout_margin="10dp"
android:id="#+id/btnAdd"
android:text="ADD"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvItems"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6" />
</LinearLayout>
This is MainActivity.Java
package com.example.todolist_09082020;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> items;
Button btnAdd;
EditText etItems;
RecyclerView rvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItems = findViewById(R.id.etItems);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList<>();
final ItemAdapter adapter = new ItemAdapter(items);
rvItems.setLayoutManager(new LinearLayoutManager(this));
rvItems.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = etItems.getText().toString();
if(!text.equals("")){
items.add(text);
adapter.notifyDataSetChanged();
}
etItems.setText("");
}
});
}
}
I tried solutions from other posts related to this, but this could not be solved.
edit text shrinked
Change layout_height of EditText and its parent LinearLayout to wrap_content. It should solve your issue
Your problem is the linear layout holding the text and button height. There's several things you can do, but a minimal way to keep it from collapsing is to change
android:layout_height="0dp"
to
android:layout_height="wrap_content"
You'll probably also see nicer results if you additionally change the text and button heights to wrap_content and choose a fixed font size (24sp looked nice) for your button text.
Here is my MainActivity.java code :
package com.dg.buttontest3;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("dg","button was clicked");
}
}
And here is the activity_main.xml code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="18dp"
android:text="Button" />
</LinearLayout>
I am getting the following error while running this code on Eclipse ADT : button1 cannot be resolved or is not a field.
I don't understand what is wrong with my code. Please help.
You haven't posted your XML code, but as this code seems to be working properly:
button = (Button)findViewById(R.id.button1);
I'd guess that in your activity_main.xml you never create something called button1, or that you don't use the #+id/ prefix.
It should look something like:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"/>
Just realised something else, you need to change your LinearLayout opening tag so it has a >.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
That might cause the button to never properly be recognised.
I have a simple Activity/ LinearLayout combination that has two buttons and changes the background colour when each button is clicked. The code is as below
It appears to me that there is a lot of high-level duplication of code. For example, I should not need to define and make variables out of the buttons and the LinearLayout background in my Java file. When I express my intention to couple them, I should be getting them automatically
So, lines like btnBlue = (Button) findViewById(R.id.btnBlue); should not be necessary. A good framework should allow me to use some conventions (like give me a Java variable btnBlue if my resource file #+id/ resource is named as btnBlue, etc
Where can I find a framework that achieves these for me in Android programming?
Thanks
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
android:weightSum="1"
android:id="#+id/background">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Linear Layout Tutorial"
android:textColor="#ff33ff"
android:textSize="32sp"
/>
<Button
android:id="#+id/btnGreen"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:text="Change to Green"
android:layout_weight="0.07" />
<Button
android:id="#+id/btnBlue"
android:layout_width="400sp"
android:layout_height="wrap_content"
android:text="Change12 to Blue"
android:layout_weight="0.19" />
</LinearLayout>
Activity Java code
package com.example.xxx.testandroid;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private final String TAG = "TKT";
LinearLayout background;
Button btnGreen, btnBlue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.som_linear);
Log.d(TAG, "onCreate");
background = (LinearLayout) findViewById(R.id.background);
btnBlue = (Button) findViewById(R.id.btnBlue);
btnGreen = (Button) findViewById(R.id.btnGreen);
btnGreen.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// click button code here
background.setBackgroundColor(Color.parseColor("#00ff00"));
}
});
btnBlue.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// click button code here
background.setBackgroundColor(Color.parseColor("#006699"));
}
});
}
}
ButterKnife by Jake Wharton seems a good solution for your problem.
I'm new to android and am currently attempting to create a simple application which calculates the area of a rectangle.
When the calculation is performed, the result is shown on the TextView specified.
This is my current code for calculating the area of the rectangle:
package org.me.myandroidstuff;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.app.Activity;
import android.os.Bundle;
public class CalculateRectangleArea2Activity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText length = (EditText) findViewById(R.id.txtLength);
EditText width = (EditText) findViewById(R.id.txtWidth);
TextView result = (TextView) findViewById(R.id.lblResult);
Button calculate = (Button) findViewById(R.id.btnCalculate);
calculate.setOnClickListener(this);
}
public void onClick(View v){
EditText length = (EditText) findViewById(R.id.txtLength);
EditText width = (EditText) findViewById(R.id.txtWidth);
calculateArea(length.getText().toString(), width.getText().toString());
}
private void calculateArea(String clength, String cwidth){
TextView result = (TextView) findViewById(R.id.lblResult);
int area = Integer.parseInt(clength)*Integer.parseInt(cwidth);
result.setText(area);
}}
Here is my main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="#+id/txtLength"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
<EditText
android:id="#+id/txtWidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
<TextView
android:id="#+id/lblResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btnCalculate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
When clicking the button to calculate, the application has the error "The application calculateRectangleArea2 has stopped unexpectedly. Please try again."
I believe this has something to do with NullPointerException, though attempting to trace the error through logCat and breakpoints hasn't helped.
Any help would be hugely appreciated :), thanks!
Try using:
result.setText(String.valueOf(area));
instead of:
result.setText(area);
setText() expects a String or CharSequence argument, while you're passing it an int. This is likely causing your crash.
Also, you don't need to get a reference to each View over and over again. Assigning them once to an instance variable in onCreate() is enough.