Can't find the findViewById function - android

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

Related

Cannot Resolve R.menu and other R. stuff in Android Studio

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.

Can't replace AppCompatActivity with Activity

I'm a beginner to android programming.So could not get what the methods are in the MainActivity class. Of course googled it. But didn't find the answer. Every tutorial asking to extend the main class from Activity,but I'm not able to do it. Any answes, explanations, suggestions are appreciated.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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);
}
#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);
}
}
AppCompatActivity is the latest Activity class (Its inheritance is Activity) that provide compatilibity with old phones (from 2.1+, api 7) so it will follow the activity life cycle (onCreate, onStart, onResume, onPause, onStop, onDestroy).
With the AppCompatActivity you can use all the methods from activity and also have access to Fragments (for dynamic UI via SupportFragmentManager), Loaders (for sql queries via SupportLoaderManager), the Action bar (via SupportActionBar).
To Summarized: AppCompatActivity = Activity + Support for old phones.
AppCompatActivity is what you can use instead of Activity for v21+ of the android sdk.
As for the methods, in your example:
onCreate - infaltes the layout and you can do a lot of the instantiation in this method, read intent extras etc.
OnCreateOptionsMenu - inflates the menu for the view that you have specified in res/menu which appears on the action bar.
OnOptionsItemSelected - you can specify the actions that should be initiated when a menu item is clicked.
And if you try extends your class to ActionBarActivity?

Android WebView returns weird error

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.

New Android Project with Eclipse (Explanation of the Code)

I am new to programming Android. When I start a new Android Application Project in Eclipse it automatically opens 2 files for me:
MainActivity.java and fragment_main.xml
package com.example.testprogram;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
and
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.testprogram.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
I wonder what all this stuff is. The Tutorials I watch always have a different code when they start a new project. For example the .xml file that Eclipse opens for them is named activity_main.xml (unlike mine which is named fragment_main.xml). Also their MainActivity.java has less methods integrated then mine.
Can Someone tell me wether that makes a huge difference and how i can maybe change that.
You are using latest one so it get like this. Don't worry about it.
public class MainActivity extends ActionBarActivity
instead of
public class MainActivity extends Activity
Remove all code in your MainActivity except OnCreate(). Then you follow your tutorial.
You onCreate should be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Those are probably old tutorials (on Android platform even after couple of months an tutorial can become old), Android ADT is constantly changing. It now automatically adds an Activity and a Fragment to your project. Instead of just an Activity. You can still use knowledge from those tutorials and fit it into Fragments. Or delete Fragments and use only Activities. But Android is promoting the use of Fragments with Activities, not just mere Activities so you should learn how to use them.
Create New Project Like This:
Start `File->New->Android Application Project->Next->Next->Next->Select-> Empty Activity->Next->Finish

How do I make a slide left/right menu in Android?

I'm trying to create a small game on Android and have some questions on a specific section of my game. I'm fairly new to android so please excuse if I don't have a full understanding of certain things.
When clicking "play" I'd like to view slide-able menu that makes it able for the user to swipe left and right to choose a level. Overtime I will be adding few more levels but have 2-3 of them now.
What would be the best way to do this? Is it best to implement a fragment for each "level page" or create entirely new activities?
My project is compatible for Android ver. 2.3.3 and above, so it's automatically included the "appcompat_v7" project. (I don't know if that makes a difference).
I've pasted my code below if needed:
package com.example.snake;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class SnakeLevelSelectActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_snake_level_select);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.snake_level_select, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_snake_level_select, container, false);
return rootView;
}
}
}
What I intended to do was create several fragment classes and animate between the fragments when the user swipes. I also have issues understanding on how to use several fragments with the "PlaceHolderFragment" class, since the solutions I've found on SO have been different. This is an entirely question, but would be appreciated if it was answered as well.
What would be the best way to do this? Is it best to implement a fragment for each "level page" or create entirely new activities?
This is exactly what a Fragment is for. What you are looking for is already there and named ViewPager. Using ADT and Eclipse you can even create an Activity with this already implemented. Use the "Navigation type" combobox for that purpose:
You can also choose "Action Bar Tabs (with ViewPager)", which will enable Tabs in the ActionBar and make sure that you can switch to different Fragments using both the swipe gesture as well as the tabs.
Android now has this built-in in the SDK.
They call it the Navigation Drawer.
Look up the documentation it contains sample project, it is very easy to implement.

Categories

Resources