I am trying to create a simple browser app. Here is my code:
package com.degstu.ultralightbrowser;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Button button;
//#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Code for "GO" button
public void sendURL(View view) {
TextView textURL;
textURL = (TextView) findViewById(R.id.textBoxURL);
WebView webView = new WebView(this);
webView.loadUrl(textURL.toString());
setContentView(webView);
}
}
Everything mostly works, however, when I press "GO" in the app, I see this.
There are no errors recognized in my code, and any help would be appreciated.
Apparently, the url you wanted to visit is not valid (it starts with "andorid.support.v7..." which is a string containing a concise, human-readable description of the TextView object). To get the text of TextView, you should use getText() to return the text the TextView is displaying.
webView.loadUrl(textURL.getText().toString());
First of all,avoid extends ActionBarActivity .Use Activity or AppCompatActivity.
WHY
Since the version 22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity ORActivity .
Please update your Code Like this
TextView textURL;
textURL = (TextView) findViewById(R.id.textBoxURL);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(textURL.getText().toString());
For details you can visit here Android - WebView Tutorial
and https://developer.android.com/reference/android/webkit/WebView.html
I hope it helps you.
Related
I am new to Android Studio and I was working on v3.1.4. When I was dragging any of the widgets and put it in the design, it does not appear. It gives me an error message that the widget is not constrained and it will jump to (0,0) point unless you add a constraint to it. When I use the magic wand the error disappeared but the problem still exist and the widget does not appear.
The code:
package com.example.amr.myapplication;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Solution:
Step1: Open SDK Manager then install the latest SDK from that.
Step2: Shift to SDK Tools section in SDK Manager then install everything except NDK.
Step3: Restart Android Studio.
Finally, Problem Solved. (See the image below for reference)
Hopefully it will work.
You have the Attributes panel collapsed on the left, inside that can put costraints on the current selected widget.
You'll see a square with four plus signs around to add costraints.
The simple code is to read the image file from the SD Card and display on image view
Following is the code
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File imgFile = new File("/storage/extSdCard/DCIM/Camera/Test.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imgView);
myImage.setImageBitmap(myBitmap);
}
setContentView(R.layout.activity_main);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am using Samsung S3 as the android device. Error I am receiving is "java.Lang.RuntimeException: Unable to start the activity component" in logcat. The required permission was also considered.
Need suggestions to make the code work correctly. ( Also app successfully installed in device but unable to run properly).
This statement:
ImageView myImage = (ImageView) findViewById(R.id.imgView);
cannot be done before the setContentView();
The way you've done it myImage will be null, therefore the NullPointerException I suppose you're getting.
I have been practicing in Android Studio as per the android app development free course in UDACITY. And wherever there is a R.menu or R. stuff it cannot be resolved
package com.example.android.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.R;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
}
Plz help how to solve this Issue.
Clean and rebuild your project by going to Build>Clean Project.
Go to File>Invalidate Caches/Restart.
Make sure you built your project, and make sure there aren't any errors (red lines) in the res directory.
remove import android.R; clean and rebuild it.
if it still doesn't work try this
import com.example.android.courtcounter.R;
Check you AndroidManifest.xml, check the package name there.
use that as import .R; in code.
I am working on a simple android application that has MainActivity with FragmentStatePagerAdapter and some number of fragments side by side. I want to be able to go to the first fragment by pressing home button.
I know my code is pretty bad. Still I hope just a few lines will solve my problem.
Here is my MainActivity
package com.freestylers.druskischool;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
ViewPager pager;
FragmentStatePagerAdapter adapter;
* Each page of our pager will display one fragment from this array
* Swiping, to the right will take you to the next page
*/
String[] fragments={
"start",
"mes",
"whatever",
"next",
"name",
"of",
"a",
"fragment",
"more fragments",
"and",
"more",
"fragments"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager=(ViewPager)findViewById(R.id.my_pager);
adapter=new FragmentStatePagerAdapter(
//maybe should use normal getFragmentManager()
getSupportFragmentManager()
){
#Override
public int getCount() {
// This makes sure getItem doesn't use a position
// that is out of bounds of our array of fragments
return fragments.length;
}
#Override
public Fragment getItem(int position) {
// Here is where all the magic of the adapter happens
// As you can see, this is really simple.
//(fragments[position]);
if(position==0){
return StartFragment.newInstance();
}
else if(position==1){
return MesFragment.newInstance();
}
else if(position==2){
returnNextFragment.newInstance();
}
//this goes on until position==11
else{return StartFragment.newInstance();}
}
};
//Let the pager know which adapter it is supposed to use
pager.setAdapter(adapter);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is one of my fragments, MesFragment. On the line 19 (commented out by double slash)I just don`t know which command to use to get to my first fragment:
package com.freestylers.druskischool;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MesFragment extends Fragment {
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
//(getActivity()).getSupportFragmentManager().??? I don`t know how
to go back to first fragment
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
(getActivity()).getActionBar().setDisplayHomeAsUpEnabled(true);
View view=inflater.inflate(
R.layout.fragment_mes,
container,
false);
return view;
}
// This is the method the pager adapter will use
// to create a new fragment
public static Fragment newInstance(){
MesFragment f=new MesFragment();
return f;
}
}
Thanks
You can set the currently selected item using ViewPager, which is in your activity:
public void switchToMesFragment() {
pager.setCurrentItem(1);
}
To switch from within a fragment, you need to get a reference to your activity and call the switching function, like so:
((MainActivity)getActivity()).switchToMesFragment();
You cannot completely control the Home button key since it is a key that is broadcasted to all active apps on the device. If you want to process the Home button for your app, you may overrride the Activity.onPause() method. Documentation # Pausing an Activity. But still you cannot prevent the user from seeing the Home screen, that's not fair to other apps perhaps. Try this out first...
I'm an Android newbie working with the Eclipse IDE and the Android SDK. Anyway, I've watched a few tutorials and I can't seem to be able to call the findViewById() function.
I would appreciate if you could instruct me how to use it, if it's a method of a static class or something else.
Thanks, I'm sure this won't be a problem for the more advanced users!
Well the in response to the comments I'm trying to use it in an Activity.
Here's the code in there:
package tk.quiero.test1;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Where exactly am I supposed to use it?
Thanks
findViewById is defined in the Activity class, so it sounds like you're trying to call this from outside of the Activity. If so, you should post what exactly you're trying to do because there are ways of doing this, but there's also a high likelihood that there's a cleaner or easier way than resolving UI elements outside of the Activity
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.xxxxx)
}
replace xxxxx with the id that you've defined in your xml...which should look like #+id/xxxxx