Android button does not print out a string message - java

I have tried to search in android developers but I had no luck.
My onClick method does not print any message. I want to change my text from ... to ...; however nothing happens when I click it.
JAVA
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the Cookie button is clicked.
*/
public void eatCookie(View view) {
display("I'm so full");
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(String status) {
TextView statusTextView = (TextView) findViewById(
R.id.status_text_view);
statusTextView.setText("" + status);
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B388FF"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/android_cookie_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="#drawable/before_cookie" />
<TextView
android:id="#+id/status_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="I'm so hungry"
android:textColor="#android:color/white"
android:textSize="34sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="EAT COOKIE"
android:onClick="eatCookie"/>
</LinearLayout>

Your code is working correct in my device
Try to Clean your project and re run the code again.
Build -> Clean
and then run the code

Related

App Keeps Stopping when pressing a button

app keeps crashing when ever the button is pressed, tried on emulator as well as on a phone. I'm using Android Studio 3.1.2. How can i fix it, i've tried most of other solutions with same problems over here but didnt work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButton(View v) {
display(1);
}
public void display(int n) {
TextView qt = findViewById(R.id.quantity;
qt.setText(n);
}
}
here's its xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<TextView
android:id="#+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Quantity"
android:textAllCaps="true"
android:textSize="32sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="123dp"
android:id="#+id/quantity"
android:text="0"
android:textAllCaps="true"
android:textSize="32sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ORDER"
android:textSize="32sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="215dp"
android:onClick="onButton"/>
</RelativeLayout>
do the below:
qt.setText(String.valueOf(n));
// or use qt.setText("" + n);
instead of
qt.setText(n);
It is because
setText(#StringRes int resid)
takes the parameter as "int resId" which it expects to be the id of the String value from the Strings.xml file.
But here you want to display the "int" value in the textview, so you need to use the overloaded method:
setText(CharSequence text)
method instead, and convert your int value to the String using
String.valueOf(n) //or by ("" + n)
Change this:
public void display(int n) {
TextView qt = findViewById(R.id.quantity;
qt.setText(n);
}
To this:
public void display(int n) {
TextView qt = findViewById(R.id.quantity); //add closing bracket
qt.setText("" + n); //makes 'n' a string
}

Custom media player in android

I am trying to make a custom media player where when ever the music start a loader will start and when the music is paused or ends the the loader goes invisible .This is what i am trying to get when the button is clicked ..How to set the desired output.I am using the following gradel
compile 'com.leo.simplearcloader:simplearcloader:1.0.+'
compile 'de.hdodenhof:circleimageview:2.0.0'
my xml code
<?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:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context="com.union.playerfront1.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/load1">
<com.leo.simplearcloader.SimpleArcLoader
android:visibility="visible"
android:id="#+id/loader"
android:layout_centerInParent="true"
android:layout_width="200dp"
android:layout_height="200dp"
custom:arc_style="simple_arc"
custom:arc_speed="medium"
custom:arc_margin="3dp"
>
</com.leo.simplearcloader.SimpleArcLoader>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_marginBottom="44dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
my java code
public class MainActivity extends AppCompatActivity {
boolean isPressed = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mediaPlayer =MediaPlayer.create(this,R.raw.song);
final RelativeLayout real=(RelativeLayout)findViewById(R.id.load1);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isPressed){
button.setBackgroundResource(R.drawable.profile);
real.setVisibility(View.VISIBLE);
mediaPlayer.start();
}
}
});
}
}
How I can change the visibility of the loader on the base that a song is playing or it is stopped I am able to set the button visibility properly.How i can set the visibility on the basis of song pause,song start,song stop?

Android Studio not showing Application Name

Let me know if you can see my link without logging in below. The problem I am having is the same as the person described. The blue action bar is blocking the name of my app in every project I create. I haven't seen anyone with this problem other than the person who posted the discussion. Please help me out if you can, I appreciate it.
-Timmy
<?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"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:paddingBottom="16dp"
android:text="Quantity"
android:textAllCaps="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:onClick="minusIncrement"
android:text="-"/>
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="2"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:onClick="plusIncrement"
android:text="+"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="Price"
android:textAllCaps="true"/>
<TextView
android:id="#+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="$10"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:text="Order"/>
</LinearLayout>
package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toolbar;
import java.text.NumberFormat;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 2;
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(message);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
String priceMessage = "Total: $" + (quantity * 5);
priceMessage = priceMessage + "\nThank you!";
displayMessage(priceMessage);
}
/**
* This method is called when the plus button is clicked.
*/
public void plusIncrement(View view) {
quantity = quantity + 1;
display(quantity);
}
/**
* This method is called when the minus button is clicked.
*/
public void minusIncrement(View view) {
quantity = quantity - 1;
display(quantity);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
/**
* This method displays the given price on the screen.
*/
private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
}
https://discussions.udacity.com/t/title-on-the-app-is-not-showing/37056
When working with the toolbar you have to do things a little different. You can do this:
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My custom toolbar!");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/holo_red_light"
android:minHeight="100dp">
</android.support.v7.widget.Toolbar>
</RelativeLayout>
So the big issue is that the ToolBar is never added to the current content view and hence will never be visible until you actually add the ToolBar to the current content view.
The answer taken from here:
Title, etc. not showing in Android Toolbar

Android Sliding Drawer Open On Create

I want to have a slider that is open when the app starts. It will be open with buttons and such and when the user closes it, there will be more buttons to access. Is this possible with a sliding drawer? What would I add to the onCreate() method?
Thanks
XML Layout - In a basic LinearLayout:
<SlidingDrawer
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:id="#id/handle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/handleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tray_expand" />
<Button
android:id="#+id/handleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:text="Up me" />
</LinearLayout>
<LinearLayout
android:id="#+id/content"
android:paddingTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#013E53"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_commentDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textSize="20dp" />
</LinearLayout>
</SlidingDrawer>
And your Activity will looks like this:
public class Home extends Activity implements OnDrawerScrollListener
{
private ImageView handleImage;
private Button handleButton;
private SlidingDrawer slide;
private TextView tv_commentDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
tv_commentDisplay = (TextView)this.findViewById(R.id.tv_commentDisplay);
handleImage = (ImageView)this.findViewById(R.id.handleImage);
handleButton = (Button)this.findViewById(R.id.handleButton);
slide = (SlidingDrawer)this.findViewById(R.id.slide);
slide.open(); // not sure
slide.setOnDrawerScrollListener(this);
handleButton = ((Button)this.findViewById(R.id.handleButton));
tv_commentDisplay.setText("Hello World");
}
#Override
public void onScrollEnded() {
}
#Override
public void onScrollStarted() {
if (slide.isOpened())
handleImage.setImageResource(R.drawable.ic_tray_collapse);
else {
handleImage.setImageResource(R.drawable.ic_tray_expand);
}
}
Use open() in your onCreate(), it will open the drawer immediately.
You can take a look at the full API here

Get values from Dynamic list

Good day everyone,
I'm learning Android development and I try to build some dynamic lists for my application. And I'm stuck... like for 4 hours already.
I have two layouts:
1. main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/action_buttons"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
>
<Button
android:id="#+id/button_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_calculate" />
<Button
android:id="#+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/button_count"
android:text="#string/button_add" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/action_buttons"
>
<LinearLayout
android:id="#+id/action_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
action_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_list_item_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/action_list_item_edittext_drinkName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/action_list_item_title"
android:padding="10dp" />
<EditText
android:id="#+id/action_list_item_edittext_drinkPercent"
android:text="40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_edittext_drinkAmount"
android:inputType="number"
android:padding="10dp"/>
<EditText
android:id="#+id/action_list_item_edittext_drinkAmount"
android:text="0.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_button_remove"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:inputType="numberDecimal"
android:width="50dp"
android:padding="10dp" />
<Button
android:id="#+id/action_list_item_button_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/action_list_item_button_remove" />
And code that creates dynamic list:
public class MainActivity extends Activity {
LinearLayout actionList = null;
private Button btnAdd;
private Button btnCalculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initActivityElements();
}
private void initActivityElements() {
initAddButton();
initCalculateButton();
}
private void initCalculateButton() {
btnCalculate = (Button) findViewById(R.id.button_count);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView amount = (TextView) findViewById(R.id.action_list_item_edittext_drinkAmount);
//this retrives TextView value from first list
Toast.makeText(getApplicationContext(), amount.getText().toString(),
Toast.LENGTH_SHORT)
.show();
}
});
}
private void initAddButton() {
actionList = (LinearLayout) findViewById(R.id.action_list);
btnAdd = (Button) findViewById(R.id.button_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout listItem = (RelativeLayout) View.inflate(
MainActivity.this, R.layout.action_list_item, null);
TextView name = (TextView) listItem
.findViewById(R.id.action_list_item_edittext_drinkName);
name.setText("Here is the Title " + actionList.getChildCount());
listItem.findViewById(R.id.action_list_item_button_remove)
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
actionList.removeView((View) v.getParent());
}
});
actionList.addView(listItem);
}
});
}
My problem is that I need to get all data from TextView boxes (Amount, Percent), but I can retrive that data only from first item of a list (look at onClickListener for btnCalculate) and can't figure out how to do it, but tried to add 'unique ids' for view (but that brakes layout, badly), tried setting Tags but again with no luck.
Maybe anyone can give a tip? I bet there is some easy way to do it, but I'm failing to find it and google is no help here.
Thanks
I'm not really sure what exactly you are trying to do but it sounds like you are trying to take in information and populate it into a view. I would recommend having a static text box that takes in your information and builds that into a listview. A good example of how to do that is located here http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html.

Categories

Resources