Basic android app showing blank white screen? - android

I'm new to android studio. Right now I'm making a basic app that recursively counts down from 4 to 0 when you click a button on the screen. When I look at my activity_main.xml in Design view, it shows what I want, but when I run my app on my Virtual Device, it just shows a white screen with nothing on it. How can I fix this? I'll paste my code here, almost all of my code is in the activity_main.xml and MainActivity.java
Here is 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"
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=".MainActivity" >``
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/button1Contents"
android:onClick="sendMessage"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:text="#string/textView1Contents" />
</RelativeLayout>
and here is my MainActivity.java
public class MainActivity extends Activity {
private static final String TAG = "OneButtonRecursion";
public void sendMessage(View view) {
Button clickedButton = (Button) view;
if (clickedButton == findViewById(R.id.button1)) {
TextView cup = (TextView) findViewById(R.id.textView1);
int handFull = Integer.parseInt(cup.getText().toString());
if (handFull > 0) {
cup.setText(handFull + "");
playNextCup(cup, handFull);
}
} else {
}
}
public static void playNextCup(TextView cupIn, int handFullIn) {
handFullIn--;
cupIn.setText(handFullIn + "");
if (handFullIn != 0) {
playNextCup(cupIn, handFullIn);
Log.i(TAG, "In recursion " + handFullIn);
} else {
}
}
}
and here is my strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">One Button Recursion</string>
<string name="action_settings">Settings</string>
<string name="button1Contents">Button1</string>
<string name="textView1Contents">4</string>
</resources>

You just forgot to Override onCreate() method
onCreate() Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI,
Just Override onCreate() inside your activity it will
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Related

Android - Button under Relative layout wont initiate OnClickListener when clicked

Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}

Changong the visibilty/invisibiloty of a TextView Using onClick Method

I am trying to make two textviews appear and disappear on the same click and then on the next click vica versa appear and disappear. I have read some posts on this site, Make Textview Visible by Pressing a Button and Changing the visibility of a textview in a listview, but the solutions in these examples does not work for me. However, I have borrowed some of their ideas.
package com.mycompany.screenchangeapplication;
import android.app.*;
import android.graphics.drawable.ColorDrawable;
import android.os.*;
import android.view.*;
import android.widget.*;
public class ScreenActivity extends Activity {
public RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
container = (RelativeLayout) findViewById(R.id.ScreenActivity);
container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeScreen(v);
}
});
}
public void changeScreen(View v) {
ColorDrawable cd = (ColorDrawable) this.container.getBackground();
TextView ON = (TextView) findViewById(R.id.ON);
TextView OFF = (TextView) findViewById(R.id.OFF);
if (cd != null && cd.getColor() == getResources().getColor(R.color.WHITE)) {
container.setBackgroundColor(getResources().getColor(R.color.BLACK));
OFF.setVisibility(View.VISIBLE);
ON.setVisibility(View.INVISIBLE);
} else {
container.setBackgroundColor(getResources().getColor(R.color.WHITE));
OFF.setVisibility(View.INVISIBLE);
ON.setVisibility(View.VISIBLE);
}
}
}
and
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/ScreenActivity"
android:clickable="true"
tools:context=".ScreenActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="ON"
android:id="#+id/ON"
android:visibility="visible"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="150dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="OFF"
android:id="#+id/OFF"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="150dp"
android:textColor="#ffffff"
android:visibility="invisible" />
</RelativeLayout>
When I put the app in the emulator it crashes, so something is badly wrong.
I am not sure how View v should be passed to onClick and to changeScreen. I would hope that the View passed to onClick would also be passed to changeScreen, but I am unsure how exactly this would work.
In Android Studio, all the text seems to be fine (its not though).
Step1) First you add color.xml file into the values folder.(you can use any name for color)
Step2) Use like this into color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="White">#FFFFFF</color>
<!-- White-->
<color name="Black">#000000 </color>
<!-- BLACK -->
</resources>
Step3) Change a little changeScreen() method:
public void changeScreen(View v) {
ColorDrawable cd = (ColorDrawable) this.container.getBackground();
TextView ON = (TextView) findViewById(R.id.ON);
TextView OFF = (TextView) findViewById(R.id.OFF);
if (cd != null && cd.getColor() == getResources().getColor(R.color.Black)) {
container.setBackgroundColor(getResources().getColor(R.color.White));
OFF.setVisibility(View.INVISIBLE);
ON.setVisibility(View.VISIBLE);
} else {
container.setBackgroundColor(getResources().getColor(R.color.Black));
OFF.setVisibility(View.VISIBLE);
ON.setVisibility(View.INVISIBLE);
}
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
textview.setVisibility(textview.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});

when sending strings via put extra the underline under some words has gone

when I send strings via put extra the underlined words will not be underlined
<string name="s_hello_txt">\n{ <u>hello all</u> }\n</string>
MainActivity Button Code
public void c_hello(View v){
Intent hello= new Intent(MainActivity.this,
MainTextActivity.class);
intent_collection_e3tiraf.putExtra("key",getResources().getString(R.string.s_hello_txt));
startActivity(hello);
finish();
}
MainActivityText onCreate Code
textview = (TextView) findViewById(R.id.id_text_txt);
Intent n = getIntent();
String mrng = n.getStringExtra("key");
textview.setText(mrng);
if I put a text with direct string it will be underlined
For Example if I put in the layout of MainActivityText(activity_maintext.xml)
<TextView
android:id="#+id/id_dailyprayers_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/s_hello_txt"
android:textSize="30sp" />
the textview in MainActivityText Show the text(hello all) underlined
any help!!!!
As long as the string still has the underline html you should be able to utilize the Html.fromHtml method to style the string.
textview.setText(Html.fromHtml(mrng));
Actually, the string getResource().getString(R.string.s_hello_txt) is not be underlined.
The best way to add html source code in strings.xml is to use <![CDATA[html source code]]>. Here is an example:
<string name="s_hello_txt"><![CDATA[<u>Text</u>]]></string>
And then use Html.fromHtml(mrng) to show the underlined string
// Try This One This Will Help For Your Acheivement
**String.xml**
<string name="s_hello_txt"><br/>{ <u>hello all</u> }<br/></string>
**activity_main1.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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/s_hello_txt"/>
<Button
android:id="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GoTo Second Activity"/>
</LinearLayout>
**MainActivity1 Activity**
public class MainActivity1 extends Activity {
private Button btnClick;
private TextView txtValue;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
txtValue = (TextView)findViewById(R.id.txtValue);
btnClick = (Button)findViewById(R.id.btnClick);
txtValue.setText(Html.fromHtml(getString(R.string.s_hello_txt)));
btnClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity1.this, MainActivity2.class);
intent.putExtra("EXTRA",getString(R.string.s_hello_txt));
startActivity(intent);
}
});
}
}
**activity_main2.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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
**MainActivity2 Activity**
public class MainActivity2 extends Activity {
private TextView txtValue;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
txtValue = (TextView)findViewById(R.id.txtValue);
txtValue.setText(Html.fromHtml(getIntent().getStringExtra("EXTRA")));
}
}

Toggle between texts values on Button click

I need to be able to toggle between two text values of "Hello world!" and "Hello!!!". I know this is going to be incredibly simple, but as I'm very new to programming so bear with me. Thanks in advance.
package edu.purdue.cnit355_lab1;
public class MainActivity extends Activity {
private Button HelloButton;
private TextView Message;
private OnClickListener HelloButtonListener = new OnClickListener() {
#Override
public void onClick(View v) {
Message.setText("Hello!!!");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HelloButton = (Button) findViewById(R.id.HelloButton);
Message = (TextView) findViewById(R.id.MessageText);
HelloButton.setOnClickListener(HelloButtonListener);
}
#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;
}
}
My layout.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"
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=".MainActivity" >
<Button
android:id="#+id/HelloButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_button_label"
/>
<TextView
android:id="#+id/MessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
/>
</RelativeLayout>
My strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">Hello Button</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="hello_button_label">Say "Hello!".</string>
#Override
public void onClick(View v) {
if (Message.getText().equals("Hello World")) {
Message.setText("Hello!!!");
} else {
Message.setText("Hello World");
}
}
you should also consider getting the string value using
getResources().getString(R.string.hello_world) instead of using them hard coded.
And try to avoid naming variables with uppercase like Message as they can be mistaken for objects.
Your code looks good. You can add some flag to toogle between your strings. Something like this:
private Strig[] hello = new String[]{"Hello World", "Hello!"};
private int toggleFlag = 0;
In your onClick method:
Message.setTex(hello[toogleFlag]);
toggleFlag ^= 1; // exclusive OR

Why is my R.java file not generating the display id or the button1 id from my XML file?

I have Googled and read several Stack Overflow answers and I am pretty sure the problem is with my layout file here. I also cleaned my project so my R.java file was deleted, but I think after I find the error it will be auto-generated.
Here is my XML code.
<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:orientation="vertical"
android:padding="20sp"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/update"
android:textSize="25sp" />
<EditText
android:id="#+id/guess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textSize="26sp" />
<TextView
android:id="#+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="6sp"
android:paddingTop="6sp"
android:text="#string/default"
android:textSize="18sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="evaluate"
android:text="#string/btn" />
</LinearLayout>
Here are the java lines where there is an error shown, in case there's a problem here:
Button button = (Button) view;
TextView log = (TextView)(findViewById(R.id.display));
EditText field = (EditText)(findViewById(R.id.guess));
Update: Here is my evaluate method from the MainActivity.java file
public void evaluate(View view)
{
Button button = (Button) view;
TextView log = (TextView)(findViewById(R.id.display));
EditText field = (EditText)(findViewById(R.id.guess));
if(button.getText().toString().charAt(0) == 'S')
{
int guess = Integer.parseInt(field.getText().toString());
if(guess == num)
{
log.setText("Congratulations! You guessed it in " + guessCount + "moves");
button.setText("New game");
}
else
{
guessCount++;
if(guess < num)
log.setText("Try again. Too low");
else
log.setText("Try again. Too high");
}
}
else
refresh(button, log);
}
R.java is deleted whenever there exists any compile error in res folder or in AndroidManifest.xml file. Assuming you have no compile errors(except the files that should be referencing fields in R.java), my first call is to look at evaluate method to see if it is in the current invoking Activity as follows:
Java Code:
public class MyActivity extends Activity {
Button button;
TextView log;
EditText field;
...
...
#Override
public void onCreate(Bundle savedInstanceState) {
button = (Button) new Button(this);
log = (TextView) findViewById(R.id.display);
field = (EditText) findViewById(R.id.guess);
...
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
evaluate(v);
}
});
}
...
...
// **evaluate** is visible(public) and
// within the same activity where the **button** is
public void evaluate(View v) {
...
...
}
}
`
doesn't eclipse show you any error in the foler of res?
in the line of: android:onClick="evaluate", I haven't seen such value before, is it correct?

Categories

Resources