How to show a notification icon in android studio - java

I am building an android application where I am letting the users to sign in using google also. When they sign in using google and do not register themselves using the registration page then there are certain information which are left out.I also have a screen which updates the profile information of the user.
I want to set an icon on the home screen(kind of a bell icon) which displays that there are some notification and when the user click on that the button it tells that some information are left to be added and it redirects them to the update profile page.
The icon I want is like the notification icon(bell) in youtube.
Is there any way I can do this?
EDIT :
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#drawable/grdnt"
tools:context=".MainActivity">
<TextView
android:id="#+id/welcome_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:textColor="#2AF598"
android:textSize="50dp"
android:layout_alignParentTop="true"
android:layout_marginTop="80dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/provide_food"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_below="#+id/welcome_text"
android:layout_marginTop="100dp"
android:layout_marginLeft="80dp"
android:gravity="center"
android:text="Do you have Food"
android:textSize="20dp"
android:background="#drawable/custombutton"
/>
<Button
android:id="#+id/need_food"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_below="#+id/provide_food"
android:layout_marginTop="50dp"
android:layout_marginLeft="80dp"
android:gravity="center"
android:text="Do you need Food"
android:textSize="20dp"
android:background="#drawable/custombutton"
/>
<Button
android:id="#+id/deliver_food"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_below="#+id/need_food"
android:layout_marginTop="50dp"
android:layout_marginLeft="80dp"
android:gravity="center"
android:text="Do you want to Deliver Food"
android:textSize="20dp"
android:background="#drawable/custombutton"
/>
</RelativeLayout>
MainActivity.java:
package com.example.helping_hands_individual;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button provide, need,deliver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
provide = (Button)findViewById(R.id.provide_food);
need = (Button)findViewById(R.id.need_food);
deliver = (Button)findViewById(R.id.deliver_food);
provide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Provide_food.class);
startActivity(intent);
}
});
}
}
Note: I am not using any custom toolbar, neither am I using any toolbar, my theme for the application is: NoActionBar

There are three ways to get that notification icon depending upon the theme you are using in your Activity.
1. No.ActionBar Theme: If you're not using any ActionBar or toolbar in your activity then you can create a View(ImageView or Button) in your Activity's layout file with Height and Width as same as the notification icon which is around 28dp and then position it at the top right corner of the screen. You can change the background image of the view according to the state you want.
2. Create your own toolbar: You can create your own toolbar and set the menu item in it. While creating the menu item make sure that you select showAsAction="always" this way your icon is always visible. here is a link for the reference
3.ActionBar Theme: If you're using the action bar theme then you can create a menu file with one item like in Option 2 and then override the onCreateOptionsMenu and inflate then inflate your menu in it.

Notification notification = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.build();

Related

Dismiss a child view without it executing the parent view OnCreate function

I have the following in my AndroidManifest.xml file:
<activity
android:name=".PickemAllPicksResultsActivity"
android:label="#string/title_activity_backActionBar"
android:parentActivityName=".PoolDetailsActivity"
android:screenOrientation="portrait" />
The above places a back button/arrow (in the action bar) in my child view in order to navigate back to the parent view.
I have the following layout for the child view:
<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=".PickemAllPicksResultsActivity" >
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.android.material.tabs.TabItem
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Weekly" />
<com.google.android.material.tabs.TabItem
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Overall" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tabLayout"
android:layout_centerHorizontal="true" />
</RelativeLayout>
When i click on the back button in the action bar the app crashes because the parent view is executing the onCreate which is calling a Firebase function:
// Get the Pool Info
poolReference = mFirebaseDatabase.getReference("PICKEMPOOLS").child(poolID);
and poolID is null.
Question, why does this back button trigger the OnCreate function, but if I press the back button on the Android Navigation is just dismisses the screen and goes to the previous view?
From my understanding, if you want to get back click the event implemented below will give you a callback when back pressed.
// This callback will only be called when MyFragment is at least Started.
OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
#Override
public void handleOnBackPressed() {
// Handle the back button event
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
// The callback can be enabled or disabled here or in handleOnBackPressed()

How to show a snakbar with Lottie animation in start from a recycler view adapter

I am making a chat app and I want to add a feature like telegram to copy links ,phone numbers and etc.. from long clicking on an auto link.I used this library to add long click listeners on a auto link.I implemented it successfully.But when I do this, i want to show a Lottie animation in the start like this but on long click of a link.
I tried many answer but I get an exception.I already made the layout for custom snakbar.It is given below
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="56dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="#dimen/_3sdp"
android:backgroundTint="#color/snakbar_background"
app:cardCornerRadius="#dimen/_4sdp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lottieSnakbar"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginEnd="#dimen/_10sdp"
app:lottie_rawRes="#drawable/copy"
app:lottie_autoPlay="true"
app:lottie_loop="false"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Text copied to clipboard"
android:gravity="center_vertical"
android:theme="?snackbarTextViewStyle"
tools:ignore="HardcodedText" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Now how can I achieve this?
You can use an interface in the adapter. And receive and show snakbar from activity
You could use localBroadcast and broadcast a local signal from the adapter and let the other Activity(probably receiver) handle snakbar.
AdapterClass{
LocalBroadcastManager localBroadcastManager = null;
void sendB(parms){
if(localBroadcastManager != null){
localBroadcastManager.sendBroadcast(Intent("NOTIFICATION_RECEIVER"))
}
onCreateViewHolder(){
localBroadcastManager = LocalBroadcastManager.getInstance(applicationContext)
}
onBindViewHolder(){
if(someCondition){
sendB(parms);
}
}
then receive it from an activity then call snakBar

Custom Facebook Share-Button, Android App

So I want to implement a custom Facebook share button into my Android app, but thus far I've only managed to use the native one, which I imported into my .xml file and made use of in my Java activity for that specific page.
My code looks like this (in .xml);
<com.facebook.share.widget.ShareButton
android:id="#+id/share_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Share"
android:layout_centerHorizontal="true"/>
And in my .java, outside onCreate();
private ShareButton shareButton;
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.setContentTitle("MyTitle")
.build();
Inside onCreate();
shareButton = (ShareButton)findViewById(R.id.share_btn);
shareButton.setShareContent(content);
How would I go about making use of a custom button that I've imported into XML? Using .setShareContent obviously doesn't work if it's not an instance of ShareButton. Thanks in advance!
I have found a solution. It's clear that facebook share will work on its own button click event. So you will have to explicitly call the View.performclick() method.
Here is how i have done this:
In my layout
<com.facebook.share.widget.ShareButton
android:id="#+id/share_btn_fb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:contentDescription="#string/share" />
<LinearLayout
android:id="#+id/share_btn"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="#drawable/google_share" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/share_google"
android:textColor="#color/green_color"
android:layout_marginLeft="5dp"
android:textSize="18sp" />
</LinearLayout>
Inside activity get refrences of these views.
ShareButton shareButton = (ShareButton) layout.findViewById(R.id.share_btn_fb);
LinearLayout shareFB = (LinearLayout) layout.findViewById(R.id.share_btn);
LinearLayout shareGoogle = (LinearLayout) layout.findViewById(R.id.share_google);
shareGoogle.setOnClickListener(this);
shareFB.setOnClickListener(this);
Inside onClick()
case R.id.share_btn :
shareButton.performClick();
break;
case R.id.share_btn_fb :
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentTitle("Content title")
.setContentDescription("App descr")
.setContentUrl(Uri.parse("some url"))
.build();
shareButton.setShareContent(content);
break;
Basically shareButton.performClick(); is doing the trick.

Why does my findViewById() not find my Id that I have introduced in my xml code?

I am very new to Android app development. But in one of my first projects I have come across an error. My findViewById() does not find an Id that has been introduced in the xml code.
Here is my Java code:
package com.example.fuzzylogic;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSubtract);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is my 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=".MainActivity" >
<TextView
android:id="#+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Your total is 0"
android:textSize="45dp" />
<Button
android:id="#+id/bAdd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDisplay"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="add one"
android:textSize="25dp" />
<Button
android:id="#+id/bSubtract"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/bAdd"
android:layout_below="#+id/bAdd"
android:layout_marginTop="23dp"
android:text="substract one"
android:textSize="25dp" />
</RelativeLayout>
My findViewById() does not find an Id that has been introduced in the
xml code.
So this may cause:
Errors in XML file(s) and then R.java is not correctly generated
You did not save all edited xml files(ctrl+s)
So make it sure that you don't have errors in your project and clean and rebuild your project.
First you must save the xml file and the rebuild it
When u add a new file or changes to resource file , you better rebuild the project ,it will modify your R.java file corresponding to the changes you have made
and in findViewById() the id that we are passing is not the id from your layout xml file . actually it's an integer representation of the resource objects in R.java file
Note : If you are using eclipse chose automatically build option selected
I had a same issue, the reason why i was getting the issue was that R class was not updated with the new Xml file resources that i had added. So i rebuild the project and boom the issue was resolved.
So rebuilding the project might help.

findViewById Returns An Error

findViewById gives out an error, I think it's because it's returning null. My app is forced to close.
SensorManager sensorManager=null;
TextView x= null;
TextView y= null;
TextView z= null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
x= (TextView) findViewById(R.id.tv1);
y= (TextView) findViewById(R.id.tv2);
z= (TextView) findViewById(R.id.tv3);
}
XML:
<TextView
android:id="#+id/tv1"
android:layout_x="10px"
android:layout_y="550px"
android:text="#string/x"
/>
<TextView
android:id="#+id/tv2"
android:layout_x="100px"
android:layout_y="550px"
android:text="#string/y"
/>
<TextView
android:id="#+id/tv3"
android:layout_x="200px"
android:layout_y="550px"
android:text="#string/z"
/>
Can someone please tell me what I'm doing wrong?
SOLVED!
I used the XML given by Sunny at the bottom! Thank you to all!
That's simple.... tv* is not in your layout!
Please post the layout, I am 100% sure it doesn't contain:
<TextView android:id="#+id/tv1"/>
<TextView android:id="#+id/tv2"/>
<TextView android:id="#+id/tv3"/>
Please also learn how to get a logcat before going further in android development! ;-)
Window -> Show View -> Logcat
yes above code working fine and xml file should be like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView>
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
yes that problem and if with xml is not done then
You import android.R;
so that it will give error if xml you have
remove that
and import PACKAGENAME.R;
First : Verify that your TextView Are present on your layout : main.xml :
<TextView android:id="#+id/tv1" .../>
<TextView android:id="#+id/tv2" .../>
<TextView android:id="#+id/tv3" .../>
Second : reBuild your project by following this steps : Project ==> Clean ==> Select your project and click OK .
The reason why this happens is :
findViewById() will search for the TextViews in the View hierarchy established by setContentView(R.layout.main). So if the TextViews aren't in main.xml you will get a null

Categories