I 'm trying to do a simple browser AndroidStudio , but websites do not open ( error : net :: ERR_CACHE_MISS ).
I tried this :
Android 4.4 giving ERR_CACHE_MISS error in onReceivedError for WebView back
but it did not help me .
my ActivityMain:
public class MainActivity extends AppCompatActivity {
EditText pole;
Button przycisk;
WebView przegladarka;
public String adres;
public void Wyszukaj(View v){
adres = pole.getText().toString();
przegladarka.loadUrl(adres);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
przycisk = (Button) findViewById(R.id.przycisk);
pole = (EditText) findViewById(R.id.pole);
przegladarka = (WebView) findViewById(R.id.przegladarka);
if (Build.VERSION.SDK_INT >= 19) {
przegladarka.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
przegladarka.setWebViewClient(new ModifiedWebViewClient());
przegladarka.getSettings().setJavaScriptEnabled(true);
przegladarka.loadUrl("http://google.com");
}
private class ModifiedWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Add this line of code to your manifest file
<uses-permission android:name="android.permission.INTERNET"/>
Related
I have a textview in a fragment that I want to edit in MainActivity.java (The parent activity). I've tried many methods such as creating a function in the fragment code:
public class HomeFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
return root;
}
public void status(String txt){
TextView textView = getView().findViewById(R.id.status_message);
textView.setText(txt);
}
}
and calling it in MainActivity like so:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HomeFragment fragment = new HomeFragment();
fragment.status("Test");
}
}
But this results in the app crashing. How can I fix this? I am using the Bottom Navigation Activity Template.
Best thing I would advice is to create your fragment instance in your main activity like this:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HomeFragment fragment =
HomeFragment.createInstance("Test");
}
}
And then in your fragment:
public class HomeFragment extends Fragment {
private string text;
public View onCreateView(#NonNull LayoutInflater
inflater, ViewGroup container, Bundle
savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home,
container, false);
TextView textView =
root.findViewById(R.id.status_message);
return root;
}
public static HomeFragment createInstance(String text)
{
HomeFragment f =new HomeFragment ();
Bundle bundle=new Bundle();
bundle.putString("key", text);
f.set arguments(bundle);
return f;
}
#Override
public void onCreate(#Nullable Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments != null) {
text= getArguments.getString("key");
status(text);
}
}
public void status(String txt){
textView.setText(txt);
}
}
Hopes this helps
Fixed this by accessing MainActivity from the fragment rather than accessing Fragment from MainActivity. Instead of changing the textview from MainActivity, send the string you want to put in the textview through a method and change the textview in the fragment.
MainActivity.java
public class MainActivity extends AppCompatActivity{
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public String sendData(){
return text;
}
}
HomeFragment.java
public class HomeFragment extends Fragment {
TextView tv;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
tv = root.findViewById(R.id.textview);
MainActivity ma = (MainActivity)getActivity();
tv.setText(ma.sendData());
return root;
}
}
been alright so far. I try to use docs or find my issues.
However my mainActivity has 4 buttons. These buttons depending on which view is clicked passes the R.id.button. I pass this value into an intent which Activity2 uses bundle to get id integer. I then setContentView using this.
However im having issues getting correct view to inflate now, it seems like its loading an old layout. My aim is to inflate 3 separate layout on Activity2 depending which button was clicked.
I know my other other button does inflate another activity and get proper xml layout.
Why are my other 3 buttons not inflating on Activity2 properly?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button pizza;
Button hamburger;
Button icecream;
Button history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pizza = findViewById(R.id.buttonPizza);
hamburger = findViewById(R.id.buttonBurger);
icecream = findViewById(R.id.buttonIceCream);
history = findViewById(R.id.buttonHistroy);
pizza.setOnClickListener(this);
hamburger.setOnClickListener(this);
icecream.setOnClickListener(this);
history.setOnClickListener(this);
}
#Override
public void onClick(View v){
int idView = v.getId();
switch(v.getId()) {
case R.id.buttonBurger : startOrderActivity(R.layout.burger_layout);
makeToast(idView);
break;
case R.id.buttonPizza : startOrderActivity(R.layout.pizza_layout);
makeToast(idView);
break;
case R.id.buttonIceCream : startOrderActivity(R.layout.icecream_layout);
makeToast(idView);
break;
case R.id.buttonHistroy : startOrderActivity(R.layout.history_layout);
makeToast(idView);
break;
}
}
private void startOrderActivity(int id){
if(id != R.layout.history_layout) {
Intent intentOrder = new Intent(this, MakeOrderActivity.class);
intentOrder.putExtra("layout", id);
startActivity(intentOrder);
}
else {
Intent intentOrder = new Intent(this, OrderHistoryActivity.class);
intentOrder.putExtra("layout", id);
startActivity(intentOrder);
}
}
public void makeToast(int id){
String txt = String.valueOf(id);
Toast newToast = Toast.makeText(getApplicationContext(),txt,Toast.LENGTH_LONG);
newToast.show();
}
}
public class MakeOrderActivity extends AppCompatActivity {
Button placeOrderButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getIntent().getExtras();
int layoutNum = data.getInt("layout");
setContentView(layoutNum);
placeOrderButton = findViewById(R.id.placeOrder);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.order, menu);
return true;
}
public void onCheckboxClicked(View view){
}
}
Answer is to create a View which is created by getInflator.
https://stackoverflow.com/a/17070408/8443090
I'm new to Android Studio (and also pretty new to javascrips), so this might be a stupid question.
I do not want to maintain the same code more than once if possible, which is why I am trying to create a navigation-menu, to be used in all my activities.
So far I have created a "BaseActivity" that contains all the functions for my menu. This one works fine.
Now my problem:
In my other activities I can not get it to show up (not without issues anyway). It seems to me like it overwrites the layout.
My base ("BaseActivity"):
public class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.string.Open, R.string.Close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView NavigationView=(NavigationView)findViewById(R.id.navigation_view);
NavigationView.setNavigationItemSelectedListener(this);
}
#Override
public void setContentView(int layoutResID)
{
super.setContentView(R.layout.activity_base);
ViewGroup content = (ViewGroup) findViewById(R.id.navigation_view);
getLayoutInflater().inflate(layoutResID, content, true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if(id==R.id.nav_Home) {
startActivity(new Intent(this, MainActivity.class));
}
else
if(id==R.id.nav_Activity1) {
startActivity(new Intent(this, Activity1.class));
}
return super.onOptionsItemSelected(item);
}
}
If I change the app to start through this activity the menu works, but the layout seems to be overwritten (it's empty).
When I try to run it normally (MainActivity), I get some issues.
MainActivity:
public class MainActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }}
The menu-icon is there, but I can not click on it.
If I remove the code in my MainActivity it works as before (with the layout problem).
I have tried something like this:
public class MainActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup content = (ViewGroup) findViewById(R.id.navigation_view);
getLayoutInflater().inflate(R.layout.activity_main, content, true);
}}
Not working well. The layout from MainActivity seems to be implemented in the menu. It looks very strange.
Any ideas how to solve my problem?
I want to make a soundboard app and I want it so if I press the button for the sound it first stops all sounds but how do you do that?
public class MainActivity extends AppCompatActivity {
MediaPlayer smileMore;
MediaPlayer intro;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
smileMore = MediaPlayer.create(this,R.raw.smilemore);
intro = MediaPlayer.create(this,R.raw.romanatwoodintro);
}
public void smileMoreBtn(View view) {
smileMore.start();
}
public void introBtn(View view) {
intro.start();
}
}
I am developing a simple app in that my video is playing proper. but now I want to give "thank" message after finishing my video
How can I do this?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
this.setSupportActionBar(toolbar);
final MediaPlayer player = MediaPlayer.create(MainActivity.this,R.raw.video);
Button button = (Button)findViewById(R.id.video_Play);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
player.start();
startActivity(new Intent("com.example.lenovo.play.app.VideoPlay"));
}
});
}
and Second VideoPlay.java
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
view = (VideoView)findViewById(R.id.videoView);
String url = "android.resource://"+getPackageName()+"/"+R.raw.video;
view.setVideoURI(Uri.parse(url));
view.start();
int duration = view.getDuration();
}
Implement MediaPlayer.OnCompletionListener then add your message to onCompletion method:
#Override
public void onCompletion(MediaPlayer mp) {
//message
}