Android Notes
Android Notes
webview.setWebViewClient();
webview.loadurl("Addresss");
Goto the manifests folder and then set the uses permission to the tag above the application tag.
View
It represents a rectangular area of the screen ,and tis
responsible for displaying information or content and event
handling.
Text ,image and Buttons are all view in android.
ViewGroup
It is essentially an 'invisible container' that holds multiple Views
or ViewGroups togeather and defines their layour properties.
-1
Linear Layout = This layout works on the general concept of linear designing and
this designing all the app windows buttons are arranged linearly .
3) Grid Layout = This type of layout will lay all the child views in a grid and thus all
the child views can vary in sizes and thus used
4) Table Layout
5)Frame layout
In the scroll View WE need to update the sdk version to 33 or higher then scroll
view will work one example is done in android studion to illustrate the same
ListView In Android
ListView listView= findViewById(R.id.LView);
ArrayList<String> foodItems=new ArrayList<>();
foodItems.add("Paneer");
foodItems.add("Roti");
foodItems.add("Laccha Parantha");
foodItems.add("Kabab");
foodItems.add("Aloo tikki");
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String text="Item"+i+""+((TextView)view).getText().toString();
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
ListView VS RecyclerView
Why RecyclerView ?
Recycler View is much more effiencient compared to listView since as the diagram
explains above.
· Recycler view only manages the columns that are visible on the screen rest
of the columns do not occupy memory
· You supply data and decide how each item looks and thus RecyclerView ID
Dynamically creates elements when they are needed
· When an itme scrolls off the screen ,RecyclerView doesn't destroy it's
view .Instead RecyclerView reuses the view for new items have scrolled
onscreen.
whereas the recyclerView has no such predefined class you have to create
view ,layout etc by your own.
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.w3c.dom.Text;
import java.util.ArrayList;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//here parent means on who we have to apply the inflater
View view= LayoutInflater.from(context).inflate(R.layout.contact_row,parent,false);
//LayoutInflater.from() means that calling the layoutinflator in terms of the
RecyclerContactApdater class
//then we use the inflate(our intent ,parent ,attachroot:false;
//if we set the attach root to true then the purpose of recycler view is defeated that is it will keep
each view
//in it's active memory instead of reusing views
ViewHolder vh=new ViewHolder(view);
return vh;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView t1;
TextView t2;
ImageView img;
public ViewHolder(@NonNull View itemView) {
super(itemView);
//t1=findViewById();
//findViewByID won't work since this is not a
//activity class
//it's a support class
//to acess the contentns of the R.layout.contact view holder here
//we use the following
t1=itemView.findViewById(R.id.textView_contact);
t2=itemView.findViewById(R.id.textView_name);
img=itemView.findViewById(R.id.imageView);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerContactAdapter.ViewHolder holder, int position) {
//binds the new text to the textView or image etc
//holder contains all the ids of the elements
holder.img.setImageResource(arrayList.get(position).img);
holder.t1.setText(arrayList.get(position).name);
holder.t2.setText(arrayList.get(position).number);
}
@Override
public int getItemCount() {
return arrayList.size();
//if returned 0 then it will not scroll no element will be inesrted
}
}
ADD THE FOLLOWING DEPENDENCIES FOR IMPLEMENTING BOTTOM NAVIGATION ACTIVITY
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
Application context is associated with the application and will always be the same throughout the life
of application; it does not change. So if you are using Toast, you can use application context or even
activity context (both) because Toast can be displayed from anywhere within your application and is
not attached to a specific window. But there are many exceptions. One such exception is when you
need to use or pass the activity context.
Activity context is associated with the activity and can be destroyed if the activity is destroyed; there
may be multiple activities (more than likely) with a single application. Sometimes you absolutely need
the activity context handle. For example, should you launch a new Activity, you need to use activity
context in its Intent so that the newly-launched activity is connected to the current activity in terms of
activity stack. However, you may also use application's context to launch a new activity, but then you
need to set flag Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.
<gradient
android:startColor="#47951E"
android:endColor="#D11616"
android:angle="90" />
<corners android:radius="20dp"/>
</shape>
may make the design work but to implement color changes we must to do
This
open themes.xml
and replace the start tag with one below
<style name="Theme.FireBaseDatabase"
parent="Theme.AppCompat.DayNight.DarkActionBar">
DataSnapshot in Firebase
args.putString("TeamName", team);
teamFragment.setArguments(args);
Bundle bundle = this.getArguments();
mTeam = bundle.getString("TeamName");
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it =new Intent(v.getContext(),PageDetailsVendor.class);
it.putExtra("Desc",model.getBookDescription());
v.getContext().startActivity(it);
//ViewHolder does not contain StartActivity
Toast.makeText(mcontext, "hello", Toast.LENGTH_SHORT).show();
}
});
Custom toast
dependencies {
...
implementation 'com.github.GrenderG:Toasty:1.5.2'
}
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
super(context);
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
} explain this to me
The main modification made in this implementation is in the onLayoutChildren() method, which
is an overridden method from the parent LinearLayoutManager. This method is called when the
RecyclerView needs to layout its children views, which happens when the adapter data changes
or when the RecyclerView is first displayed on the screen.
Overall, this implementation ensures that the RecyclerView does not crash when an
IndexOutOfBoundsException occurs during the layout process. Instead, it logs an error message
and continues to function normally.
SWIPING OF THE RECYCLER VIEW ITEMS
package com.example.swipe_recycler;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import it.xabaras.android.recyclerview.swipedecorator.RecyclerViewSwipeDecorator;
List<ViewModel> list;
RecyclerView recyclerView;
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add(vh);
list.add(vh2);
list.add(vh3);
list.add(vh4);
recyclerView = findViewById(R.id.RecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
String deleted_Movie=null;
String del_age=null;
String del_contact=null;
int image_code= 0;
ItemTouchHelper.SimpleCallback simpleCallback = new
ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull
RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
//used only for dragging the items of a recycler view not in swiping
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
case ItemTouchHelper.RIGHT:
deleted_Movie=list.get(position).name;
del_contact=list.get(position).contact;
del_age=list.get(position).Age;
image_code=list.get(position).img_code;
list.remove(position);
adapter.notifyItemRemoved(position);
Snackbar.make(recyclerView,"ARCHIVED",
BaseTransientBottomBar.LENGTH_LONG).setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View v) {
list.add(position,new
ViewModel(deleted_Movie,del_age,del_contact,image_code));
adapter.notifyItemInserted(position);
}
}).show();
break;
}
}
init {
fun show() {
println("$name $age")
fun main() {
p.show()
class Person {
get() {
set(value) {
field = value
get() {
return field
set(value) {
field = value
get() {
return field
set(value) {
field = value
}
fun main() {
println(person.job) /
Model: The model is the data layer of an application. It contains the application's data
objects and their relationships. The model is responsible for storing, retrieving, and updating
data.
View: The view is the user interface layer of an application. It displays the data to the user
and allows the user to interact with the data. The view is not responsible for storing or
manipulating data.
ViewModel: The view model is the bridge between the model and the view. It exposes the
model's data to the view in a way that is easy to use. The view model also handles user
input and updates the model accordingly.
Separation of concerns: The MVVM pattern separates the application's data, presentation,
and behavior into three distinct layers. This makes the application easier to understand,
develop, test, and maintain.
Testability: The MVVM pattern makes it easy to test the application's data, presentation, and
behavior independently. This is because each layer is isolated from the others.
Reusability: The MVVM pattern makes it easy to reuse the application's data, presentation,
and behavior in other applications. This is because each layer is independent of the others.
Here are some tips for using MVVM:
Use a data binding library: A data binding library can help you to bind the model's data to
the view's controls. This can save you a lot of time and effort.
Create a separate view model for each view: This will help to keep your code organized and
maintainable.
Use the view model to handle user input: This will help to keep your view code clean and
concise.
Update the model whenever the view model changes: This will ensure that the application's
data is always up-to-date.
val Calculator=calculator();
val result=calculator.add(29,49);
println(result);
println("Difference is "+Calculator.sub(32,54));
}
class calculator{
companion object {
fun add(a: Int, b: Int): Int {
return a + b;
}
}
fun sub(a:Int,b:Int) = a-b;
Couroutine in kotlin