Error "unable to find explicit activity class" when starting new Activity - android

I'm trying to start a activity from another activity. Finally, it doesn't work and fails with error of "Unable to find explicit activity class". So far, i was trying to find out a solution from previous questions but i was unable to find out a correct answer for my question.
This is my code,
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.acer.explicitintent.MainActivity">
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn =(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,ActivityTwo.class);
i.putExtra("value","chanuka");
startActivity(i);
}
});
}
}
activity_two.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">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
ActivityTwo.java
public class ActivityTwo extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
textView = (TextView)findViewById(R.id.tv);
Bundle bundle = getIntent().getExtras();
textView.setText(bundle.getString("value"));
}
}
Any help is appreciated. I'm newly to android.

you have not defined your ActivityTwo inside your Manifest , thats why you are getting this error ,
define your ActivityTwo in your Manifest File.
Sample Code
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#mipmap/ic_launcher"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<!-- Other code for your activities etc-->
<activity
android:name=".activity.ActivityTwo"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden" />
</application>

Related

How to go to Another activity when button clicked (button in a tab)

I created a Tab Layout with 3 sections. The first one is for checking the grammar and the second one is for lessons, the second tab is consist of 6 buttons for 6 lessons and I wanted the user to click the button and go to another activity or class. But I didn't get the output that I wanted. I tried different ways to open the activity from a fragment class but nothing happened.
What should I do in order to go to another class with a tab layout?
Here's my code
secondFragment_grammarTips
public class secondFragment_grammarTips extends Fragment {
public secondFragment_grammarTips() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_second_fragment_grammar_tips, container, false);
Button lesson1 = (Button) view.findViewById(R.id.button1);
lesson1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent less1 = new Intent(getActivity(), Lesson1_GramarTips.class);
less1.putExtra("some", "some data");
startActivity(less1);
}
});
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_second_fragment_grammar_tips, container, false);
return view;
}
}
Lesson1_GrammarTips
public class Lesson1_GramarTips extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson1__gramar_tips);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
if(bundle.getString("some") != null){
Toast.makeText(getApplicationContext(), "data: " + bundle.getString("some"), Toast.LENGTH_SHORT).show();
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="renelyn.austria.capstonetwoproject">
<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"
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=".Lesson1_GrammarTips"></activity>
</application>
</manifest>
fragment_second_fragment_grammar_tips.xml
<FrameLayout 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"
tools:context="layout.secondFragment_grammarTips"
>
<!-- TODO: Update blank fragment layout -->
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:text="#string/hello_blank_fragment" />-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/button1"
android:layout_width="385dp"
android:layout_height="110dp"
android:text="Lesson 1"
/>
<Button
android:id="#+id/button2"
android:layout_width="385dp"
android:layout_height="110dp"
android:text="Lesson 2"
/>
<Button
android:id="#+id/button3"
android:layout_width="385dp"
android:layout_height="110dp"
android:text="Lesson 3"
/>
<Button
android:id="#+id/button4"
android:layout_width="385dp"
android:layout_height="110dp"
android:text="Lesson 4"
/>
<Button
android:id="#+id/button5"
android:layout_width="385dp"
android:layout_height="110dp"
android:text="Lesson 5"
/>
<Button
android:id="#+id/button6"
android:layout_width="385dp"
android:layout_height="110dp"
android:text="Lesson 6"
/>
</LinearLayout>
</ScrollView>
</FrameLayout>
Whenever you create a fragment ,it(fragment) should have activity hosting it. I didn't find any activity other than "Lesson1_GrammarTips" in Manifest file. And Lesson1_GrammarTips(activity) is not hosting any fragment. Go through this.

How to properly make an activity FullScreen - Issues with Views when trying to hide all bars

I have a simple activity that I want to display in full screen - removing status bar, action bar and title bar.
I have few issues:
During the 'hide' operation, when the activity launches - there is a noticeable lag. It seems that first it hides the ActionBar, and only afterwards removes entirely from the layout.
I don't mind that, but the operation is noticeable and there's a lag between the hiding phase to the removal phase.
How do I get rid of the lag?
I want the activity to be in fullscreen also when resuming to it (e.g. going to another app, then back to this activity from Recent view). Problem is, when I implement onResume() to make the hidings - the activity behaves odd - TextView isn't showing, ImageView is suddenly not responding to clicks.
When not using onResume() method at all - everything is working great, as expected.
My Activity code:
public class ListItemActivity extends AppCompatActivity implements View.OnClickListener {
private final static String TAG = "ListItemActivity";
private ScrollView mSvLyrics;
private TextView mTvLyrics;
private TextView mErrorMessageDisplay;
private ProgressBar mLoadingIndicator;
/**
* Shows error message view while hiding other results related views
*/
private void showErrorMessage(String s) {
mLoadingIndicator.setVisibility(View.INVISIBLE);
mSvLyrics.setVisibility(View.INVISIBLE);
mErrorMessageDisplay.setText(s);
mErrorMessageDisplay.setVisibility(View.VISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_list_item);
/* Set click listener for close image */
findViewById(R.id.iv_close).setOnClickListener(this);
/* Init fields and needed params */
mSvLyrics = findViewById(R.id.sv_lyrics);
mTvLyrics = findViewById(R.id.tv_lyrics);
mErrorMessageDisplay = findViewById(R.id.tv_item_error_message_display);
mLoadingIndicator = findViewById(R.id.pb_item_loading_indicator);
final Intent intent = getIntent();
/* Set Track properties in the views */
((TextView)findViewById(R.id.tv_item_artist)).setText(intent.getStringExtra(LyricsAPI.KEY_ARTIST_NAME));
((TextView)findViewById(R.id.tv_item_title)).setText(intent.getStringExtra(LyricsAPI.KEY_TRACK_NAME));
/* Fetch Lyrics for the track */
URL lyricsUrl = AidUtils.buildUrl(intent.getStringExtra(LyricsAPI.KEY_TRACK_ID), LyricsAPI.RequestType.TRACK_LYRICS_GET);
new AsyncFetchLyrics().execute(lyricsUrl);
}
#Override
protected void onResume() {
super.onResume();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_list_item);
}
#Override
public void onClick(View v) {
finish();
}
public class AsyncFetchLyrics extends AsyncTask<URL, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
mErrorMessageDisplay.setVisibility(View.INVISIBLE);
mSvLyrics.setVisibility(View.INVISIBLE);
mLoadingIndicator.setVisibility(View.VISIBLE);
}
#Override
protected String doInBackground(URL... urls) {
URL url = urls[0];
String response = null;
try {
response = AidUtils.getResponseFromHttpUrl(url);
} catch (IOException e) {
e.printStackTrace();
return null;
}
String lyrics = null;
try {
lyrics = AidUtils.getLyricsFromJsonStr(response);
} catch (LyricsAPIException e) {
Log.e(TAG, "Failed to fetch data from lyrics API", e);
}
return lyrics;
}
#Override
protected void onPostExecute(String lyrics) {
if(null == lyrics) {
showErrorMessage(getString(R.string.error_message));
return;
}
if(lyrics.isEmpty()) {
showErrorMessage(getString(R.string.no_results));
return;
}
mLoadingIndicator.setVisibility(View.INVISIBLE);
mErrorMessageDisplay.setVisibility(View.INVISIBLE);
mTvLyrics.setText(lyrics);
mSvLyrics.setVisibility(View.VISIBLE);
}
}
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:padding="0dp"
android:gravity="center">
<ImageView
android:id="#+id/iv_close"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:src="#android:drawable/ic_menu_close_clear_cancel"
android:scaleType="fitCenter"
android:clickable="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/tv_item_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/example_text"
android:textSize="35sp"
android:textStyle="bold"
android:textColor="#color/app_text_color"
android:textAlignment="center"
android:maxLines="2"
android:ellipsize="end"
android:padding="5dp"
android:background="#android:color/transparent">
</TextView>
<TextView
android:id="#+id/tv_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/example_text"
android:textSize="30sp"
android:textAlignment="center"
android:textColor="#color/app_text_color"
android:maxLines="1"
android:ellipsize="end"
android:padding="5dp"
android:background="#android:color/transparent">
</TextView>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/sv_lyrics"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true"
android:layout_marginTop="40dp"
android:layout_marginBottom="25dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:visibility="invisible">
<TextView
android:id="#+id/tv_lyrics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textStyle="italic"
android:fontFamily="monospace"
android:text="#string/example_text"
android:textAlignment="center"
android:textSize="16sp"/>
</ScrollView>
<TextView
android:id="#+id/tv_item_error_message_display"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="20dp"
android:text="#string/error_message"
android:textStyle="italic"
android:textColor="#android:color/holo_red_light"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/pb_item_loading_indicator"
android:layout_height="42dp"
android:layout_width="42dp"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.bilyrics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:debuggable="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="com.example.android.bilyrics.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.bilyrics.ListItemActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="#android:style/Theme.AppCompat.Light.NoActionBar" >
</activity>
<meta-data
android:name="android.support.VERSION"
android:value="26.1.0" />
<meta-data
android:name="android.arch.lifecycle.VERSION"
android:value="27.0.0-SNAPSHOT" />
</application>
</manifest>
You can do it by applying the theme for the activity.
In style.xml
<style name="FullScreenTheme"
parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
And in manifest.xml
<activity android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#style/FullScreenTheme"/>
The easiest way to make Fullscreen Activity is by this code in OnCreate:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

The intent passing one data twice(actually should pass 2 data)

I'm new in android and I just write a test app that should take the texts of the two buttons & change the text of them to each other in the new activity with intent.
but when I run the app and click the first button, it goes to second activity and in the second activity it just showing only the text of that button I just clicked on for both 2 buttons! actually, it should change the text of buttons to each other.(I mean the text of button 1 should display in button 2 and vice versa)
I check every question about the intent here, but my case is waired!!! because it seems everything is right! also, I check the code many times, I don't realize what's wrong!!!!
Thanks.
Here is the code:
MainActivity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button buttonone,buttontwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeText(View view){
buttonone=(Button) view;
buttontwo=(Button) view;
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("button1text",buttonone.getText().toString());
intent.putExtra("button2text",buttontwo.getText().toString());
startActivity(intent);
}
}
activity_main.xml
<?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:id="#+id/activity_main"
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.myb.assignment.MainActivity">
<Button
android:text="One"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="35dp"
android:layout_marginTop="46dp"
android:id="#+id/buttonone"
android:onClick="changeText"/>
<Button
android:text="Two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="59dp"
android:id="#+id/buttontwo"
android:onClick="changeText"
android:layout_alignBaseline="#+id/buttonone"
android:layout_alignBottom="#+id/buttonone"
android:layout_alignParentEnd="true" />
</RelativeLayout>
SecondActivity
public class SecondActivity extends Activity {
Button button1,button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
#Override
public void onResume(){
super.onResume();
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
String intentData1=getIntent().getExtras().getString("button1text");
String intentData2=getIntent().getExtras().getString("button2text");
button1.setText(intentData2);
button2.setText(intentData1);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button1"
android:layout_alignParentEnd="true"
android:layout_marginEnd="59dp"
android:id="#+id/button2"
/>
<Button
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:layout_marginTop="46dp"
android:id="#+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myb.assignment">
<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=".SecondActivity"> </activity>
</application>
</manifest>
In your MainActivity, in changeText(),
buttonone=(Button) view;
buttontwo=(Button) view;
These two lines assign the same button view, to both the buttons.
Insted of this, I recommend doing the following in your onCreate() method after setContentView();
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
And remove those two lines in changeText().

Android eclipse. Error when running my application

I cant get my application to work, but theres no error message in the development.
Im trying to learn how to link my page to another page.
TMactivity page 1.
public class TmActivity extends Activity {
private ImageButton NewPage;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
this.NewPage = (ImageButton)this.findViewById(R.id.widget38);
this.NewPage.setOnClickListener(new OnClickListener() {
public void onClick(View WebView) {
Intent i = new Intent(TmActivity.this, New.class);
startActivity(i);
}
});
}
}
// Page 2:
public class WebView extends Activity {
public class New extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
}
}
}
//first xml:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="#+id/widget37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_x="4dp"
android:layout_y="387dp" />
<ImageButton
android:id="#+id/widget38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="69dp"
android:layout_y="386dp" />
<ImageButton
android:id="#+id/widget39"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="151dp"
android:layout_y="386dp" />
<ImageButton
android:id="#+id/widget40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="242dp"
android:layout_y="383dp" />
<TextView
android:id="#+id/widget43"
android:layout_width="wrap_content"
android:layout_height="47px"
android:background="#FF0000"
android:text="Teknikmagasinet"
android:textSize="20sp"
android:typeface="sans"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_x="74dp"
android:layout_y="11dp" />
<TextView
android:id="#+id/widget44"
android:layout_width="203px"
android:layout_height="30px"
android:text="nyheter"
android:textColor="#FFFF00"
android:layout_x="34dp"
android:layout_y="77dp" />
<ImageView
android:id="#+id/widget45"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff33cc00"
android:layout_x="44dp"
android:layout_y="143dp" />
<TextView
android:id="#+id/widget46"
android:layout_width="wrap_content"
android:layout_height="47px"
android:background="#ffcc6600"
android:text=" emil bergstrlm han är kung "
android:hint="phuong"
android:layout_x="13dp"
android:layout_y="255dp" />
</AbsoluteLayout>
// second xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
package="tm.com"
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/hello"
/>
<ImageButton
android:id="#+id/widget38"
android:layout_width="150dp"
android:layout_height="wrap_content" />
<Button
android:text="Second Page"
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
sting xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TmActivity!</string>
<string name="app_name">Tm.com</string>
<string name="main_title">My Main Title</string>
</resources>
Thank you for any help!
//remove this life from your second activity
public class WebView extends Activity {
and register New in your manifest.xml
If I understand correctly, you're trying to open a new activity from an already running activity (Switching your page)? Try this.
Activity Main.java :
package tm.com
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class TmActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button butn = (Button) findViewById(R.id.button1);
butn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TmActivity.this, New.class);
startActivityForResult(intent, 0);
}
});
}
}
Layout Main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="#+id/button1" android:text="name" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</RelativeLayout>
In Application Manifest : *Modify the manifest in the application bracket to look like this.
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".TmActivity"
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=".New"></activity>
</application>

Difficulties with android on click listener

firstly I have to write that I have just started my adventure with android, I'm building simple menu and as far as I have about and exit button working :), I've tried with start button but it doesn't listen at all, If You have any suggestions what to do with it I would more than happy :)
main activity:
public class MainActivity extends Activity implements OnClickListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View startButton = findViewById(R.id.start_button);
startButton.setOnClickListener(this);
View whereButton = findViewById(R.id.where_button);
whereButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit_button:
finish();
break;
case R.id.start_button:
i = new Intent(this, Start.class);
startActivity(i);
break;
}
}
Strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">City Inspector</string>
<string name="main_title">Ldz Inspector</string>
<string name="start_title">Profile</string>
<string name="start_label">START</string>
<string name="where_label">WHERE AM I?</string>
<string name="option_label">OPTIONS</string>
<string name="about_label">ABOUT</string>
<string name="create_label">CREATE PROFILE</string>
<string name="browse_label">BROWSE MAP</string>
<string name="exit_label">EXIT</string>
<string name="about_text">\
about program things
</string>
</resources>
start.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
}
}
start.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="20dip"
android:orientation="horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center" >
<TextView
android:text="#string/start_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="#+id/Create_Profile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/create_label" />
<Button
android:id="#+id/Browse_Map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/browse_label" />
</LinearLayout>
</LinearLayout>
!!EDIT:
Sorry I knew I forgot to post something :), here is the AndroidManifest :)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="menu.dot"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/icon">
<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=".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=".Start">
andorid:label="#string/start_title">
</activity>
/application>
</manifest>
Please check the start.xml edition
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="20dip"
android:orientation="horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center" >
<TextView
android:text="#string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="#+id/start_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/start_label" />
<Button
android:id="#+id/where_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/where_label" />
<Button
android:id="#+id/option_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/option_label" />
<Button
android:id="#+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/about_label" />
<Button
android:id="#+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit_label" />
</LinearLayout>
</LinearLayout>
In Android, you don't need to implement an onClickListener and check which button was clicked. Android offers you the neat way to specify the method used to handle the onClick-event by simply using the onClick-attribute for the button in your XML-Layout file.
Consider the following layout:
<Button android:id="#+id/some_button"
android:onClick="someButtonsAction"
android:layout_width="fill_parent"
android:layout_height="match_content"
/>
The Activity presenting this button will now need to have a method like this:
public void someButtonsAction(View v){
// Do your onClick stuff here
}
The supplied View v-argument is the clicked view itself. The whole thing is also illustrated in the docs on the top if the page.
This gives you the opportunity to make your code more readable and you don't need to get all the buttons from the layout, add them an onClickListener and decide which button was clicked (in the listener).
Do you register 'Start' activity in AndroidManifest.xml?
If you did not register activity in AndroidManifest.xml, you are not able to run the acticity.
Could you post your main.xml? I tried out your code making assumptions about what your main.xml looks like and start activity worked just fine (i.e., start layout displayed).
mainActivity.java
public class MainActivity extends Activity implements OnClickListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View startButton = findViewById(R.id.start_button);
startButton.setOnClickListener(this);
View whereButton = findViewById(R.id.where_button);
whereButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit_button:
finish();
break;
}
}
public void StartCl(View v){
Intent idd = new Intent(this,Start.class);
startActivity(idd);
}
}
Main.xml
<Button
android:id="#+id/start_button"
android:onClick="StartCl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/start_label" />

Categories

Resources