android devolpment linking buttons to other xml pages - java

I'm developing an app for android. The main screen has 6 buttons. Each button leads to another screen. I'm having trouble with the code to make the buttons do anything when clicked. this is what I have:
on the main page my button id is glass the page opened when clicked is glass.xml
android:onClick="Intent i = new Intent(FirstActivity.this, SecondActivity.class);"
and my scr folder I have the java activities FirstActivity.java
package install.fineline;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fine_line);
Button btnload = (Button) findViewById(R.id.glass);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
and SecondActivity.java
package install.fineline;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass);
}
}
what am i doing wrong?

On the button definition in the xml remove the onClick attribute.
The button click action is set in code so it doesn't need to be in the xml file.
There is sample code here showing how to do what you want.

Related

the Android Studio OnClickListener doesn't work

Nothing happens when I click the "start" button, the app suddenly closes. I think the View.OnClickListener doesn't work, but I can't find how to fix it.
package com.example.myquiz;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView title;
private Button start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = findViewById(R.id.main_title);
start = findViewById(R.id.ma_startB);
Typeface typeface = ResourcesCompat.getFont(this,R.font.blacklist);
title.setTypeface(typeface);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,CategoryActivity.class);
startActivity(intent);
}
});
}
}
I got this error in Logcat:
error screenshot
This is the code for the CategoryActivity class, this is where I get the error from, but I really don't see where the problem is.
package com.example.myquiz;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.GridView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class CategoryActivity extends AppCompatActivity {
private GridView catGrid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Categories");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
catGrid = findViewById(R.id.catGridView);
List<String> catList = new ArrayList<>();
catList.add("Cat 1");
catList.add("Cat 2");
catList.add("Cat 3");
catList.add("Cat 4");
catList.add("Cat 5");
catList.add("Cat 6");
CatGridAdapter adapter = new CatGridAdapter(catList);
catGrid.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home)
{ CategoryActivity.this.finish();}
return super.onOptionsItemSelected(item);
}
}
The click listener is working correct, looking at the error log the launching of the CategoryActivity class is causing the app crash:
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor...
This tells the ActionBar / ToolBar inside the CategoryActivity is setup twice. Probably (if not, please share the class code) you can either remove setSupportActionBar() or change the theme of the CategoryActivity to something like: Theme.MaterialComponents.Light.NoActionBar to see if you get the Activity running after the button click.

Open Another Activity in Android

I am new to Android Development and I have this problem of opening another activity from the main activity.
The problem is that whenever I click on the button, the app closes.
Below is my Java code.
package com.Notely.SplashScreenandAccounts;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ActivityWelcome extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Button signup_btn = (Button) findViewById(R.id.signup_btn);
signup_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, ActivitySignup.class);
startActivity(intent);
}
}
I need suggestions on what is the problem and on how to rectify that.
You should add your Activity(ActivitySignup.class) in your
AndroidManifest file.
Like this..
<Activity android:name = .ActivituSignup android:theme="AppTheme"/>
i hope this will help you .
If it not works you should add this line in your ActivitySignup.java at on create before super.onCreate()
setTheme(R.style.AppCompat);
send your logcat error to understanding best.
You should take a tour of Activity in
android.developers.com web

event can't run in android ( get errors)

I recently use android studio instead of eclipse.
but somthing is wrong!
these are main java code that run currectly in eclipse but now get errors!
package com.example.amir.myapplicationeeee;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(),Main2Activity.class));
finish();
}
});
setOnClickListener is hilighed red and error ";" excepted showed when want to runnin app in virtual device
If the code you provide here is exactly the same as you have in your project, then you should just add '}' on the end of your code - it closes the MainActivity class.
You should also have evertyhing implemented inside onCreate() method.
So it should look like:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new
Intent(this,Main2Activity.class));
finish();
}
});
}
}

#override error: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method

I wanted to navigate to a new page when clicking a button. Below are my both activities I have created. I am using eclipse and I am getting an error saying The method onClick(View) of type new View.OnClickListener(){} must override a superclass method on the main activity.
This is my main activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button grammerButton = (Button)findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
This is my second activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grammer);
Button grammerButton = (Button) findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View view) {
finish();
}
});
}
}
Removing the #override will remove the error, but then app is not working as intended.
Check this
OnClickListener() must override a superclass method?
Uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above
Have this import statement
import android.view.View.OnClickListener;
There is no need to remove #Override Annotation.
Also calling finish() is not necessary. The hardware back button does the job.
When you press back button in Grammar Activity your current activity is popped from the back stack and the previous activity in the back stack takes focus. So there is no need to call finish() on button click.
http://developer.android.com/guide/components/tasks-and-back-stack.html
Also if you have a Button with id grammar in activity_grammer.xml it is ok.
Make sure you have a button with id grammar in activity_grammer.xml
You can read the topic id
http://developer.android.com/guide/topics/ui/declaring-layout.html
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching.
Change "Compiler Compliance Level" to 1.6. of java from project properties.
Place this code::
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alayout);
Button grammerButton = (Button)findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blayout);
Button grammerButton = (Button) findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
You are suppose to import the ViewClickListener namespace in your code.
Just Press ctrl + shift + O and it will add the relevant and missing namespaces in your project.
All you need is to import the library for OnClickListener. Just press ctrl + Shift + O in your eclipse and it will import the import android.view.View.OnClickListener file for you.
Try this..
First, You havn't import OnClickListener
import android.view.View.OnClickListener;
and second one
Button grammerButton = (Button) findViewById(R.id.grammar);
you are giving same name for both Button Ids in different layouts. Make sure you have a button with id grammar in activity_grammer.xml present are not.

My app will only output "false", not the code I want

I used samples from similar code to make this, unfortunately I'm not to sure what I did wrong.
The purpose of this app is to output text entered in a field to a TextView, where I changed the color, when a button is pressed.
package edu.wmich.project3
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class Main extends Activity {
String txtResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button text =(Button) findViewById(R.id.btnColor0);
final TextView result = ((TextView) findViewById(R.id.txtResult));
text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
txtResult= getText(R.id.txtField0).toString();
result.setText(txtResult);
}
});
}
}
txtResult = (findViewById(R.id.txtField0)).toString();
will solve...
the problem is that you're using the method getText() from the Activity which has nothing to do with the TextField you're dealing with.
...what is the type of view of R.id.txtField0? I guess with this you can take it from here.

Categories