Cannot resolve method findViewByID - java

I'm very new to android development, so I'm sorry if I can't understand what exactly is going on here.
I've created a shape, as can be seen below. This is contained within shape.xml within the drawable folder:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/lboro_pink"/>
<stroke
android:width="50dp"
android:color="#color/lboro_blue"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
Now within my activity_main.xml I have this TextView:
<TextView
android:id="#+id/grid_1"
android:background="#drawable/shape"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="#string/event_1"/>
Now this is my MainActivity.java:
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.res.Resources;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.test_image);
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.shape);
TextView tv = (TextView)findViewByID(R.id.grid_1);
tv.setBackground(shape);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#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);
}
}
I've been trying to follow this doc in order to do this, and by using their code I just get the error that it "Cannot resolve method findViewByID". I've searched for answers, but just can't understand what they're trying to explain, when the official android doc says to do this.
All I'm trying to do with this is apply the shape I've created as the background to a TextView.
Any help is greatly appreciated. Thank you :)

TextView tv = (TextView)findViewByID(R.id.grid_1);
This line is misspeled. It should be
TextView tv = (TextView)findViewById(R.id.grid_1);

Related

how to add menu items

Like the older version of android studio menu is not pre generated. So I tried and added some code of my own. But unfortunately I am unable to add menu items. So need some help:(
MainActivity:
package com.buckydroid.materialapp;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#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, 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();
return super.onOptionsItemSelected(item);
}
}
Menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="next"
android:orderInCategory="100"
android:icon="#drawable/refresh"
app:showAsAction="always"
/>
</menu>
app_bar:
<?xml version="1.0" encoding="utf-8"?>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
android:background="#color/colorPrimary"
android:title="#string/app_name"
android:titleTextColor="#fff"
>
</Toolbar>
So if you need any other codes then comment :(
Change your code in MainActivity.java
from
#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, menu);
return true;
}
To
#Override
public boolean onPrepareOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
I hope it will fix the problem...

Cant use Appbar with Buttons

I started working with the Holo Theme, and I want to build my app with this Theme (without using the Compact)
The problem is that my App bar is whithout the Buttons. (I cannot use the ifRoom option for showing the menus icons).
my menu code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:orderInCategory="105"
app:showAsAction="ifRoom"
android:title="#string/action_search"
/>
<item
android:id="#+id/action_edit"
android:icon="#drawable/ic_action_edit"
android:orderInCategory="110"
android:title="#string/action_edit"
app:showAsAction="ifRoom" />
and my Java Class:
package myfirsttry.another;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity{
public final static String EXTRA_MESSAGE = "linir.key";
#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);
}
public void sendMessage(View view){
Intent intent = new Intent(this, secondActivity.class);
EditText editText = (EditText)findViewById(R.id.etInput);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}
Thanks
The app:showAsAction is only applicable if you use AppCompat as is strongly recommended for compatibility and consistency across all API levels.
If you'd like to only use the system provided action bar, then you'd replace all instances of app:showAsAction with android:showAsAction.

Android Action Button not shown in action bar

I want to display an icon on the action bar. But it always comes in the overflow list. Could you please help me up with a solution. This is my code.
CreateScheduleActivity.java
package com.mpeers.ui;
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.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class CreateScheduleActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_schedule);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, 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);
}
}
action_bar.xml in menu folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/plusicon"
android:title="CS"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
If you are using the appcompat support library, you need to define
xmlns:app="http://schemas.android.com/apk/res-auto"
in the parent tag and then use app:showAsAction for controlling the visibility of the action items.
If you are not using the appcompat support library, you can use the android:showAsAction tag for controlling the visibility of the action items.
define xmlns:app="http://schemas.android.com/apk/res-auto" in parent tag.
then use app:showAsAction="ifRoom"
Following is the example code.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.taazi.android.MainActivity" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_right_drawer"
android:icon="#drawable/ic_action_play_over_video"
android:title="#string/action_right_drawer"
app:showAsAction="ifRoom"/>
</menu>

No actions or icons showing on the actionbar; only in overflow

I'm not really new to programming but a complete fresh noob regarding android and java.
I recently made the decision to programm an Android App using this tutorial.
I got till the actionbar and can even display some actions like search and settings in the overflow. What I cant manage to do is to let an action appear on the actionbar(with or without icon).
I managed to it once with this tutorial but I somehow cant repeat it on my actual "Tutorialapp".
Here is my code:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.newapp.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:orderInCategory="1"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
</menu>
MainActivity.java:
package com.example.newapp;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.newapp.MESSAGE";
#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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSearch() {
Toast.makeText(this, "This is a Search Button", Toast.LENGTH_SHORT).show();
}
public void sendMessage (View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
try swapping the menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="101"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
I dont know why, but you need to specify in java code the action always. I had the same problem, and I solved it like that:
#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);
menu.findItem(R.id.actionSearch).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
Hope it solve your problem. :D!! Good Luck!

How to add action bar to Android app?

I just start with Android. Want to add action bar following the tutorial. I was added res/menu/main_activity_actions.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:NecoSeparator="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
NecoSeparator:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
In MainActivity.java i do:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
But Eclipse says that cant find : R.menu.main_activity_actions.
What's wrong with this code? I have to do something more with this xml file?
UPDATE
MainActivity.java
package com.example.necoseparator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
I Think main problem is to saving your menu layout, if you have an error in file its might happened. please change your menu file with my code and save that and import R file (make sure that is not android.R and its must be YourPackageName.R) then clean your project and run.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="filtered"
/>
<item
android:id="#+id/menu_load"
android:icon="#drawable/ic_luncher"
android:orderInCategory="200"
android:showAsAction="always"
android:title="update"/>
</menu>
Remove this line xmlns:NecoSeparator="http://schemas.android.com/apk/res-auto and try again
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
You can use Toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
and add list of menus

Categories