Android Studio Tutorial (Error with referencing xml file) - android-studio

Error when transitioning from first activity to next activity.
Cant seem to reference the R.layout.activity_display_message even though its under that folder,
when I type out the R.layout portion, it only autofills with activity_main.xml and cant reference the activity_display_message.xml file.
package com.example.friendmeter;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}

Related

How to use 2 public classes, protected void on creat functions in the same fragments?

How to put more than one class,overview,void in MainActivity java of Android Studio? I need to have both the button and a code to make the map of google have some restricions in the same page. What names of the classes should be changed for it to work. The codes work when put independently but not together.How to make them work good together?
package com.example.maps;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
public class FirstFragment extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openFoodFragment();
}
});
}
public void openFoodFragment(){
Intent intent = new Intent(this, FoodFragment.class);
startActivity(intent);
}
}
public class MainACTIVITY extends FragmentActivity implements OnMapReadyCallback {
GoogleMap mapAPI;
SupportMapFragment mapFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapFragment =(SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.mapAPI);
mapFragment.getMapAsync(this);
}
public void openFoodFragment(){
Intent intent = new Intent(this, FoodFragment.class);
startActivity(intent);
}
#Override
public void onMapReady(GoogleMap googleMap){
mapAPI = googleMap ;
LatLng one = new LatLng(-21.754812, -48.219451);
LatLng two = new LatLng(-21.787443, -48.113332);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
//add them to builder
builder.include(one);
builder.include(two);
LatLngBounds bounds = builder.build();
//get width and height to current display screen
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int padding = (int) (width * 0.20);
mapAPI.setLatLngBoundsForCameraTarget(bounds);
mapAPI.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding));
//set zoom to level to current so that you won't be able to zoom out viz. move outside bounds
mapAPI.setMinZoomPreference(mapAPI.getCameraPosition().zoom);
LatLng Kampai = new LatLng(-21.780985, -48.186859);
mapAPI.addMarker(new MarkerOptions().position(Kampai).title("Kampai"));
mapAPI.moveCamera(CameraUpdateFactory.newLatLng(Kampai));
}
}
Yes the can be in the same file but you have your mainACTIVITY inside your FirstFragment which cannot happen you need to remove the bottom most bracket and place it right above the start of your mainACTIVITY class, your FirstFragment's brackets need to close before you paste another class into the file.

Opening a new fragment from within a ViewPager in Android Studio

I have a question about opening a new fragment from within a ViewPager.
I setup FragmentA, FragmentB and FragmentC to be fragments of a viewpager and then proceeded to setup Fragment1 and Fragment2 as fragments of another viewpager which resides in FragmentB.
So Fragment1 and Fragment2 are in a viewpager which is essentially nested in another viewpager. I'm playing around with UI styles etc and this is is similar to that found in the Flickr app.
The problem I'm having is that once I get into the fragment1 or fragment2 (the nested fragment) I want to be able to click on something and enter a full screen fragment bypassing the toolbar I set up in FragmentB.
Below is the the code I have for FragmentB with the toolbar, tablayout and viewpager.
package com.app.fragmentdemo;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.tabs.TabLayout;
public class FragmentB extends Fragment {
public FragmentB() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_b,container,false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.viewPagerFragmentB);
ViewPagerAdapter adapter = new ViewPagerAdapter(((FragmentActivity) getContext()).getSupportFragmentManager());
adapter.addFragment(new Fragment1(),"Fragment 1");
adapter.addFragment(new Fragment2(),"Fragment 2");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
// Inflate the layout for this fragment
return rootView;
}
}
Here is the code I've put together for fragment2 which contains a button I would like to take to take me to another fragment.
package com.app.fragmentdemo;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Fragment2 extends Fragment {
public Fragment2() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_2,container,false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new FragmentFull();
FragmentManager fm = ((FragmentActivity) getContext()).getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentFullContainer,fragment);
ft.commit();
Toast.makeText(getActivity(), "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
// Inflate the layout for this fragment
return rootView;
}
}
Upon clicking the button the app crashes. I get the error:
No view found for id 0x7f080084 (com.app.fragmentdemo:id/fragmentFullContainer) for fragment FragmentFull
If I change the id of the fragment container to the id I assigned to the Fragment2 layout file, it works fine. However, this is still contained in the nested viewpager and doesn't allow me to enter a new layout.
I really appreciate your help.
Thanks.
In the following line
ft.add(R.id.fragmentFullContainer,fragment);
fragmentFullContainer should be a view in your R.layout.fragment_2
You are getting the crash because the OS cannot find fragmentFullContainer in R.layout.fragment_2
UPDATE
replace getSupportFragmentManager with getChildFragmentManager
If you want full screen , you have to manually hide Activity View elements.
Most probably, you have toolbar from activity, you can remove it like that.
((AppCompatActivity)getActivity()).getSupportActionBar().hide();

Notifications wont show android studio

Im trying to get a notification when pressing a button, but it is not showing up. I have followed a lot of tutorials and i think it has something ti do with the version of android. Something with channels. I just dont know how to implement it.
package com.example.root.savetheworldv2;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent =new Intent();
PendingIntent pIntent =
PendingIntent.getActivity(MainActivity.this,0,intent,0);
Notification noti = new
Notification.Builder(MainActivity.this)
.setTicker("TickerTitle")
.setContentTitle("Content Title")
.setContentText("Content Text")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager nm =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0,noti);

android studio error: reached end of file while parsing

package com.example.vicky.videostory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
Button btnPlay;
VideoView myvideo;
MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay=(Button) findViewById(R.id.btn_Play);
myvideo=(videoView) findviewById(R.id.videoPlayer);
mediaController = new MediaController( this) ;
btnPlay.setOnClickListner(new View.OnClickListener() {
#Override
public void onClick(View view) {
String videoPath = "android.resource//com.example.vicky.videostory/"+R.raw.technology;
Uri uri=Uri.parse(videoPath);
myvideo.setVideoURI(uri);
myvideo.setMediaController(mediaController);
mediaController.setAnchorView(myvideo);
myvideo.start();
}
}
);
}
error: reached end of file while parsing
I think its just errors in brackets. Because when I click on an error it points towards brackets and says unexpected error.
this is the error please check whats wrong
You have problem in your syntax correct it first and then Rebuild Project also remember that follow the basics properly before doing code.

Android Studio - Creating a Loading/Splash Screen for WebView

I have followed through this tutorial (I am completely new to Android Studio): Loading/Splash Screen Tutorial
and I am unsure why my code isn't working correctly.
I get the loading circle come up on the app I have created, but it does not go away when the app loads the webpage.
I have gone over my code a few times now and I cannot see any errors (obviously there are some somewhere!) Just seems as if it isn't recognising that my webpage has loaded within the app.
Here is my code from my MainActivity.java file:
import android.net.http.SslError;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
String ShowOrHideWebViewInitialUse = "show";
private WebView myWebView;
private ProgressBar spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
spinner = (ProgressBar)findViewById(R.id.progressBar1);
myWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);
myWebView.getSettings().setMinimumFontSize(1);
myWebView.getSettings().setMinimumLogicalFontSize(1);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadUrl("https://node-red-test.ftp21.net:1024/ui");
myWebView.setWebViewClient(new WebViewClient() { #Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){ handler.proceed(); } });
}
// This allows for a splash screen
// (and hide elements once the page loads)
private class CustomWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView myWebView, String url, Bitmap favicon) {
// only make it invisible the FIRST time the app is run
if (ShowOrHideWebViewInitialUse.equals("show")) {
myWebView.setVisibility(myWebView.INVISIBLE);
}
}
#Override
public void onPageFinished(WebView view, String url) {
ShowOrHideWebViewInitialUse = "hide";
spinner.setVisibility(View.GONE);
view.setVisibility(myWebView.VISIBLE);
super.onPageFinished(view, url);
}
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
I am currently running Android Studio version 2.1.2 - due to problems with later versions that I came across when following another tutorial.
Thanks,

Resources