Application name bar disappears after switching to new activity - java

Basically, the problem is pretty straight-forward if you read the title.
I started my test application for testing purposes and when I'm at the main menu, there is that bar with the application name above and when I click on any button that triggers a new activity, the bar disappears.
http://i.stack.imgur.com/uI70f.png
After clicking, in this example, "What's the meaning of life?" button, the result looks like this
http://i.stack.imgur.com/7mvmY.png
The codes that are used in order to make the button are as follows:
options_menu.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is the meaning of life?"
android:onClick="ShowLifeMeaning"
android:textAllCaps="false"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true" />
Life.java
public class Life extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meaning_of_life);
}
#Override
public void onBackPressed() {
finish();
}
MainActivity.java
public void ShowLifeMeaning(View view) {
startActivity(new Intent(this, Life.class));
}

You can try using
public class Life extends ActionBarActivity

Hi I think it 's because of theme, in your manifest file maybe you right this code for theme :
theme."your theme".NoActionbar or something like this.
if you remove it , it would be correct

You need to use ActionBarActivity.
But this class being deprecated , you can use AppCompatActivity with which you can support actionBar starting from API 7 and it supports fragments as well.

Related

How can I swap images on button click?

Essentially All the ImageButton has to do (InactiveButton) is switch to an image when pressed. I have no clue where to start with this because I am new to Android Studio.
I have tried using the selector class but I don't know what it's supposed to accomplish. When I use other's code such as public void buttonOnClick(View v), android studio says it's deprecated?
XML layout:
<ImageButton
android:id="#+id/InactiveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/button_default" />
EDIT 1: I have added the following code and still got an "annotations are not allowed here" #override on line 52, and cannot resolve symbol v on line 53.
Picture of error
EDIT 2: Image of final error on line 50
Location of error
Actual Compiler Message
What Ferran has said is absolutely right.
You need to implement it in correct way.
Firstly define you image view before onCreate method so that it will be treated as global variable and advantage of this is you can use it anywhere in same activity.
Like
ImageButton inactiveButton;
Secondly, find its view by using FindViewById in onCreate method.
Like
inactiveButton = convertView.findViewById(R.id.InactiveButton);
Next, you can implement onClickListener on inactiveButton by using two ways:
1. You can implement "implements View.OnClickListener" to your activity by using inactiveButton.setOnClickListener(this); This will prompt you to implement "android.view.View.OnClickListener" to your activity or fragment. Implement this method this will add onClick method and here you can write you code to change image in the image button
#Override
public void onClick(View v) {
// add this, this will help you to implement multiple clicklisteners and you can add different methods in each click listener
switch (v.getId())
{
case R.id.InactiveButton:
inactiveButton.setImageResource(R.drawable.otherimage);
break;
default:break;
}
}
You can directly write OnClickListener, you need to keep this in main method
inactiveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inactiveButton.setImageResource(R.drawable.otherimage);
}
});
Hope this will help you. I know I have added a lot of information but I think it will help you in long run. You can check images for more clarity then you can easily understad you flaw.
Regards
Amanpreet
You can change the image like this:
ImageView someImageView= (ImageView) findViewById(R.id.some_image_view);
imageView.setImageResource(R.drawable.some_image);
In your onCreate method from your Activity
ImageButton inactiveButton = convertView.findViewById(R.id.InactiveButton);
Now, you have a reference to your ImageView.
Then, you need to define what to do when you click on it.
inactiveButton .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// put here the code when ImageView is clicked
// to change the ImageView image
inactiveButton.setImageResource(R.drawable.otherimage);
}
});
That's all.

Why do I get an error statement when using an if comparison of ids

I make a Button and I want to make onClick method in XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/button2"
android:onClick="maysara"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button"
android:layout_marginTop="63dp" />
Then I go to Java code to make the method "maysara"
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void maysara(View v){
**if(v.getId()==findViewById(R.id.button2))**
Toast.makeText(this,"button2",Toast.LENGTH_SHORT).show();}}
But I got an error in if statement >>> I really dont know why #?!
Check the return type of findViewById() which is a view and you are comparing with v.getId() which is id. You should not compare this.
public View findViewById (int id)
Just use like this
public void maysara(View v){
Toast.makeText(this,"button2",Toast.LENGTH_SHORT).show();
}
It help you.
I know this has an answer, but I thought I'd add one.
You are declaring
android:onClick="maysara"
as the onClick method in your xml for this button.
There is no need to do a check on which button is clicking, as you have explicitly defined this in your xml.
So within your mayasara method, you only need to show what you want to do, not a check on the button clicking.
public void maysara(View v){
<Button
android:id="#+id/button1"
android:onClick="maysara"
<Button
android:id="#+id/button2"
android:onClick="maysara"
A switch statement is a much better option, as you do not need to check for the value of R.id.button2 as you have in your if statement. Checking those values for this type of function is a clumsy way of programming.
public void maysara(View v){
switch(v.getId()) {
case R.id.button1:
// to do
break;
case R.id.button2:
// to do
break;

Android EditTextPreference disable Dialog

I want to disable(not show) the Dialog, when i click the EditTextPreference in a Preference Activity.
When i click the EditTextPreference, a pop up dialog show. Now i want to disable this. I try set an onclicklistener, but the dialog show same.
public class Settings extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
EditTextPreference userName=(EditTextPreference) findPreference("prefUsername");
userName.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
}); ...
I try add this to the EditTextPreference: android:clickable="false", but nothing.
Disable it.
Use
android:enabled="false"
in XML. Or to disable in runtime,
userName.setEnabled(false);
Actually, if you do not want a dialog, you probably should use simple Preference instead of EditTextPreference.
try to replace
EditTextPreference
with
Preference
so your code will be like this
<Preference
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Version "
android:key="edit_text_preference_1"
android:summary="1.0"
android:enabled="true" />
hope this help you

How to program efficiently? When pressed button create corresponding activity

I'm new to programming so I have a really easy question. Since I do not really know the terms I couldn't find any topic on my problem, so excuse me if this question has been asked before.
My problem is as follows: I'm running an app, created with Eclipse, on an Android machine. On the first screen I have just a list of buttons:
layout_main:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/alpha"
android:onClick="alpha" />
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/beta"
android:onClick="beta" />
If I press a button then the corresponding activity will start. I programmed the main activity as follow in order to do that:
main activity:
public void alpha(View view) {
Intent intent = new Intent(this, AlphaActivity.class);
startActivity(intent); }
public void beta(View view) {
Intent intent = new Intent(this, BetaActivity.class);
startActivity(intent); }
Since I have many buttons, I will have as many times the operation public void as seen above. Isn't there any way to program it more efficient? For example: Start new activity, if alpha was selected then start activity alpha, if beta was selected start activity beta, else do nothing.
You could apply the same listener to each button, not really sure if it's more efficient or even better style, but this will probably work. Something like:
public void buttonListen(View view)
{
Class clas;
int id = view.getId();
switch(id)
{
case R.id.alpha:
clas = AlphaActivity.class;
break;
case R.id.alpha:
clas = BetaActivity.class;
break;
...
case R.id.zeta:
clas = ZetaActivity.class;
break;
}
startActivity(new Intent(this, clas));
}
and assign the corresponding id in the XML as the next answer states.
you can give every button an id like this :
<Button
android:id="#+id/alpha"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/alpha"
/>
<Button
android:id="#+id/beta"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/beta"
/>
then in the main activity you can findviewbyid() to find the two buttons and give buttons onclicklistener,then you implements the onclicklistener like this :
public void onClick(View v) {
Intent i = new Intent();
switch(v.getid()){
case: R.id.alpha:
i.setClass(context,AlphaActivity.class);
break;
case: R.id.beta:
i.setClass(context,BetaActivity.class);
break;
}
startActivity(i);
}
hope that helps.
There are many different ways to achieve this and make it simpler. Not a single method will be "the best". What you are doing is probably down the list of "what I would not do".
Don't assign things in XML, it's harder to work with, cannot be really changed at running time and make it less portable when you have to maintain a lot of different layouts. Instead…
This is just ONE way to do it:
Instead of defining the onClick method in the XML, do it programatically.
Add an android id to your Views:
<Button
android:id="#+id/alpha_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/alpha" />
(the same for all the other buttons)
Note: don't use fill_parent, use match_parent (the former is deprecated and both do the same).
The in your Activity onCreate() method, right before you setContentView(R.layout.your_above_layout_with_the_buttons);
You can obtain a reference to each button:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_above_layout_with_the_buttons);
final TextView alphaButton = (TextView) findViewById(R.id.alpha_button);
//etc for the rest
// now add click listeners:
alphaButton.setOnClickListener(this);// more on this later
betaButton.setOnClickListener(this);
// etc.
}
Now your activity should be:
public class YourActivity extends Activity implements View.OnClickListener {
and somewhere in your activity you have to implement:
#Override
public void onClick(final View view) {
// which button was clicked? Different ways to tell… a simple one:
Intent intent;
switch (v.getId()) {
case R.id.alpha_button:
intent = new Intent(this, AlphaActivity.class);
break;
case R.id.beta_button:
intent = new Intent(this, BetaActivity.class);
break;
//etc.
}
if (intent != null) {
startActivity(intent);
}
}
There are many ways you could accomplish your goal.
Note: the best solution would be to add android:id to your layout file, but if for some reason you can't do that, you could use the button text.
A very simple (although not great) way to do it without changing your layout file much would be to look at the text on the button, if all buttons have unique text, and perform the action based on that.
public void onButtonClick(View view)
{
//Cast the click View into a Button
Button selectedButton = (Button) view;
Intent intent = new Intent();
String buttonText = selectedButton.getText().toString();
if(buttonText.equals("beta"))
{
intent = new Intent(this, BetaActivity.class);
}
else if(buttonText.equals("alpha"))
{
intent = new Intent(this AlphaActivity.class);
}
startActivity(intent);
}
Make sure to set the onClick for each button in the layout file to call this method
android:onClick="onButtonClick"
For your specific situation, you could could change the way you handle the calls from your buttons, for example, you could give the buttons IDs and check the IDs of the buttons in a single method.
<Button
android:id="#+id/alpha_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/alpha"
android:onClick="onButtonClick" />
<Button
android:id="#+id/beta_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/beta"
android:onClick="onButtonClick" />
public void onButtonClick(View view) {
Intent intent;
switch (view.getId()) {
case R.id.alpha_button:
intent = new Intent(this, AlphaActivity.class);
break;
case R.id.beta_button:
intent = new Intent(this, BetaActivity.class);
break;
default:
return;
}
startActivity(intent);
}
However, I would suggest that your current code is clear and concise so just leave it as it is. There are many ways to solve a problem, especially when it comes to designing software.
In general though, programming languages try to balance making code (and more importantly it's function or purpose) easy to read/understand while trying to keep the syntax as concise as possible. In time you will learn about the advantages and disadvantages of each language and it's syntax, some will be better at solving certain types of problem while being too verbose for solving others. For now just learn how Java works and try to understand why they have implemented each keyword and syntax element.

Setting up buttons for Android

Here is my problem. I setup the buttons exactly the way they are setup in the Android documentation, but I am getting a warning, and the button will not do anything.
Here is my Java code:
package com.variDice;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
public class VariDiceActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//die1Clicked();
}
private void die1Clicked() {
ImageButton die1button = (ImageButton)findViewById(R.id.die1button);
die1button.setImageResource(R.drawable.icon);
}
}
...and the 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"
android:weightSum="1" android:layout_gravity="center_horizontal">
<ImageView
android:id="#+id/varidice_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/icon"></ImageView>
<ImageButton
android:id="#+id/die1button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#null"></ImageButton>
</LinearLayout>
...and the warning:
The method die1Clicked from the type VariDiceActivity is never used locally.
I must say that I am completely new to Android development. I made my app for the iPhone, and I am now trying to make a version for the android. The iPhone version was sooo much easier, because of the better interface builder (so I can just make an action and connect it to the button that way), so this is almost impossibly hard for me to understand. In other words, I do not understand how you connect an action to the button. Could somebody please tell me what I am doing wrong?
Try this in your xml:
<ImageButton
android:id="#+id/die1button"
android:onClick="die1Clicked"
...></ImageButton>
And in your code, change the method signature to:
public void die1Clicked(android.view.View v) {
ImageButton die1button = (ImageButton)findViewById(R.id.die1button);
die1button.setImageResource(R.drawable.icon);
}
Here is the Android Button tutorial.
To bind some behavior to an UI button, you need to register a listener that receives notifications of a certain event type. In your case, you register a OnClickListener (for the click event); just like in the following snippet:
// create the implementation of OnClickListener
private OnClickListener mDie1Listener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// get the button from layout
Button button = (Button)findViewById(R.id.die1button);
// register the onClick listener with the implementation above
button.setOnClickListener(mDie1Listener);
...
}
You need to add a click listener to your button. Put this in your onCreate():
ImageButton die1button = (ImageButton)findViewById(R.id.die1button);
die1button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// What to do when the button is clicked
});
Most answers on SO tend to use 'setOnClickListener' instead of using xml properties.
I personally prefer using xml for making items clickable in android.
The mistake you have made is setting your function as private. The function which gets called after clicking the item should be public.
There are 3 things you should keep in mind:
Define the 2 properties in xml.
android:clickable="true"
android:onClick="functionName"
Define that function in the Activity file. Make sure to keep the function public.
public void functionName(View v) {
// TODO Auto-generated method stub
}
Make sure to pass 'View v' as an argument for that function.

Categories