I'm a beginner, but I want to learn and I'm developing my first app!!I would also like to use the "toaster" function in the app. Unfortunately no longer works !!
Not only in my app, but no matter where I want to use it. Should I reinstall android studio?
Thank you, Stefan
Hi, no error message! Just not work!!
MainActivity:
package de.havadinagy.toaster_test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void info(View view){
Toast.makeText(this,"Only Toastertest!!" ,Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main :
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="243dp"
android:layout_height="172dp"
android:onClick="info"
android:text="ToasterTest"
android:textColor="#color/black"
android:textSize="24sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
try this
Toast.makeText(requireContext(), "message", Toast.LENGTH_SHORT).show()
do try this
try
public void info(View view){
Toast.makeText(MainActivity.this,"Only Toastertest!!" ,Toast.LENGTH_LONG).show();
}
and change "info"
Related
I am a new comer to Android app.When I run an app with buttons in it in the emulator, the buttons are shown after the app is completed, so onclick etc is not working.
It shows a blank white screen
This same code is from a youtube video and there it is working fine. Tried to debug but couldnt make much headway.
Pasting the code :
package com.example.weatherapiapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn_cityID,btn_getWeatherByID,btn_getWeatherByName;
EditText et_dataInput;
ListView lv_weatherReport;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//assign values to each control on the layout
btn_cityID = findViewById(R.id.btn_getCityID);
btn_getWeatherByID = findViewById(R.id.btn_getWeatherByCityID);
btn_getWeatherByName = findViewById(R.id.btn_getWeatherByCityName);
et_dataInput = findViewById(R.id.et_dataInput);
lv_weatherReport = findViewById(R.id.lv_weatherReports);
//click listeners for each button
btn_cityID.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked me 1.", Toast.LENGTH_SHORT);
}
});
btn_getWeatherByID.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked me 2.",Toast.LENGTH_SHORT);
}
});
btn_getWeatherByName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked me 3.",Toast.LENGTH_SHORT);
}
});
}
}
The activity_main.xml is as follows:
<?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">
<Button
android:id="#+id/btn_getCityID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get City ID"
app:layout_constraintEnd_toStartOf="#id/btn_getWeatherByCityID"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_getWeatherByCityID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weather by ID"
app:layout_constraintEnd_toStartOf="#id/btn_getWeatherByCityName"
app:layout_constraintStart_toEndOf="#+id/btn_getCityID"
app:layout_constraintTop_toTopOf="#id/btn_getCityID" />
<Button
android:id="#+id/btn_getWeatherByCityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weather by Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/btn_getWeatherByCityID"
app:layout_constraintTop_toTopOf="#id/btn_getWeatherByCityID" />
<EditText
android:id="#+id/et_dataInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="City Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_getCityID" />
<ListView
android:id="#+id/lv_weatherReports"
android:layout_width="409dp"
android:layout_height="628dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/et_dataInput" />
</androidx.constraintlayout.widget.ConstraintLayout>
Can you help in letting me know what may be the issue?
you forgot .show() in toast
it should be
Toast.makeText(getApplicationContext(),"You clicked me 1.", Toast.LENGTH_SHORT).show();
I have this xml file where there are 4 image views, with one logo at the centre and the other three are just circles with radius greater than the previous one surrounding the logo. I want to display this as an animation until my page gets loaded. In want that the three circles appear one after the other in loop in a sequence. How can I do that ?
<?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/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/center" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/anim1" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/anim2" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/anim3" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have also attached a picture for a better visualization if needed :
enter image description here
#Srijan,
There are more ways to achieve this.
Please refer to objectAnimator or animation-list drawable for more details.
As a quick answer to your problem, please see the below sample code.
package com.jrh.testanim;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
int count= 0;
int imgArr[] = new int[]{
R.drawable.circle1,
R.drawable.circle2,
R.drawable.circle3,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = (ImageView) findViewById(R.id.animation_imageview);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
imageView.setImageResource(imgArr[count]);
handler.postDelayed(this, 500);
count++;
if (count > 2) {
count = 0;
}
}
}, 500);
}
}
Please check this and mark accepted, if it solves your problem.
Thanks
JRH
Hi this is my first question here.
MainActivity.java
package com.example.kerry.chellenge04;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.text);
}
public void button(View view){
String string=name.getText().toString();
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
}
}
activity_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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="288dp"
android:layout_height="346dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="8dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button"
android:text="Button"
tools:layout_editor_absoluteX="58dp"
tools:layout_editor_absoluteY="401dp" />
</LinearLayout>
It's very simple code. When I press the button, the app is supposed to show you what you typed in edittext, but it stops. Can you guys please tell me what the problem is?
Try this one
You need to assign an id to your EditText:
<EditText
android:id="#+id/text"
android:layout_width="288dp"
android:layout_height="346dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="8dp" />
You need to assign an id to your EditText:
android:id="#+id/text"
otherwise this line:
name=(EditText)findViewById(R.id.text);
will assign null to name.
So when you try to access any property or method of the object name
a Null Pointer Exception will be thrown and your app will crash.
you should you the activity context rather than the application context.
try to replace
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
with
Toast.makeText(view.getContext(), string, Toast.LENGTH_LONG).show();
then, as already said, you should add the id, but I believe you did, as you can access the generated R.id.text
This question already has answers here:
"cannot resolve symbol R" in Android Studio
(100 answers)
Closed 5 years ago.
I'm new to android and saw other people having the same question but the solutions didn't work.
i tried multiple times to clean, sync and build the project after changes but every time is says that it can't resolve R
adding: import nl.test123.R; didn't work because than the next error popsup:Error:attribute 'nl.test123.soundboard:layout_background' not found.
also this didn't work
import android.R
this is the code:"MainActivity.java"
package nl.test123.testsoundboard;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
//import nl.test123.R;
public class MainActivity extends AppCompatActivity {
MediaPlayer mysound1;
MediaPlayer mysound2;
MediaPlayer mysound3;
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_top:
// mTextMessage.setText(R.string.title_top);
return true;
case R.id.navigation_latest:
// mTextMessage.setText(R.string.title_latest);
return true;
case R.id.navigation_favorite:
// mTextMessage.setText(R.string.title_favorite);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mysound1 = MediaPlayer.create(this, R.raw.bird);
mysound2 = MediaPlayer.create(this, R.raw.bomb);
mysound3 = MediaPlayer.create(this, R.raw.dog);
}
public void sound1(View view){
mysound1.start();
}
public void sound2(View view){
mysound2.start();
}
public void sound3(View view){
mysound3.start();
}
}
can anyone see what i'm doing wrong or forgetting to do.
see the xml below:"activity_main.xml"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="nl.marvinboontjes.cucumbersoundboard.MainActivity">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="92dp"
android:layout_marginTop="128dp"
android:text="#string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_light"
android:onClick="Sound1"
android:text="Bird"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp"
tools:ignore="MissingConstraints" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sound2"
android:text="Bomb"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="16dp"
tools:ignore="MissingConstraints" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onclick="sound3"
android:text="Dog"
tools:layout_editor_absoluteX="269dp"
tools:layout_editor_absoluteY="16dp"
tools:ignore="MissingConstraints" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:layout_background="#color/colorPrimary"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemTextColor="#drawable/nav_item_color_state"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
Error images:
picture1
picture2
use yourpackagename.R.
in your case it will be nl.test123.testsoundboard.R
I think you have an error in your layout. BottomNavigationView doesn't have app:layout_background attribute. remove it and rebuild again.
I have coded an app to learn about Custom List View. While the app has no errors, I have one issue. I have shown it in the images as follows.
IMAGE 1
IMAGE 2
The problem is I have to scroll a long way before i can find the next list item.The list items do not appear one below the other.How to resolve this issue?
Here is my code:
package com.example.hp.customlistview;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int IMAGES[]={R.drawable.adsense, R.drawable.allo, R.drawable.chrome, R.drawable.firebase, R.drawable.youtube};
String[] NAMES={"AdSense","Allo","Chrome","Firebase","YouTube"};
String [] DESCRIPTIONS={"Money through ads","Video calling","Web Browser","Cloud Database","Video Streaming"};
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
CustomAdapter customAdapter=new CustomAdapter();
listView.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter{
#Override
public int getCount() {
Log.d("Length of Array ","The length is "+IMAGES.length);
return IMAGES.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater=(LayoutInflater)getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
view=layoutInflater.inflate(R.layout.customlayout, null);
Log.d("Image ID","The id is "+R.id.imageView);
ImageView imageView=(ImageView)view.findViewById(R.id.imageView);
TextView textView_name=(TextView)view.findViewById(R.id.textView);
TextView textView_desc=(TextView)view.findViewById(R.id.textView2);
if(imageView==null)
Log.d("NULL?","YES IT IS NULL");
else
Log.d("NULL?","NO IT IS NOT NULL");
imageView.setImageResource(IMAGES[i]);
textView_name.setText(NAMES[i]);
textView_desc.setText(DESCRIPTIONS[i]);
Log.d("Hello","Hello there "+textView_name.getText().toString());
return view;
}
}
}
customlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="120dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="86dp"
android:layout_height="91dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="161dp"
android:layout_height="39dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="28dp"
android:layout_marginTop="24dp"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView2"
android:layout_width="237dp"
android:layout_height="57dp"
android:layout_marginBottom="380dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="16dp"
android:layout_marginStart="44dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="39dp"
android:layout_height="39dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.hp.customlistview.MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="386dp"
android:layout_height="409dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Ok"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/listView" />
</android.support.constraint.ConstraintLayout>
To resolve this, i tried the following
1)android:layout_height="match_parent" to a specific height of 120 dp.
(It is in customlayout.xml)
But it did not work
2)Used some other layout, such as Absolute and Relative Layout, but image fills up the list content much beyond expected size
EDIT:
I have the list items displayed in a compact manner, as follows,
after suggestions by Ben P. in the comments below.
IMAGE 3
But the description is not appearing,although clearly i have set it programatically in MainActivity.java
I observed that setting android:layout_height to some custom value makes the description text view disappear in the preview.
If someone is downvoting,please give the reason for doing so. This will help me improve the quality of my questions.
Ok, I figured it out. Posting it for anyone who may have similar doubts in future.
Set layout height as 120dp or some finite value even before dragging and dropping items.
Now start drag and drop operation on this reduced layout.
Pretty simple i guess. This helped me resolve it.
Here it is:)
Image 5