Android Second activity does not load - android

Sorry for my English.
I'm trying to use a second activity in Android, but it doesn't load. It's like the code jump it. Below, you can see the code. Thanks.
1- First Activity
public class MainActivity extends Activity
{
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
});
}
2- Second Activity
public class activity_janela1 extends Activity
{
public void OnCreate(Bundle saveInstaceState)
{
super.onCreate(saveInstaceState);
setContentView(R.layout.activity_janela1);
}}
3- First layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btenviardados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
4- Second layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutFormulario"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Im screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
5- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.seven.reader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name="com.example.seven.reader.activity_janela1" >
</activity>
</application>
</manifest>

You have created a method within your oncreate. Remove that and leave it in onCreate.
///public void addListenerOnButton() {
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
});
You are misunderstanding how to use methods and scope. I recommend you do a read up on these things.

The problem is with your method addListenerOnButton().
You could define a method which will be executed when button is clicked via xml just add onClick attribute where you have defined your button.
As:-
<Button
android:id="#+id/btenviardados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to another screen"
android:onClick="click" />
And define click method in MainActivity.java
as:-
public void click(View view)
{
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
Or you can add OnClickListener in you onCreate method of MainActivity.

Try this, may help you:
public class MainActivity extends Activity
{
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(getApplicationContext, activity_janela1.class);
startActivity(intent);
finish();
System.out.println("ebadasdadas");
}
});
}
}

Related

Button doesn't redirect to activity but crashes app instead

My button doesnt open up a new activity, the app crashes instead. I've even copied the source code from http://www.androidbegin.com/tutorial/android-button-click-new-activity-example/ and try running it on my own but still the app crashes. I cant seem to find the problem.
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
button = (Button) findViewById(R.id.MyButton);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
secondActivity.class);
startActivity(myIntent);
}
});
}
}
XML BUTTON
<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" >
<Button
android:id="#+id/MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
YOUR CODE IS FINE!
The most likely cause of this behavior is that secondActivity is not registered in your manifest. Check whether it is and try again. If it's not, you can simply register it by adding the line below inside the application tag of your manifest.xml file.
<activity android:name=".secondActivity" />
Re-run your code and try again. It'd most likely work this time.
I hope this helps.. Merry coding!
As per the tutorial link you provided, second activity's name is NewActivity.class. In your code, it looks like you modified it to secondActivity.class.
So ensure to have it manifest also
<activity android:name=".secondActivity" >
</activity>
And always use PascalCase for Classes and camelCase for methods
Place this code snippet:
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
button = (Button) findViewById(R.id.MyButton);
// Capture button clicks
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
secondActivity.class);
startActivity(myIntent);
}
});
}
}
Register secondActivity in manifest.xml
<activity
android:name=".secondActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
This may help you

Add Fragment overlay on activity's view

I am new bie in android.
Below is my senerio what i have and what i actually need.
I have one activity where i have declare one custom view which contains a set of button which click event display respective fragments.
This custom view will be appear on top of each fragment.
Now suppose on first button of custom view i am display fragment a fragment which display list-view..
on click on list-view it show another fragment i.e detail fragment
In detail fragment.. i have one button where i need to show an fragment overlay on main activity ... as on full screen.. how can i achive this?
In my opinion easiest way lead to this behavior is use Activity Overlay or DialogFragment. Example:
Activity Overlay
MainActivity.java
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.buttonShow);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
MainActivity.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:id="#+id/mainLayout"
tools:context=".MainActivity"
android:background="#android:color/holo_orange_dark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Activity"
android:textSize="30dp"
android:layout_centerInParent="true"/>
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:text="show"
android:id="#+id/buttonShow"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
SecondActivity.java
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
SecondActivity.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:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".SecondActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="30dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>
Styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="TransparentFloatingActivity" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arturszymanski.test" >
<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:theme="#style/TransparentFloatingActivity"
android:name=".SecondActivity"
android:label="#string/title_activity_second" >
</activity>
</application>
</manifest>
DialogFragment
MyDialogFragment.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyDialogFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FullScreen Fragment"
android:textSize="30dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
MyDialogFragment.java
public class MyDialogFragment extends DialogFragment
{
private int margin;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
margin = 10;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my_dialog, container, false);
}
#Override
public void onResume() {
int dialogHeight = MainActivity.displayMetrics.heightPixels - (margin * 2) - MainActivity.StatusBarHeight;
int dialogWidth = MainActivity.displayMetrics.widthPixels - (margin * 2);
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
super.onResume();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity
{
public static DisplayMetrics displayMetrics;
public static int StatusBarHeight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
StatusBarHeight = getStatusBarHeight();
Button button = (Button) findViewById(R.id.buttonShow);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getFragmentManager(), "Dialog");
}
});
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
Other Files
Other files are the same as in example above.
If i follow your question than, You should use a RelativeLayout as overlay in main activity's layout. and set visibility according to your need.
layout_activity_main.xml
<RelativeLayout
android:width="match_parent"
android:height="match_parent">
<LinearLayout>
// your toolbar
// You fragment container
// your main layout goes here
</LinearLayout>
<RelativeLayout
android:id="#+id/overlay"
android:width="match_parent"
android:height="match_parent">
</RelativeLayout>
</RelativeLayout>
like this :
<LinearLayout id = "#+id/container">
<ToolBar id ="#+id/toolbar>
</ToolBar>
<FrameLayout id ="#+id/main_containt"/>
</LinearLayout>
getSupportFragmentManager().add(new Fragment(),R.id.container).commit();
use container as the container of fragment;

About Button in Layout

I placed a Button in a layout
here is my xml code
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#ff0000"/>
and wrote the following code in activity:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
}
public void clkBtn(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
When I run this code, I am getting white blank screen (without any button). Can any one tell me what is wrong with my code?
Update your first.xml under res->layout folder like this
<?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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clkBtn"
android:text="Click Me" />
</LinearLayout>
To get the Button event you need to first register your Button for that. The code which you have written is right but to invoke that method you have to add the property android:onClick="clkBtn" in your layout file.
OR
If you don't want to use this way then you can also explicitly invoke the event by registering your Button in your class as below:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
And also to launch your activity make sure have added your activity as launcher in your manifest file as below .
<activity android:label="#string/app_name"
android:name="Basic" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is My Answer. In XML file Button should be like this:
<Button
android:id="#+id/ID of you button "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name To Display Button name" />
In the MainActivity OR In any activity (in which you have called XML):
public class MainActivity extends Activity {
public void oncreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout."Your XML file in which Button is.");
Button btn = (Button)findViewById(R.id."Your button Id");
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do your onclick program here
}
});
}
}

XML Parser Android - Calling New Activity

I have a question about xml parser in android. I am creating button information in the xml. I am getting its height,width,color and so on. I am creating all buttons in a function. Now i want to do the following. When i click the buttons, new android page opens. As far as i know i will do it by creating new activities.
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
I am writing this on my function which i create all buttons.when i do this it does not work.can somebody help me?
In your Activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
View btnTapOnMe = findViewById(R.id.btnTapOnMe);
btnTapOnMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
}
}
view_main.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" >
<Button
android:id="#+id/btnTapOnMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap on Me" />
</RelativeLayout>
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourpackage"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.yourpackage.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.yourpackage.MainActivity2"/>
</application>
</manifest>
Should work like a charm
UPDATE:
Don't ever use AbsoluteLayout it is deprecated
This class was deprecated in API level 3.
Use FrameLayout, RelativeLayout or a custom layout instead.
First of all change it to Frame or Relative layout.
Here what i get in my example below :
public class MainActivity extends FragmentActivity {
public void AddAllButtons() {
AbsoluteLayout Layout = (AbsoluteLayout) findViewById(R.id.layout1);
Button btn = new Button(this);
btn.setText("Test case");
Layout.addView(btn);
AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) btn.getLayoutParams();
absParams.x = 100;
absParams.y = 100;
absParams.width = 150;
btn.setLayoutParams(absParams);
btn.setBackgroundColor(Color.GREEN);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SearchActivity.class);
startActivity(i);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
AddAllButtons();
}
}
and here is xml file :
<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">
<Button
android:id="#+id/btnTapOnMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap on Me"/>
<AbsoluteLayout
android:id="#+id/layout1"
android:layout_below="#+id/btnTapOnMe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</AbsoluteLayout>
</RelativeLayout>

Do I have to start some kind of sqlite server before using it in android application

well, almost everything is in the question, I have a problem with program which should use the sqlite, it still returns the "unexpected error", I was just wondering do I have to start some kind of server, deamon or service?
androidManifest[ snippet ]
<activity android:name=".About">
android:label="#string/about_title"
android:theme="#android:style/Theme.Dialog" >
</activity>
<activity android:name=".Exit">
andorid:label="#string/exit_title">
</activity>
<activity android:name=".Options">
</activity>
<activity android:name=".Start">
</activity>
<activity android:name=".Create">
</activity>
</application>
AndroidSQLite
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sql);
TextView listContent = (TextView)findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
mySQLiteAdapter.insert("ABCDE");
mySQLiteAdapter.insert("FGHIJK");
mySQLiteAdapter.insert("1234567");
mySQLiteAdapter.insert("890");
mySQLiteAdapter.insert("Testing");
mySQLiteAdapter.close();
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
String contentRead = mySQLiteAdapter.queueAll();
mySQLiteAdapter.close();
listContent.setText(contentRead);
}
sql.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
/>
<ListView
android:id="#+id/contentlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
main.xml [snippet]
<Button
android:id="#+id/where_button"
android:onClick="WhereCl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/where_label" />
code responsible for button Where Am I?
mainActivity.java
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View startButton = findViewById(R.id.start_button);
View whereButton = findViewById(R.id.where_button);
View aboutButton = findViewById(R.id.about_button);
View exitButton = findViewById(R.id.exit_button);
}
public void OptionsCl(View v){
startActivity(new Intent(this,Options.class));
}
public void WhereCl(View v){
startActivity(new Intent(this,AndroidSQLite.class));
}
public void StartCl(View v){
Intent idd = new Intent(this,Start.class);
startActivity(idd);
}
public void AboutCl(View v){
startActivity(new Intent(this,About.class));
}
public void ExitCl(View v){
finish();
}
}
the error I receive is similar to this which come up with unregistred action, but the action is registred [ check the code ]
it is just "the application blabla (menu.dot) has stopped unexpectedly. Please try again" Force close.
Android SQLite is readymade library. So you don't need to start any Service, server or demon. Just to initiate a database class extending SQLite DatabaseHelper class and use it.
Thanks.

Categories

Resources