First I create app of Navigation Drawer Activity and travel from fragment to new activity, Now I want to set Home Button on New activity which navigates to main activity of navigation drawer.
but when i run it from app it gives me this when Home button is click
but i want to go direct on MainActivity
Here
NewActivity.java
package com.shubham.navi_demo;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class NewActivity extends FragmentActivity {
private static Button Button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
onclickbutton();
}
public void onclickbutton(){
Button = (Button)findViewById(R.id.button);
Button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.MAIN");
startActivity(intent);
}
}
);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewActivity">
<intent-filter>
<action android:name="com.shubham.navi_demo.NewActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You don't have to start activity with Intent. Just call finish() in NewActivity, it will show you the Main Navigation drawer activity.
hope this helps.
Intent intent = new Intent(this,MainActivity.class);
Change
Intent intent = new Intent("android.intent.action.MAIN");
to
Intent intent = new Intent(this,MainActivity.class);
or call finish(); to finish() current Activity
Simple solution that you can provide users to navigate back to home screen is explained here.
developer.android.com/training/implementing-navigation/ancestral.html
Add this line to your 'AndroidManifest' under relevant activity declaration.
android:parentActivityName="com.example.your_main_activity"
Related
I am trying to change the main activity in android to another activity and playing a video in the new activity. but, even I changed in the manifest the main activity, it didn't show me the video when I'm starting the app. I tried to Check in the Logcat if the activity is still launched, and i didn't see any message.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mta2223">
<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/Theme.MTA2223">
<activity
android:name=".Entering"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityShowingPlayers"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
</application>
</manifest>
The new activity
package com.example.mta2223;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
public class Entering extends AppCompatActivity {
VideoView videoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entering);
videoView= (VideoView) findViewById(R.id.videoV);
videoView.setVideoPath("res/raw/intrologo.3gp");
videoView.start();
while (videoView.isPlaying()){
Log.d("HEy","hey");
}
videoView.stopPlayback();
Intent i = new Intent(this,MainActivity.class);
startActivity(i);
finish();
}
}
Try this
In this code get video path from raw folder
String path = "android.resource://com.example.mta2223" + "/" + R.raw.intrologo;
videoView.setVideoPath(path);
videoView.start();
I have been trying to go from an empty activity to a tab layout but the app keeps crashing once button is clicked. Without the empty activity the code worked fine.
Main activity (empty)
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void statsButton(View view){
Intent intent = new Intent(this, stats.class);
startActivity(intent);
}
}
Stats.java (where I want tabbed view to start)
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
public class stats extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private TabItem tab1,tab2,tab3;
public PageAdapter pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tab1 = (TabItem) findViewById(R.id.Tab1);
tab2 = (TabItem) findViewById(R.id.Tab2);
tab3 = (TabItem) findViewById(R.id.Tab3);
viewPager = findViewById(R.id.viewpager);
pagerAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 0 ) {
pagerAdapter.notifyDataSetChanged();
}
else if (tab.getPosition() == 1){
pagerAdapter.notifyDataSetChanged();
}
else if (tab.getPosition() == 2){
pagerAdapter.notifyDataSetChanged();
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
}
for some reason these errors are given in crash log once the stats button is clicked and I don't know what the problem is
crashlog
my manifest looks like this
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emptytab">
<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>
just add this to your AndroidManifest.xml
<activity android:name=".stats"> </activity>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emptytab">
<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=".stats" >
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Note :
So as not to be repeated this problem if you want to add new activity please follow these steps :-
right click > new > Activity > Empty Activity
I hope it will help you .
<activity android:name="stats" />
Add this line in your AndroidManifest.xml
i have one package with two class
MainActivity:
package co.edu.unimagdalena.projecto;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
String[] info;
private EditText nombre;
private EditText apellido;
private EditText email;
private EditText telefono;
private Button enviar;
final static String INFO = "co.edu.unimagdalena.projecto.informacion2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nombre=(EditText)findViewById(R.id.EditTextNombre);
apellido=(EditText)findViewById(R.id.EditTextApellidos);
email=(EditText)findViewById(R.id.EditTextCorreo);
telefono=(EditText)findViewById(R.id.EditTextTelefono);
enviar = (Button)findViewById(R.id.BtnEnviar);
enviar.setOnClickListener(this);
info=new String[4];
}
public void onClick (View v){
Intent intent = new Intent (this, informacion2.class);
intent.setClassName("co.edu.unimagdalena.projecto","co.edu.unimagdalena.projecto.informacion2");
startActivity(intent);
}
public void pasarActidadInfo (View v){
info [0] = nombre.getText().toString();
info [1] = apellido.getText().toString();
info [2] = email.getText().toString();
info [3] = telefono.getText().toString();
Intent act = new Intent (this, informacion2.class);
act.putExtra(INFO, info);
startActivity(act);
}
and other class call it informacion2:
package co.edu.unimagdalena.projecto;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.TextView;
import co.edu.unimagdalena.projecto.MainActivity;
public class informacion2 extends Activity {
String [] info;
TextView nombre,apellido,email,telefono;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.informacion2);
nombre = (TextView) findViewById(R.id.mostrarNombre);
apellido= (TextView) findViewById(R.id.mostrarApellido);
email = (TextView) findViewById(R.id.mostrarEmail);
telefono = (TextView) findViewById(R.id.mostrarTelefono);
Intent men = getIntent();
info = men.getStringArrayExtra(MainActivity.INFO);
nombre.setText(info[0]);
apellido.setText(info[1]);
email.setText(info[2]);
telefono.setText(info[3]);
}
}
and this is the AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.edu.unimagdalena.projecto">
<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>
</application>
<activity
android:name=".informacion2"
android:label="#string/app_name">
</activity>
</manifest>
**I already trying everything and the error is not corrected, i am new in android studio
I can fix the error but now android studio give me this error
java.lang.RuntimeException: Unable to start activity ComponentInfo{co.edu.unimagdalena.projecto/co.edu.unimagdalena.projecto.informacion2}: java.lang.NullPointerException: Attempt to read from null array**
Try including other activity inside application in manifest file like this
<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=".informacion2"
android:label="#string/app_name">
</activity>
</application>
setClassName takes a Package Context as first param setClassName(Context packageContext, String className):
In Android manifest
<activity android:name="co.edu.unimagdalena.projecto.informacion2">
And while starting the activity from your main activity
intent.setClassName("co.edu.unimagdalena.projecto","co.edu.unimagdalena.projecto.informacion2");
You are getting NullPointerException inside second activity, when you try to get values of Array. And you are filling array inside pasarActidadInfo method of first activity. The problem is that you are not calling pasarActidadInfo from anywhere after filling array in your first activity. So,your array would be null.
Try calling pasarActidadInfo method when you want to start new activity.
May be in onClick of button.
Always declare all android components (activities, service, receivers) inside the application tag / block and other configurations will be defined outside of application tag/block and inside manifest tag / block in Androidmanifest.xml.
Copy and paste this code in your manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.edu.unimagdalena.projecto">
<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=".informacion2"
android:label="#string/app_name">
</activity>
</application>
</manifest>
I have been following the mybringback tutorials on youtube and I tried implementing what I learned. Trying to get a button on my main page to open another page. Finally got the program to run without errors but now when I press the button nothing opens.
Main .xml file where my button is
<Button
android:id="#+id/btnChpt3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Appearance and Grooming Policies"
android:textSize="18sp"
android:textStyle="bold|italic"
android:gravity="center"
/>
Name of .xml file im trying to get to is chapter3.xml
Menu.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button chapterThree = (Button) findViewById(R.id.btnChpt3);
chapterThree.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.th3ramr0d.learnar670_1.CHAPTER3"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
And my manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.learnar670_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/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=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Chapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
So button ided as btnChpt3 wont open up my .xml file named chapter3.xml. Thanks for the help.
Here is my Chapter3.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.os.Bundle;
public class Chapter3 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter3);
}
}
Here is my MainActivity.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#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) {
// 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);
}
}
Copy this and paste in your AndroidManifest and try,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.learnar670_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Menu"
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=".Chapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
To help you understand the problem,
<category android:name="android.intent.category.LAUNCHER" />
The code above in the AndroidManifest defines the activity to be launced when the App Icon is pressed. As per your earlier manifest, it launches the activity MainActivity which also sets it setContentView(R.layout.activity_main); by default as the IDE creates a Hello World program.
So when you launch you app, its MainActivity (that looks the same layout you have designed) which is loading and not Menu activity which you want to load. Hence making few changes in the manifest where we declare the Menu activity as the launcher now launches Menu activity which has the piece of code to process your button click.
I hope this helped!
Actually you can try a more convenient way of starting activities inside your application:
startActivity(new Intent(Menu.this, Chapter3.class))
Also you can read more how it works here:
http://developer.android.com/training/basics/firstapp/starting-activity.html
Good day.
Try replacing the line :
startActivity(new Intent("com.th3ramr0d.learnar670_1.CHAPTER3"));
with the code below :
Intent intent = new Intent(Menu.this, Chapter3.class);
startActivity(intent);
Try reverting your code to the original code, copy your layout activity_main.xml and rename it menu.xml.
now in the layout menu.xml change this line:
android:text="Appearance and Grooming Policies"
to:
android:text="Go to menu"
and the line:
android:id="#+id/btnChpt3"
with:
android:id="#+id/btnMenu"
and replace the line:
setContentView(R.layout.activity_main);
in Menu.java with:
setContentView(R.layout.menu.xml);
finally in MainActivity.java add the following to your oncreate method:
Button btnGoToMenu = (Button) findViewById(R.id.btnMenu);
btnGoToMenu.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Menu.class);
startActivity(intent);
}
});
and re-run the application.
Added an OnClickListener to my button. I would like it to switch to 'Step1' activity when I click the button. When I debugged it, I clicked on the button but nothing happened. It stayed on 'MyActivity' activity. Here's my main activity:
package apk.olley102.roothelp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyActivity extends Activity {
Button step1btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
step1btn = (Button) findViewById(R.id.step1btn);
step1btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openStep = new Intent("apk.olley102.roothelp.Step1");
startActivity(openStep);
}
});
}
}
I added the button in the activity_main.xml and associated the layout to the activity. Also added MainActivity.java and Step1.java to the AndroidManifest.xml
Any help would be appreciated!
Usually to launch an activity you specify the current one instance and the one you want to open. In your case it would be something like this:
Intent openStep = new Intent(MyActivity.this, Step1.class);
startActivity(openStep);
If you still want to launch it using an action string, check that your activity declares the intent filter in AndroidManifest.xml:
<activity android:name=".Step1">
<intent-filter>
<action android:name="apk.olley102.roothelp.Step1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Also note that the action string does not need to match your activity name: you could name it apk.olley102.roothelp.GOTOSTEP1 for example and use the same string when you create the intent.
I have finally got my button working. I had to use '.DEFAULT' in the category of my second activity.
<activity
android:name=".Step1" android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="apk.olley102.instructroot.Step1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Also I had to specify the activity '.this' in my intent.
step1btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openStep = new Intent(MyActivity.this, Step1.class);
startActivity(openStep);
}
});
I debugged my app and now the button launches Step1 on click. Thanks for all the help! :)