I am trying to create a very simple help page in my app. The layout xml for the page contains only a textview that displays the help info. In the Eclipse layout view, the layout looks perfect... however, when i try to load it in the emulator (via an OptionsMenu) it comes up as a blank black screen. What am I doing wrong??
Here is the Code:
//This is my Help page code
package com.soapbox.servicerec;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
public class HelpPage extends Activity {
private static final int INSERT_ID = Menu.FIRST;
private static final int ACTIVITY_CREATE=0;
protected void onCreate(){
setContentView(R.layout.help_page);
setTitle(R.string.help_title);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
return true;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createNote();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private void createNote() {
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
}
AND the layout XML...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/white">
<TextView android:textSize="16px" android:textColor="#android:color/black" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:gravity="center_horizontal" android:paddingLeft="10px"
android:paddingRight="10px" android:text="#string/help_text"/>
</LinearLayout>
You must have your onCreate() function defined as ::
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page);
setTitle(R.string.help_title);
}
Your onCreate method signature is wrong.
notice the difference below
your method protected void onCreate()
proper way #Override public void onCreate(Bundle savedInstanceState)
The #Override notation is optional, but very useful because it ensure the method signature matches one from a parent class, helping prevent these situations.
Related
After Seeing here Disable EditText context menu . I am able to Hide the context menu but unfortunately i am unable to select the text from the edittext. how do i do that i mean select text? Here is my code
import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
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 {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.myid);
editText.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear();
MenuInflater inflater=mode.getMenuInflater();
inflater.inflate(R.menu.mymenu,menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}
}
Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="pallob.example.com.customedit.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myid"
android:gravity="top|left"
/>
</android.support.constraint.ConstraintLayout>
change return true to False in onLongClick. it will work mate
I have found a solution of this problem both hiding the context menu and selecting the text . I have already answered it Here
I'm not able to dispay MenĂ¹ in my main Activity and all the other Activities.
This is my main.xml:
<LinearLayout
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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/cardview_dark_background">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardBackgroundColor="#color/cardview_light_background">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/articoli"
android:onClick="visualizzaArticoli"
android:background="#null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Articoli"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
This is my main.java file:
package com.mcsolution.easymanagementandroid.main;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Toast;
import com.mcsolution.easymanagementandroid.R;
import com.mcsolution.easymanagementandroid.beans.Program;
import com.mcsolution.easymanagementandroid.connection.MyDatabase;
import com.mcsolution.easymanagementandroid.utility.SyncDati;
public class Main extends Activity {
public MyDatabase db;
public ProgressDialog dialog;
public String url="";
private static final Intent SCAN_INTENT = new Intent("com.google.zxing.client.android.SCAN");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.care_home, menu);
return super.onCreateOptionsMenu(menu);
}
private void apriConnessioneDB(){
db=new MyDatabase(getApplicationContext());
db.open(); //apriamo il db
}
public void visualizzaArticoli(View view){
Intent intent = new Intent(this, com.mcsolution.easymanagementandroid.articoli.viewArticoli.class);
startActivity(intent);
}
}
This is my menu care_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_logout"
android:orderInCategory="100"
android:title="Logout"/>
</menu>
If I try to start my application, I don't have any error but I can't to see MenuBar. I have try to set a debug point into OnCreateOptionMenu method but, the method is never called.
How can I fixed it ?
You need to return true; in onCreateOptionsMenu() as shown in the code below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.care_home, menu);
return true;
}
Simply use this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.care_home, menu);
return true;
}
Your activity class extends Activity, not AppCompatActivity. The default theme for these activities does not include an Action Bar, which is where the menu would live. You will need to add a <Toolbar> to your activity's layout file, and then call setActionBar() from your onCreate() method.
For example, add this as the first child of your top-level <LinearLayout> tag:
<Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And add this just after you call setContentView() in your onCreate():
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setActionBar(toolbar);
Now you will have an Action Bar in your activity, and so your menu will appear.
You should return true instead of super like this:
#SuppressLint("RestrictedApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if (menu != null) {
}
return true;
}
Hope it helps.
You have to use both methods on your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
My objective is to have the user touch anywhere on the screen, and then to have the touch coordinates appear in a textview on the screen.
This is what I have so far.
activity_main.xml:
<LinearLayout 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:orientation="vertical"
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=".MainActivity" >
<TextView
android:id="#+id/touchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="To be filled" />
</LinearLayout>
Main Activity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.MyFirstApp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView)findViewById(R.id.textView1);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}
#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;
}
}
SOLVED My first problem is that on MainActivity.java, there is an error on line 18, in the code setContentView(R.layout.main);. Researching online, it looks like the way to fix this error is to change import android.R; to import your.application.packagename.R;. However, I do not have import android.R; in my .java file. Does anyone know how to fix this error?
EDIT Second, the textView only displays the touch coordinates when the first textview is clicked, it does not display the touch coordinates of a touchpoint anywhere on the screen (it only displays the touch coordinates of a touch point on the first text view). How can I change this so that the touch coordinates of anywhere on the screen are displayed?
Another Edit One idea I had for the second problem was giving the LinearLayout I had an id, such as android:id="#+id/touchLayout". I was then planning on modifying the .java file as such:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.MyFirstApp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView)findViewById(R.id.textView1);
// this is the view on which you will listen for touch events
final View touchLayout = findViewById(R.id.touchLayout);
touchLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}
#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;
}
}
Change the following:
setContentView(R.layout.activity_main);
Yup, the answer I had in the second edit was correct. I simply replaced gave the LinearLayout an id, and replaced the touchView id with the LinearLayout id
i put click event on this click_screen
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="click_screen" />
another activity's .java file (click_screen)
public void click_screen(View v)
{
Intent click_screen=new Intent(this, MainActivity.class);
startActivity(click_screen);
}
MainActivity.java
package com.amcct.amcostapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity
{
#SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/* #Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
Intent click_screen=new Intent(this, MainActivity.class);
startActivity(click_screen);
}
return super.onTouchEvent(event);
}*/
#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;
}
public void click_screen(View v)
{
Intent click_screen=new Intent(this, MainActivity.class);
startActivity(click_screen);
}
}
=====================================================================
.xml when i click anywhere it generates ^^ above click_screen
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:onClick="click_screen"
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=".Click_Page"
android:background="#drawable/img_1" >
</RelativeLayout>
See this my All Files....
Your code is in MainActivity and through intent you are again trying to go to the MainActivity.class? specify the activity to which you want to go to for eg:
Intent click_screen=new Intent(MainActivity.this, nextActivity.class);
startActivity(click_screen);
Try setting onClick for the parent view of your xml instead. Something like.-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="click_screen" >
...
</RelativeLayout>
Or just override onTouchEvent in your Activity like this.-
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Intent click_screen=new Intent(this, MainActivity.class);
startActivity(click_screen);
}
return super.onTouchEvent(event);
}
Myabe you can try this code, i make textview.setClickable(true)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textView1);
textView.setClickable(true);
textView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
startActivity(new Intent(this, MainActivity.class));
}
You want to move to a different activity when clicked anywhere on screen?then why are you setting the onclick on that textview?set the onclick attribute on the top relative layout/Linear layout instead of setting on the textview.
## Please add clickable property ##
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Click Me"
android:clickable="true"
android:onClick="click_screen" />
I'm trying to use droidParts, specifically the DI part. I'm testing 2 injections, InjectResource and InjectView, but the second one doesn;t seem to be working.
I have a simple default layout with a textView:
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
and here's my sample code:
import org.droidparts.annotation.inject.InjectResource;
import org.droidparts.annotation.inject.InjectView;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends org.droidparts.activity.Activity {
#InjectView(R.id.text1) private TextView txtView;
#InjectResource(R.string.app_name) private String appName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("testLog", appName);
txtView.setText("Test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The InjectResource works properly and logs the String, but when it gets to the setText("Test"); line I get a nullPointerException. Is there anything else that needs to be done to use InjectView?
The content view needs to be set in preInject() as onCreate() is called right after the injection has been performed. And Views can't be found without layout information.
So the code should look like this:
#Override
public void onPreInject() {
setContentView(R.layout.activity_main);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txtView.setText("Test");
}