other way to make start up menu instead using button - android

i am thinking of doing a start up menu before going into the real page like the picture below.
i think i can do it with button menu. so, the pikpok, all-star, etc i made it using button widget. Is there any other way to make this?
anyway, when user click on all-star button, it appear to the next xml file. but then the activity seems not working out.
this is my code. this code shows the start up activity.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
setContentView(R.layout.about);
}
});
button = (Button) findViewById(R.id.signin);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
setContentView(R.layout.report);
}
});
}
}
report.xml and about.xml will have their own activity named ReportActivity and AboutActivity but it does not work.
is there a better way to the start up menu instead of using button. also it would be nice if the menu could have animation to it.

For rather complex "Buttons" you can define your own View by using layouts/images/other view
check
http://developer.android.com/guide/topics/ui/custom-components.html
for more information.
To call another activity, you have to define an activity class for that activity first, which calls the layout etc. On how to start another activity read
http://developer.android.com/guide/components/activities.html#StartingAnActivity

Related

How to make a button open a new layout xml

I'm trying to make a button for my app, which will bring the screen to another page. However, I'm not successful in doing so.
I've tried many things, without a reliefing answer.
My project doesn't accept "Intent" in my program.
My button I need to open a new layout is called "OptionButton"
Here's what I've got:
in MainActivity.java
In the beginning I got this
public class MainActivity extends Activity {
private Button startButton;
private Button pauseButton;
private Button resetButton;
public Button OptionButton;
/** further I got this**/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
/** (I'm just mentioning this because I use savedInstanceState here too)**/
/**MainActivity.java And my code for my button is this **/
OptionButton = (Button) findViewById(R.id.Button1);
OptionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
public void myClick(View v) {
Intent intent = new Intent(this, Background2.class);
startActivity(intent);// for calling the activity
}
});
}
}
}
I added this in AndroidManifest:
<activity android:name=".Background2"></activity>
and this in the 2nd class (java file in src map)
(package & imports, then this:
public class Background2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
_
I got 2 classes in src map:
-Background2.java
-MainActivity.java
Also 2 layout xml's:
activity_main.xml
activity main2.xml
In Activity_main, I got this for the button:
<Button
android:id="#+id/Button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/OptionButtonLabel"/>
Still it's not working. What am I missing?
Ty all so much!
I've tried changing this:
button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this);
But it didn't work.
Inside of the listener you are calling this (which refers to the listener itself), while what you want to refer to is the MainActivity.
Simply change to
Intent intent = new Intent(MainActivity.this, Background2.class);
You Main Activity has to implement OnClickListner
public class MainActivity extends Activity implements OnClickListener{
From Eclipse IDE press Ctrl+Shift+O it will automatically implement and import necessary functions
Thank you for the answers! :)
I've tried installing it on my real device.
Now, it opens, but it's the 2nd xml that opens, and when I click the button, it re-opens the same xml.
:-/
I don't get any error messages though when I changed your suggested solutions (from both of you)

Android app error “Unfortunately App has Stopped”

Hello i have a problem with running the application, when ill run it, its opening but when i will press the button saying me that the application is stopped and cant run: “Unfortunately, App has Stopped”. Here is my simple code and i hope i will get the answer fast...
Thanks allot :)
package com.stefan.stefan1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView1.setText("Hello Im Stefan !");
}
});
}
#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;
}
}
Hello again i have new problem i have added another label(textView2) and im trying to add again this line textView2 = (TextView) findViewById(R.id.textView2);
but giving me error for adding something in class "R.java"
please help .. :/
You forgot to initialize textView1.
So when you're doing :
textView1.setText("Hello Im Stefan !");
it throws a NullPointerException and stop your app.
If you defined your textview in the layout, add after setContentView
textView1 = (TextView) findViewById(R.id.idOfYourTextView);
P.S : You should always provide the stacktrace(logcat) when you have such an error.

android public textbox

I have come across another prickly little problem.
I am writing an app with tabs, but I have a textbox (EditText) at the top of the screen that I want to be able to receive text data from any of the tabs. As each of the tabs has it's own activity and layout, this is proving difficult to achieve.
I want to be able to use:
editText1.setText("Hello World");//sample text
from any Tab/Activity.
Does anyone know how to make a textbox from one layout public and able to recieve text?
I am using TabActivity, yes I know it's deprecated but as this is my first app with tabs, it's easier to learn than Fragments. I will try them next time, unless they are the answer to my problem, in which case I have a lot of re-coding to do!!
ok, new part.
package com.epsilonitsystems.epecsandroid;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.view.Menu;
import android.speech.tts.TextToSpeech;
public class MainActivity extends TabActivity {
public EditText editText1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
TabHost tabHost = getTabHost();
// Tab for Core
TabSpec corespec = tabHost.newTabSpec("Core");
corespec.setIndicator("", getResources().getDrawable(R.drawable.ic_i));
Intent coreIntent = new Intent(this, CoreActivity.class);
corespec.setContent(coreIntent);
// Tab for Drink
TabSpec drinkspec = tabHost.newTabSpec("Drink");
drinkspec.setIndicator("", getResources().getDrawable(R.drawable.ic_drink));
Intent drinkIntent = new Intent(this, DrinkActivity.class);
drinkspec.setContent(drinkIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(corespec); // Adding Core tab
tabHost.addTab(drinkspec); // Adding Drink tab
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
That's the Main Activity, I'll just show the Core Activity as they will all be the same when I get it working.
package com.epsilonitsystems.epecsandroid;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;
public class CoreActivity extends Activity {
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;
EditText editText1;
String Spch,Str;
ImageButton imageButton1,imageButton2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.core_layout);
//button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
mTts = new TextToSpeech(this, null);
}
public void onimageButton1Click(View view) {
//mTts.speak("I",TextToSpeech.QUEUE_FLUSH,null);
//Spch=editText1.toString();
//Spch=Spch+"I ";
editText1.setText("Hello World");
}
}//Activity End
I can't post a screenshot as I'm still a new user, sorry.
Any ideas please?
Gary,
What you are looking for probably is how to share data between different Activities.
You can do this in a couple of ways, but the cleanest is using the Intent you start the new Activity with.
With this you can set extra data via the Intent.putXXXXX methods.
You could use something as Intent.putExtra("my_private_key", mTextVar); and fetch that out in your other Activity with this.getIntent().getStringExtra("my_private_key");
ps. As tempting as it might seem to start with the TabActivity, you're actually making it a lot more difficult for yourself by making a bad start and learning classes which you should not use anymore. I'd advise you to pick up some good fragments tutorials which will be just as easy once correctly explained. You should take a look at per example http://www.vogella.com/android.html

Android app force closes soon as i open it on emulator

I went through similar kind of questions on older posts.. rectified them. Still getting force close problem. Please help.
Which of the following is correct?
Button continueButton = (Button) findViewById(R.id.continue_button);
or
View continueButton = findViewById(R.id.continue_button);
And what's the difference between them ?
package org.example.sudoku;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
public class Sudoku extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button continueButton = (Button) findViewById(R.id.continue_button);
continueButton.setOnClickListener((OnClickListener) this);
Button aboutButton = (Button)findViewById(R.id.about_button);
aboutButton.setOnClickListener((OnClickListener) this);
Button newButton = (Button)findViewById(R.id.new_game_button);
newButton.setOnClickListener((OnClickListener) this);
Button exitButton = (Button)findViewById(R.id.exit_button);
exitButton.setOnClickListener((OnClickListener) this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Both is correct, because Button is a subclass of View. But you will typically have to use Button so you can add listeners and other stuff to it.
You should really have a look at the logcat output of you application. There you will find a stacktrace that will help you identifying the problem.
I think the problem comes from the way you set up your click listeners on your buttons. You use this but your activity class does not implement the interface. I advice you to do that instead:
View.OnClickListener clickHandler = new View.OnClickListener() {
public void onClick(View v) {
}
}
Button continueButton = (Button) findViewById(R.id.continue_button);
continueButton.setOnClickListener(clickHandler);+-

Webview buttons stop working after website loads

I have created a simple webview with two buttons below it. If I press one of the button the view seems to create a new view called "web" then the page loads and I still have my buttons below but they don't function. If I use the back button on the phone it takes me to the opening view blank page and the buttons work again? Sorry I am new..
I just want it to load in the original view and have the buttons continue to function.
Do I have to suppress the creation of a new view somehow?
Kind Regards,
-Mike
** and I am not sure why my code always has extra crap when I post it because it does't when I copy it to the clipboard. **
Webscreen Class
package com.example.cam;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Webscreen <URL> extends Activity {
WebView webview1;
public static final String URL = "";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String turl = getIntent().getStringExtra(URL);
webview1 = (WebView)findViewById(R.id.webview01);
webview1.clearCache(true);
webview1.loadUrl(turl);
}
}
cam Class
package com.example.cam;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class cam extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
View firstButton = findViewById(R.id.button1);
firstButton.setOnClickListener(this);
View secondButton = findViewById(R.id.button2);
secondButton.setOnClickListener(this);
}
// Process the button click events
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.example.cam.Webscreen.URL,
"http://m.yahoo.com");
startActivity(j);
break;
case R.id.button2:
Intent k = new Intent(this, Webscreen.class);
k.putExtra(com.example.cam.Webscreen.URL,
"http://m.google.com");
startActivity(k);
break;
}
}
}
I don't know why are you not using Button class and using View Class.
Use
Button b1=(Button)findViewById(R.id.button1);
instead of
View b1=findViewById(R.id.button1);
Use relative layout for placement of buttons
On button click you are recreating your activity with the intent of displaying the web page/s.
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.example.cam.Webscreen.URL,
"http://m.yahoo.com");
startActivity(j);
it is only creating new activity on top of the new activity
you need to load the web page on button click instead.
//on button1 click
mWebView.loadUrl("http://m.yahoo.com");

Categories

Resources