I have a simple app that has a Button and a TextView, when I try screen rotation, the TextView stays same while the Button disappears as below.
The Button comes back when I rotate the screen again.
I couldn't find a solution, is this a possible bug?
1- Why does this happening, I guess it's about onCreate() state, but why does TextView stays while the Button disappears?
2- How to solve this issue
3- I'm using Bundle to save the number, is it related to this problem?
I'm using the latest version of Android Studio. Here is my code
MainActivity.java
package com.example.kalicidegiskenler;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView t1;
Button b1;
int say;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView) findViewById(R.id.text1);
b1 = (Button) findViewById(R.id.button);
if (savedInstanceState != null){
say = savedInstanceState.getInt("sayac");
t1.setText(String.valueOf(say));
}
else {
say = 0;
}
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
say ++;
t1.setText(String.valueOf(say));
}
});
}
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("sayac", say);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.12" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="159dp"
android:layout_marginLeft="159dp"
android:layout_marginBottom="513dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
It is because the button is drawn out of screen; somewhere above the toolbar.
With app:layout_constraintBottom_toBottomOf used in combination with a padding of 513dp, the button falls out of the screen width (or landscape height) of 320 dp.
If you align it directly under the text, it will be always visible:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.12" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/text1"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Button" />
</androidx.constraintlayout.widget.ConstraintLayout>
Pro-tip: Google advises to use multiples of 8dp for paddings, margins and sizes. This scales well on different screen sizes and it is aesthetically pleasing if views fall in a grid.
Related
I am a beginner to android. I want to slide a slidebar in a horizontal linear layout but when I run the code, the application ends giving error FallingBall keeps stopping
My xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
android:weightSum="100"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="72"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8"
android:orientation="horizontal">
<ImageView
android:id="#+id/slideBar"
android:layout_width="100dp"
android:layout_height="match_parent"
app:srcCompat="#android:drawable/bottom_bar" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:orientation="horizontal">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/leftBut"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/left_but"
android:onClick="hitLeft"/>
<ImageButton
android:id="#+id/rightBut"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/right_but"
android:onClick="hitRight"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
My java file is like this:
package com.example.fallingball;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView s = findViewById(R.id.slideBar);
float x = s.getX();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
void hitLeft(View view){
x--;
s.setX(30);
}
void hitRight(View view){
x++;
s.setX(40);
}
}
Note: leftBut and rightBut are two Image button and I have copied the images to the drawable folder so if you are trying this code on your system, make sure you adjust this else it will show you Image not found or so...
In MainActivity.java you should initialize your value into onCreate() method
ImageView s;
float x;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
s = findViewById(R.id.slideBar);
x = s.getX();
}
Well, I found my own mistake. The thing what hossein answered earlier was right but the application was still crashing. Actually the problem was coming that I was triggering the methods hitLeft and hitRight which were not initialized as public. That was the mistake and now it is working alright...
I am new to Android Studio. I am following a video tutorial to make a TODO app. I was following this video
https://www.youtube.com/watch?v=kpHVtSvdeOI
At 14:44, I typed "fab", and it is in red color. I tried "Show Context Actions" but nothing works. I followed the exact same steps that he did but I still have that problem.
Here is the code:
package com.example.medicinereminder;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import com.example.medicinereminder.Utils.DataBaseHelper;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class ReminderActivity extends AppCompatActivity {
private RecyclerView mRecyclerview;
private FloatingActionButton fab;
private DataBaseHelper myDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
mRecyclerview = findViewById(R.id.recyclerView);
fab = findViewById(R.id.fab);
myDB = new DataBaseHelper(ReminderActivity.this);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
And a screenshot
Here is the XML code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".ReminderActivity">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medicine Reminder List"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.042" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<Button
android:id="#+id/addBtn"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:foreground="#drawable/plus"
android:text="Button"
app:backgroundTint="#00FDFDFD"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.977"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recyclerView"
app:layout_constraintVertical_bias="0.957" />
</androidx.constraintlayout.widget.ConstraintLayout>
If you are using the Floating Action button(dependency needed) then there must be floating action in XML
then only this line work
private FloatingActionButton fab;
And use this instead of Button in XML
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_my_icon"
android:contentDescription="#string/submit"
android:layout_margin="16dp" />
But if you are using the normal button in XML
and you are giving
<Button
android:id="#+id/addBtn"
so you need to declare like this
private Button btnadd;
then to read that button you can do
btnadd = findViewById(R.id.addtn);
For better understand refer this
I am trying to get Glide to display a local jpeg file in an ImageView.
I have created this simple example that shows my problem. Normally, I obtain the URI programatically, but for this example I am just using the hard coded uri string from one of the files I am trying to display. When I run it, the ImageView is empty.
package com.example.g1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.image);
Glide.with(this)
.load("content://org.chromium.arc.file_system.fileprovider/download/assets/images/DSC01062.JPG")
.into(imageView);
}
}
And here is the layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<ImageView
android:id="#+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="356dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image" />
</androidx.constraintlayout.widget.ConstraintLayout>
Any idea on what I am doing wrong?
My 1st Activity java file source code is not working the way I want. I can not seem to get the animation to run at the start of my 1st activity. I can only get it to run after the 2nd Java Activity by physically going back to the 1st activity by hard pressing back on the Android keyboard.
package com.demotxt.droidsrce.welcomescreen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
public class WelcomeActivity extends AppCompatActivity {
LinearLayout l1, l2;
Button btnsub;
Animation uptodown, downtoup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
btnsub = (Button) findViewById(R.id.buttonsub);
btnsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NextActivity();
}
});
}
public void NextActivity() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
uptodown = AnimationUtils.loadAnimation(this, R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);
l1.setAnimation(uptodown);
l2.setAnimation(downtoup);
}
}
This is my XML File for the 1st Activity and everything here is also running smoothly, but I have the same problem. There seems to be an issue running the java and XML script backward and forward to run the animation both ways.
<?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"
tools:context="com.demotxt.droidsrce.welcomescreen.WelcomeActivity"
android:orientation="vertical"
android:background="#drawable/background">
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:text="Welcome to"
android:textColor="#color/lightorange"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="398dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text=" BLUEY AUSTRALIA"
android:textAlignment="textStart"
android:textColor="#android:color/holo_blue_light"
android:textColorHighlight="#android:color/holo_blue_bright"
android:textColorHint="#android:color/holo_blue_bright"
android:textColorLink="#android:color/holo_blue_bright"
android:textSize="30sp" />
<ImageView
android:id="#+id/Bluey_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/bluey_logo" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="#+id/l2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#drawable/spaceullustration"
android:orientation="vertical">
<Button
android:id="#+id/buttonsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstyle"
android:text="JOIN BLUEY"
android:textColor="#color/bluey"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
try change the order
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
uptodown = AnimationUtils.loadAnimation(this, R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);
l1.setAnimation(uptodown);
l2.setAnimation(downtoup);
startActivity(intent); //<<<<<<<<<<<<<<,
**I am making some app and cant see a button that I have created (this is when running the app on my phone) ,but if I run it on the emulator I can see everything - the button that I can't see on my phone is the clicked button (its id is "startBtn").
**
Here is my layout :
<?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:id="#+id/activity_main"
android:background="#798"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="unleashed.myprefs.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="hello"
android:background="#ff14"
/>
<Button
android:id="#+id/startBtn"
android:text="start"
android:layout_below="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addOne"
/>
<Button
android:id="#+id/resetBtn"
android:text="restart"
android:layout_below="#+id/textView"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="restart"
/>
</RelativeLayout>
And this is my main activity
package unleashed.myprefs;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button restartNums;
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("main", MODE_PRIVATE);
}
private int getNum(){
return prefs.getInt("num",1);
}
private void setNum(int num){ // put the number into my shared preference
prefs.edit().putInt("num", num).apply();
}
public void addOne(View v) { //adding number to the starting button
int num = getNum();
((Button)v).setText("Clicked " + num);
setNum(num + 1);
}
public void restart(View v){
restartNums = (Button) findViewById(R.id.startBtn);
prefs.edit().remove("num").apply();
restartNums.callOnClick();
}
}
I will appreciate any help.
AlignParentEnd should be used in ResetButton.( Not sure )
(Alternative) If you trying to have two buttons in the same row, you can try this layout..
`
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="hello"
android:background="#ff14"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation = "horizontal"
android:weightsum="2"
android:layout_below="#+id/textView">
<Button
android:id="#+id/startBtn"
android:text="start"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="addOne"
/>
<Button
android:id="#+id/resetBtn"
android:text="restart"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="restart"/>
`