android important topics :
1-Validator##########################################Implement Form Validation (Error to EditText) in Android :
Step-1 ; create java class and put given code :
package com.like.tryforbase;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.util.regex.Pattern;
public class Validator {
private static Validator validator;
private Validator() {
}
public static Validator getInstance() {
if (validator == null) {
validator = new Validator();
}
return validator;
}
public static String isValidEmail(Context context, CharSequence target) {
if (target == null ||target.length()==0) {
return context.getString(R.string.error_empty_email_id);
} else if (!android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches()) {
return context.getString(R.string.error_invalid_email_id);
}
return "";
}
public final static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
public boolean isValidMobile(Context context, String phone) {
if(!Pattern.matches("[a-zA-Z]+", phone)) {
return phone.length() > 6 && phone.length() <= 17;
}
return false;
}
}
Step-2 put given code on String resource :
<string name="error_invalid_email_id">Please provide valid email id</string>
<string name="error_empty_email_id">Email id field is required</string>
<string name="error_emptyMetaTitle">Please provide Meta Title</string>
<string name="error_editText_phone">Please enter your mobile no</string>
put uses-permission on Manifest.xml;
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2-how to hide keyboard in android *************************
protected void toHideKeyboard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Implement progress Bar :*************************
ProgressDialog dialogr = new ProgressDialog(currentActivity);
dialogr.setMessage("Please wait.....");
dialogr.setCancelable(false);
dialogr.setCanceledOnTouchOutside(false);
dialogr.show();3-put value on SharedPreference after responce come from APi :************
SharedPreferenceUtils.getInstance(context).putString(AppConstants.USERPHONE, phoneNoResponse.getMobileNumber());
SharedPreferenceUtils.getInstance(context).putString(AppConstants.ENDUSERID, phoneNoResponse.getEndUserID());
SharedPreferenceUtils.getInstance(context).putString(AppConstants.USER_EMAIL, phoneNoResponse.getEmailID());4-Finish Activity After Complete complete all process:*************finish();
5-Take Android Id :***************************
String AndroidID = Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID_ID);6 - Check check permission android:*****************
if(ContextCompat.checkSelfPermission(
getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(
SellerRegisterActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_LOCATION_PERMISSION
);
}else {
getCurrentLocation();
}7-Take value from SharedPreference :************
String id = SharedPreferenceUtils.getInstance(context).getString(ENDUSERID);8-Take arrayList from Adapter class on Main Activity ,data come from server set data in List*****
ArrayList<BuyerWishListModel> wishlist = new ArrayList<>(); wishlist is use on adapter class ,wishlist store list data and send to adapter : list store Table Data that is come from server ,List<BuyerWishListModel> list = buyerWishListResponseModel.getTable(); before stor data on wishlist clear wishlist,and notify Adapter :wishlist.clear();
adapter.notifyDataSetChanged();
Add list Data on wishlist and show on Sceen
wishlist.addAll(list);
adapter.notifyDataSetChanged();9-Create new thread in Android :
Thread thread = new Thread(){
public void run(){
youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
String videoId = youtubevideoId;
youTubePlayer.loadVideo(videoId, 0);
youTubePlayer.pause();
}
});
}
};
thread.start();10-Link our app through social media :************
public void onClick(View view) {
drawerLayout.closeDrawer(GravityCompat.START);
switch (view.getId()) {
case R.id.twitter: { //Pass social media Link below :
String socialNetwork = "https://twitter.com/";
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(socialNetwork));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(socialNetwork));
try {
context.startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
context.startActivity(webIntent);
}
break;
}
case R.id.facebook: {
String socialNetwork = "https://www.facebook.com/";
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(socialNetwork));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(socialNetwork));
try {
context.startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
context.startActivity(webIntent);
}
break;10-Store html data on Web View :************
WebView webView;What is binding in Android?webView = (WebView) findViewById(R.id.webView);
final String descriptionUsingWebView = "<div class=\"privacy-policy-wrapper py-3\" style=\"height: auto !important;\">\n" +
" <h1>Privacy Policy</h1>\n" +
" <p>We, <a href=\"https://www.aajjo.com\" target=\"_blank\">Aajjo.com</a> (hereinafter referred to as the \"Company\")
webView.loadDataWithBaseURL(null, descriptionUsingWebView, "text/html", "utf-8", null);11-Divide Url in Part :************
Uri uri = getIntent().getData();
if (uri != null) {
String path = uri.toString();
String[] parts = path.split("/");
int size = parts.length;
if (size == 3) {
String HOST = parts[0];
String HOST1 = parts[1];
String HOST2 = parts[2];
}
if (size == 4) {
String HOST = parts[0];
String HOST1 = parts[1];
String HOST2 = parts[2];
String PORT1 = parts[3];
String CategorySlugURL ="";
String SlugURL = "";
if(PORT1.contains("?")){
String[] data1 = PORT1.split("\\?");
String chk = data1[0];
String[] data = chk.split("_");
CategorySlugURL = data[0];
SlugURL = data[1];
}else {
String[] data = PORT1.split("_");
CategorySlugURL = data[0];
SlugURL = data[1];
}12 : About JSON***********************
JSON format is used for serializing and transmitting structured data over network connection. It is primarily used to transmit data between a server and web applications. Web services and APIs use JSON format to provide public data. It can be used with modern programming languages.When should I use FragmentPagerAdapter?
Its view hierarchy may be destroyed but the fragment may still remain in memory.Hence it is advised to use this Android FragmentPagerAdapter only when there are low number of static fragments, since all fragments would be kept in the memory and this would increase the memory heap
View Binding Part of Android Jetpack. View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module,
The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources
Step-1: define globally:***************************
ActivityMainBinding binding;
Step-2:Write given code inside On Create():super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());replace fragment in framelayout android :**************
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
binding.toolbar.setVisibility(View.GONE);
transaction.replace(R.id.content, new HomeFragment());
transaction.commit()If We Want replace Multiple fragment with framelayout android :
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (i) {
case 0:
binding.toolbar.setVisibility(View.GONE);
transaction.replace(R.id.content, new HomeFragment());
break;
case 1:
binding.toolbar.setVisibility(View.GONE);
transaction.replace(R.id.content, new NotificationFragment());
break;
case 2:
binding.toolbar.setVisibility(View.GONE);
break;
case 3:
transaction.replace(R.id.content, new SearchFragment());
binding.toolbar.setVisibility(View.GONE);
break;
case 4:
transaction.replace(R.id.content, new Profile2Fragment());
binding.toolbar.setVisibility(View.VISIBLE);
break;
}
transaction.commit();
}
});Set OptionsMenu on Main Activity :***********************
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_item, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.setting:
Toast.makeText(this, "Setting Clicked", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}dependency onbinding :Put on build.gradle
buildFeatures {
viewBinding true
}