I thought I did everything right but when I click the button I get a blank page instead of a page with a text. I want that the creation.xml comes up after clicking the button on the main xml. What did I wrong ?
My main activity xml
<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=".MainActivity"
android:background="#drawable/creeper" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/hello"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:textSize="20sp"
android:text="#string/button1_text" />
</RelativeLayout>
My main activity java
package com.berkcoop.deneme;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(MainActivity.this, Creation.class);
startActivity(intent1);
}
});
}
My creation xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Creations"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.berkcoop.deneme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.berkcoop.deneme.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.berkcoop.deneme.Creation"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
and my creation java
package com.berkcoop.deneme;
import android.app.Activity;
public class Creation extends Activity{
#Override
public void setContentView(int layoutResID) {
// TODO Auto-generated method stub
super.setContentView(R.layout.creation);
}
}
Thank you..
Why do you override the setContentView method in your Creation class? Since you don't call it, it won't set the correct view.
You should call setContentView from onCreate, and as far as I know, there is no need to override setContentView.
Try this in Creation:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.creation);
}
Related
We are having a resource error when we try to run our code.
package com.example.searchtest;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class GoogleSearchIntentActivity extends Activity {
private EditText editTextInput;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextInput = (EditText) findViewById(R.id.editTextInput);
}
public void onSearchClick(View v)
{
try {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = editTextInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
}
}
<?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:padding="10dp">
<edittext
android:id="#+id/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search text">
<requestfocus>
</requestfocus></edittext>
<button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_gravity="center"
android:onclick="onSearchClick"
android:layout_margintop="10dp">
</button></linearlayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchtest">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".GoogleSearchIntentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
these are our mainActivity.java, activity_main.xml, and AndroidManifest.xml codes from our app.
The error is within our public class GoogleSearchIntentActivity extends Activity
We are high school students trying to create an image search app for our computer science capstone class, we have been trying to fix this error without success, and our teacher is completely clueless.
Components in your xml file must have a first capitalized letter.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp">
<EditText
android:id="#+id/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search text">
<requestFocus>
</requestFocus></EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_gravity="center"
android:onclick="onSearchClick"
android:layout_margintop="10dp">
</Button></LinearLayout>
I want to create a java page with a Button and a TextBox
So that when the user writes the number 1 in the TextBox and then presses the Button
It will redirect him to open a pdf file from
ex:
www.mywebsite / myfile1.pdf
When the user writes the number 2 in the TextBox and then presses the Button
It will redirect him to open pdf file from
www.mywebsite / myfile2.pdf
Thank you
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_main.xml
<?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=".MainActivity">
<EditText
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter file number"
android:inputType="number"
app:layout_constraintBottom_toTopOf="#+id/Open"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.507" />
<Button
android:id="#+id/Open"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:layout_marginBottom="276dp"
android:text="Open"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Main_activity.java
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button Open;
EditText Filenumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Open=(Button)findViewById(R.id.Open);
Filenumber=(EditText)findViewById(R.id.editText);
Open.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
public void open(){
if(Filenumber.equals("1234"))
Uri.parse("http://www.xmlpdf.com/manualfiles/hello-world.pdf");
if(Filenumber.equals("12345"))
Uri.parse("http://www.google.com");
{
}
}
}
If your pdf source is a link, you can either download the pdf first or open it using WebView. Here is some link that might be of help.
I am writing an app that will display a menu of juices at a vape shop.
I have the main screen which displays the name and a button that once clicked will send the user to a new screen displaying the menu.
I am having trouble setting up the button click and when i run the app it crashes after the button is clicked
any input would be appreciated!
MainActivity
package com.example.vitoriano_vaz.easybayvapes;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public void sendMessage(View view){
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startJuiceMenu(View view) {
Log.d("MyApp", "button clicked");
}
}
Second activity once the button is clicked
package com.example.vitoriano_vaz.easybayvapes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
public class juiceMenu extends AppCompatActivity {
ArrayList<String> juiceMenu = new ArrayList<String>(50);
private static String VALUE = "myValue";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
juiceMenu.add("#1 Blueberry Bombshell");
juiceMenu.add("#2 Richie Rich");
juiceMenu.add("#3 Chiquita");
juiceMenu.add("#4 Afternoon Delight");
juiceMenu.add("#5 Poppin Otters");
juiceMenu.add("#6 Viva La Sangria");
juiceMenu.add("#7 Okole Maluna");
juiceMenu.add("#8 Carmen Miranda");
juiceMenu.add("#9 Pomalade");
juiceMenu.add("#10 Izual");
juiceMenu.add("#11 Butter Stotch ");
juiceMenu.add("#12 Blue Bulls");
juiceMenu.add("#13 Grape Ape");
juiceMenu.add("#14 Bruce Juice");
juiceMenu.add("#15 Doc Holiday");
juiceMenu.add("#16 Peachy Keen");
juiceMenu.add("#17 Hula");
juiceMenu.add("#18 New York");
juiceMenu.add("#19 Al Gore");
juiceMenu.add("#20 Lux Charms");
juiceMenu.add("#21 Sailor jack");
juiceMenu.add("#22 Get Him to the Greek");
juiceMenu.add("#23 Key We Lie Chi");
juiceMenu.add("#24 Spring Fling");
juiceMenu.add("#25 Gumby");
juiceMenu.add("#26 Chai-Milk");
juiceMenu.add("#27 Mr. Bean");
juiceMenu.add("#28 50 Shades of Orange");
juiceMenu.add("#29 Blue Waffles");
juiceMenu.add("#30 Enigma");
juiceMenu.add("#31 Mr. Freeze");
//juiceMenu.add("#32 New Flavor"); need to update to get newest flavor
}
}
Activity_main
<?xml version="1.0" encoding="utf-8"?>
<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.vitoriano_vaz.easybayvapes.MainActivity"
android:id="#+id/main_view">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:text="Welcome to East Bay Vapes"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="20sp"
android:id="#+id/textView" />
<Button
android:id="#+id/juicemenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="#string/JuiceMenu"
android:layout_marginTop="42dp"
android:layout_alignParentTop="true" />
</RelativeLayout>
Activity_juice_menu
here i have a LinearLayout for the ArrayList I declared in juicemenu.java class
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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.vitoriano_vaz.easybayvapes.juiceMenu">
<ListView
android:id="#+id/JuiceMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vitoriano_vaz.easybayvapes">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".juiceMenu"></activity>
</application>
</
The Intent that you are building needs to reference your own Activity classes.
The first parameter is a Context and the second is the Class of the Activity you want to start so it should be the following.
Intent intent = new Intent(this, juiceMenu.class);
Since your question has already been answered, I thought I'd suggest a more efficient means of populating your array to remove that blemish from your code. For example you can use a string-array resource. With that you simply add the values using a single code line. As an example:
String[] juiceArray = getResources().getStringArray(R.array.JuiceTextArray);
The array values themselves are populated in your arrays.xml file (found in values folder) as such:
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation">
<string-array name="JuiceTextArray" tools:ignore="MissingTranslation">
<item name="Juice1">#1 Blueberry Bombshell.</item>
<item name="Juice2">#2 Richie Rich.</item>
[etc., etc.]
</string-array>
</resources>
i'm fairly new to coding but decided i would take the huge plunge into the ocean without knowing how to swim, anyway my button keeps failling to open my new activity when i click on it, instead it just crashes. when I run it on my emulator i get a message saying that it has stopped. I'm using a genymotion emulator and the darcula android studio version.
My main activity.xml:
<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="wrap_content" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#drawable/background_black">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unpressed_button"
android:background="#drawable/unpressed"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
My second Activity.xml:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/background_black"
tools:context="quirkykoders.flash_flash.Pressed_Button">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pressed_button"
android:background="#drawable/pressed"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="quirkykoders.flash_flash" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Pressed_Button"
android:label="#string/title_activity_pressed__button" >
<intent-filter>
<action android:name="android.intent.action.PRESSED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
My java
package quirkykoders.flash_flash;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static Button button_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_sbm = (Button)findViewById(R.id.unpressed_button);
button_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent ("quirkykoders.flash_flash.Pressed_Button");
startActivity(intent);
}
}
);
}
}
Any help will be greatly appreciated
When you want to create A new Activity you have to send two parameters in the intent object First is currentActivity name and the second is CallingActivity name
Intent intent=new Intent(MainActivity.this,Pressed_Button.class);
startActivity(intent);
I have tried editing the code as it would re-install the application on the emulator but again I'm getting the same error again and again.
It's saying edit source lookup path and showing a button for it.
<?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:background="#drawable/images"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter the text here"
android:textColor="#000000"
android:textSize="30dp" />
<EditText
android:id="#+id/textReader"
android:layout_width="match_parent"
android:layout_height="150dp"
android:inputType="textMultiLine"
/>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/startReading"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Start reading"
android:textSize="15dp" />
<Button
android:id="#+id/clearBox"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Clear Box"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7.58" >
</RelativeLayout>
</LinearLayout>
<EditText
android:id="#+id/textDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
That was the main.xml file above.
package a.fr.read;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class FastreaderActivity extends Activity {
/** Called when the activity is first created. */
Button startReading,clearBox;
EditText textReader;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startReading=(Button)findViewById(R.id.textReader);
clearBox=(Button)findViewById(R.id.textReader);
textReader=(EditText) findViewById(R.id.textReader);
clearBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textReader.setText("");
}
});
startReading.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
And this is the java code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.fr.read"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name=".FastreaderActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And that was Android manifest file.
First clean your project and refresh that project(In Eclipse- Project-clean)for refresh(go that projectName(ListVeiwdemo) and right click and refresh option will come.)