Android Studio - EditText text , both hint and normal text , not displaying - java

I know this question has been asked several times, and with different solutions, but I have tried all I could find here, and none worked. As the title suggest, in Android Studio, my EditText widget's field isn't showing any type of text, even what it has by default (like the hint and default text). The text is there if I print it out , but not displaying. It is for a simple login page.
This is the xml file :
<?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">
<TextView
android:text="Name : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:id="#+id/atcoNameTag" />
<TextView
android:text="Password : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/atcoNameTag"
android:layout_marginTop="30dp"
android:id="#+id/passTag"
android:layout_alignStart="#+id/atcoNameTag"/>
<TextView
android:text="Role : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/roleNameTag"
android:layout_below="#+id/passTag"
android:layout_alignStart="#+id/atcoNameTag"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:layout_above="#+id/passTag"
android:layout_centerHorizontal="true"
android:id="#+id/insertName"
android:layout_alignTop="#+id/atcoNameTag"
android:layout_toEndOf="#+id/atcoNameTag"
android:layout_alignStart="#+id/insertPass"
android:hint="username"
android:cursorVisible="true"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_alignBottom="#+id/passTag"
android:layout_toEndOf="#+id/passTag"
android:id="#+id/insertPass"
android:layout_alignTop="#+id/passTag"
android:hint="password"
android:cursorVisible="true"/>
<Spinner
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_alignTop="#+id/roleNameTag"
android:layout_toEndOf="#+id/roleNameTag"
android:layout_alignStart="#+id/insertPass"/>
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/loginButton"
android:onClick="onClick"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />
</RelativeLayout>
And this is the java file :
package mecals.mecalsapp;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
Button nameBtn = (Button) findViewById(R.id.loginButton);
nameBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText nameEntry = (EditText) findViewById(R.id.insertName);
nameEntry.setTextColor(Color.BLACK);
String name = nameEntry.getText().toString();
//TODO username checking, username not showing , need devC Team's work
if (name.length() > 0) {
Toast toastYes = Toast.makeText(getApplicationContext(), "Hi there, " + name + "!", Toast.LENGTH_SHORT);
toastYes.show();
Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
startActivity(intent);
} else {
Toast toastNo = Toast.makeText(getApplicationContext(), "Please insert name !", Toast.LENGTH_SHORT);
toastNo.show();
}
EditText passwordEntry = (EditText) findViewById(R.id.insertPass);
passwordEntry.setTextColor(Color.BLACK);
String pass = passwordEntry.getText().toString();
//TODO password checking , need devC Team's work
}
});
}
}
And these are the solutions I have tried , to no avail :
Difference between content_main.xml and activity_main.xml?
Android edittext typed text not showing
Android EditText typed value not showing
Text not displayed in Android editText when changing android:hint to android:text
EditText in Android doesn't show text when typing while using the on-screen keyboard
Edit Text hints not appearing on later version SDK
edittext not showing the typed text in android
In short, I have
-set the text colour to Black , with nameEntry.setTextColor(Color.BLACK);
-I have added android:windowSoftInputMode="adjustPan" in the manifest
-I have NOT used something like android:gravity , just android:layout
-the cursor is set to visible
-I even checked the height of the widget
-I checked the size of the text with android:ems to be 10
-and the input type is set to text for the first field and textPassword for the second
Help !
PS : before anyone asks , my Android Studio version is 2.2.3 , and the API I am using is min 21.

The problem is that the height of the EditText field is much to small to display the text. This is present because both the top and the bottom of the EditText field are set according to the TextView left of the EditText. But since the height of the TextView is smaller than the normal height of the EditText you force it to be very narrow (and so there is not enough space for the Text). If you remove this line
android:layout_alignTop="#+id/atcoNameTag"
you can see that the text gets displayed normally.
There are actually several ways to fix this:
you could increase the text size of the TextViews
you could add enough padding on top and on the bottom of the TextViews
you could decrease the text size of the EditText
you could decouple the height of the EditText from the TextView
It is probably a good idea to go with the last solution and put the Textview and EditText fields that belong together into a separate LinearLayout and remove the align calls like this:
<?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">
<LinearLayout
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:weightSum="100"
>
<TextView
android:layout_weight="30"
android:text="Name : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/atcoNameTag" />
<EditText
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:id="#+id/insertName"
android:hint="username"
android:cursorVisible="true"/>
</LinearLayout>
<LinearLayout
android:id="#+id/second"
android:layout_below="#+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:weightSum="100"
>
<TextView
android:layout_weight="30"
android:text="Password : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/passTag"/>
<EditText
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="#+id/insertPass"
android:hint="password"
android:cursorVisible="true"/>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:weightSum="100"
android:layout_marginTop="10dp"
>
<TextView
android:layout_weight="30"
android:text="Role : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/roleNameTag"/>
<Spinner
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"/>
</LinearLayout>
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/loginButton"
android:onClick="onClick"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />
</RelativeLayout>

Related

Add many CardViews with onclick method programmatically

I'm still new to the Android and I want to add a CardView to the activity each time a button is clicked. The CardView has text on it and a background image. I already have the XML file that can add this, but because I want to be able to add more than one, I can't use <include.>. The first image is when the button is clicked once and the second is when the button is clicked 3 times. I already have the onClick for the TextView that says "Click To Add Block" and the XML for the CardView, but I can't make it so that you can add them and change the text in the TextView in each and every one of them. I also can't seem to find a way to programmatically add an onClick listener to the programmatically created CardView. Later down the line, I would also like to be able to delete the CardView from a click of a button too.
Here is the CardView XML file (Before it was inside a Relative Layout)
<androidx.cardview.widget.CardView
android:id="#+id/cardviewClassesBlock1"
android:layout_width="330dp"
android:layout_height="75dp"
android:layout_marginTop="90dp"
android:layout_centerHorizontal="true"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_background">
<TextView
android:id="#+id/textviewClassesBlock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="3dp"
android:textSize="22sp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/white"
android:text="Block A"/>
<ImageView
android:layout_width="60dp"
android:layout_height="6dp"
android:layout_marginStart="10dp"
android:layout_below="#+id/textviewClassesBlock1"
android:background="#drawable/rounded_corner_edittext" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="5dp"
android:textColor="#color/white"
android:text="P - 0 | T - 0 | A - 0"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
I have created a sample project for You.
First You have to create a layout for Your CardView. In res/layout create card_base.xml. In this layout add:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardviewClassesBlock1"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
app:cardCornerRadius="10dp"
android:layout_margin="10dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_background"
>
<TextView
android:id="#+id/textviewClassesBlock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="3dp"
android:text="Block A"
android:textSize="22sp"
/>
<ImageView
android:layout_width="60dp"
android:layout_height="6dp"
android:layout_below="#+id/textviewClassesBlock1"
android:layout_marginStart="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="5dp"
android:text="P - 0 | T - 0 | A - 0"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
This is basically Your CardView with small changes.
Next, in Your activity_main.xml add this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<Button
android:id="#+id/butAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add New Card"
/>
<Button
android:id="#+id/butDoSth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Do something"
/>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/cards"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="#+id/includedLayoutFirst"
layout="#layout/card_base"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
This is a starter (very simple) look of Your app. It has one button and one CardView which is already inserted.
Now in Your MainActivity.java paste this:
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
public class MainActivity extends AppCompatActivity
{
private int starter = 66; //ASCII code for `B`
LinearLayoutCompat cards;
Button buttonAdd;
Button buttonDoSth;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = findViewById(R.id.cards);
buttonAdd = findViewById(R.id.butAdd);
buttonDoSth = findViewById(R.id.butDoSth);
buttonAdd.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
CardView newCard = new CardView(MainActivity.this);
getLayoutInflater().inflate(R.layout.card_base, newCard);
TextView t = newCard.findViewById(R.id.textviewClassesBlock1);
String current = Character.toString((char) starter++);
t.setText("Block " + current);
newCard.setTag(current); //
cards.addView(newCard);
}
});
buttonDoSth.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
findBlockAndDoSomething("B");
}
});
}
private void findBlockAndDoSomething(String name)
{
Log.d("MyTAG", "CLICK");
for (int i = 0; i < cards.getChildCount(); i++)
{
CardView selected = (CardView) cards.getChildAt(i);
if (selected.getTag() != null && selected.getTag().toString().equals(name))
{
// do something. E.g change block name
TextView textViewClassesBlock1 = selected.findViewById(R.id.textviewClassesBlock1);
textViewClassesBlock1.setText("Block XXX");
return;
}
}
}
}
Result (starter code and with adding new CardView):

Android emulator doesn't take input

I wanted to create an android app that adds two number.
Firstly i set up the Layout design.
Secondly in the main_activity file i wrote the code.
This is my code:
package com.example.asus.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
double num1,num2, sum;
EditText firstNumber;
EditText secondNumber;
TextView addResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
//TextView addResult;
Button btnAdd;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstNumber = (EditText)findViewById(R.id.editText);
secondNumber = (EditText)findViewById(R.id.editText2);
addResult = (TextView)findViewById(R.id.textView4);
btnAdd = (Button)findViewById(R.id.button);
//Button button = (Button)findViewById(R.id.button);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1 = Double.parseDouble(firstNumber.getText().toString());
//num1 = Double.parseDouble(firstNumber.getText().toString());
num2 = Double.parseDouble(secondNumber.getText().toString());
//num2 = Double.parseDouble(secondNumber.getText().toString());
sum = num1 + num2;
//sum = num1 + num2;
addResult.setText(Double.toString(sum));
// addResult.setText(Double.toString(sum));
}
});
}
}
my code has no errors, however when i run the android emulator it doesn't take any input.
when i click on letter or number, the cursor moves a step, but no input is shown.
I have tried those solutions that i have found, but none has worked for me:
From advanced settings, check enable keyboard input.
From AVD, create a virtual device, hardware profile, check has hardware keyboard input.
Add in config file hw.keyboard=yes
However none of them worked for me, where is the error exactly?
This is my Layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:text="First Number"
app:layout_constraintBottom_toBottomOf="#+id/editText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/editText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:text="Second Number:"
app:layout_constraintBottom_toBottomOf="#+id/editText2"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="33dp"
android:layout_marginLeft="33dp"
android:text="Result:"
app:layout_constraintBottom_toBottomOf="#+id/editText3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/editText3" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_marginTop="103dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="87dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:text="ADD"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
</android.support.constraint.ConstraintLayout>
show your XML code , it might be :
1- you set a custom input type for Edit Text;
2- your windows/Ubuntu/mac is on a unsupported keyboard language for emulator;
3- in emulator advanced settings keyboard input is disabled ;
The height of the EditText are too small to display the text. So increase the height of those views or change it into wrap_content.

Android app crashes when dialog box is called

I'm still learning how to make android apps and I'm practicing using the different controls, like EditText, radio buttons, dialog boxes, etc.
I'm trying to have the user enter some info into the app and then display that info into a dialog box when they press the "save" button. Whenever I press save, the app crashes.
I know it has something to do with my onClick method where I set all the input to strings so they can be written to the dialog box. It seems to happening when I try to convert the data from the spinner, radio button, and toggle button to string. When I comment those out, the dialog box works fine with just the edit text data.
Can anyone see what's wrong?
Java code
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;
import android.widget.Spinner;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.content.DialogInterface;
import android.app.AlertDialog;
import java.text.DecimalFormat;
import java.util.Calendar;
public class ActMain extends Activity {
//----------------------------------------------------------------
// Variables
//----------------------------------------------------------------
// Declare variables
EditText etAppName;
Spinner spCategory;
RadioGroup rgRating;
RadioButton rbGood;
RadioButton rbFair;
RadioButton rbBad;
ToggleButton tbGooglePlay;
EditText etPrice;
Button btnSave;
//----------------------------------------------------------------
// Activity overrides
//----------------------------------------------------------------
//----------------------------------------------------------------
// onCreate
//----------------------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("### DEBUG ### onCreate started at " + currentTime() + ".");
super.onCreate(savedInstanceState);
setContentView(R.layout.laymain);
// Define edit text controls
etAppName = (EditText) findViewById(R.id.etAppName);
etPrice = (EditText) findViewById(R.id.etPrice);
// Define spinner adapter
String[] categories = {"Business", "Comics", "Education", "Finance", "Games", "Music", "News", "Tools", "Travel", "Weather"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Define spinner control
spCategory = (Spinner) findViewById(R.id.spCategory);
spCategory.setAdapter(adapter);
// Define spinner event
spCategory.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
System.out.println("Spinner: \"" +
parent.getItemAtPosition(position) +
"\" selected.");
}
public void onNothingSelected(AdapterView<?> parent)
{
System.out.println("Spinner: no item selected.");
}
});
// Define save button click event
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Mobile App Reviewer Message");
String appName = etAppName.getText().toString();
String price = etPrice.getText().toString();
String category = spCategory.getSelectedItem().toString();
int rbCheckedId = rgRating.getCheckedRadioButtonId();
RadioButton rbChecked = (RadioButton) findViewById(rbCheckedId);
String rating = rbChecked.getText().toString();
String onGooglePlay;
if(tbGooglePlay.isChecked())
onGooglePlay = "NO";
else
onGooglePlay = "YES";
builder.setMessage("Application: " + appName +
"\nCategory: " + category+
"\nRating: " + rating +
"\nOn Google Play? " + onGooglePlay +
"\nPrice: $" + price +
"\nSave this data?");
builder.show();
}
});
}
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Title rows - Text View
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<TextView
android:text="Software News"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_margin="4dp"/>
<TextView
android:text="Mobile App Reviewer"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_margin="4dp"/>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App info
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App name - Edit text
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Application: "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<EditText
android:id="#+id/etAppName"
android:textSize="16sp"
android:background="#33CCCC"
android:gravity="left"
android:layout_gravity="left"
android:layout_width="100dp"
android:layout_height="25dp"
android:padding="0dp"
android:layout_margin="4dp" />
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App Category - Spinner
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="6dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category: "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<Spinner
android:id="#+id/spCategory"
android:layout_width="160dp"
android:layout_height="wrap_content" />
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App Rating - Radio Buttons
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="6dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rating: "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<RadioGroup
android:id="#+id/rgRating"
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rbGood"
android:text="Good"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/rbFair"
android:text="Fair"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/rbBad"
android:text="Bad"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Google Play? - Toggle Button
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="6dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On Google Play? "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<ToggleButton
android:id="#+id/tbGooglePlay"
android:textOn="NO"
android:textOff="YES"
android:textSize="12sp"
android:textStyle="bold"
android:layout_width="75dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Google Play? - Toggle Button
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="6dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price: $ "
android:textStyle="bold"
android:textSize="14sp"
android:gravity="left"
android:layout_gravity="top"
android:padding="4dp"
android:layout_margin="4dp" />
<EditText
android:id="#+id/etPrice"
android:textSize="16sp"
android:background="#33CCCC"
android:inputType="numberDecimal"
android:gravity="left"
android:layout_gravity="left"
android:layout_width="100dp"
android:layout_height="25dp"
android:padding="0dp"
android:layout_margin="4dp" />
</LinearLayout>
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#A0CFEC"
android:gravity="center"
android:padding="4dp"
android:text="Save"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
rgRating is not assigned to anything. So you will get a nullPointerException when calling getCheckedRadioButtonId.
This would be obvious if you checked the stack trace or debugged the application.
To fix it, assign an ID to rgRating in the XML file and do a
rgRating = findViewById(...)
...before you try to call getCheckedRadioButtonId()
Try this:
AlertDialog dialog = builder.create();
dialog.show();
change this
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
to
AlertDialog.Builder builder = new AlertDialog.Builder(ActMain.this);
So after #DKIT pointed out my error with rgRating, I noticed that I had done the same thing with tbGooglePlay.
In the end, adding these lines of code to the Java file fixed my issue:
rgRating = (RadioGroup) findViewById(R.id.rgRating);
tbGooglePlay = (ToggleButton) findViewById(R.id.tbGooglePlay);

Java physics triangles

okay, i cannot post images yet :/
Basically, i have an application with three edittext fields, three check boxes, a calculation button and a text field to output the answer. i want the program to scan to see which value is missing, so that from this it know which calculation it should do. Another method would be for the user to check one of the checkbox's and then the program would only run the calculation for the corresponding checkbox.
for instance, if the user inputs Q = 10, T = 2, and checks the checkbox for I, the program would then do the math and display I = 20 ?
this make much sense yet
i spose in psuedo code it would look like
" if checkbox one is checked and values for Q and T have been entered, then calculate value for I and display in answers field" however i will be adding some error checks and validation methods in there.
if you need the xml file and java file then please ask, im just looking for a method at the moment as to how i could do it, i wouldnt expect you all to waste your free time writing a whole program for me.
cheers guys and girls
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background1"
android:gravity="fill"
android:orientation="vertical"
android:textColor="#ffffff"
tools:context=".CurrentPage" >
<!--
The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc.
-->
<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows.
-->
<TextView
android:id="#+id/Current_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginTop="33dp"
android:text="Welcome to the Current Calculation page."Hint - Use the check boxes to find the values that are missing "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/Equation_letter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/current_calculate"
android:layout_marginTop="37dp"
android:layout_toLeftOf="#+id/current_calculate"
android:text=" HELLO "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/Distances_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Equation_letter"
android:layout_alignBottom="#+id/Equation_letter"
android:layout_alignLeft="#+id/current_calculate"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<TextView
android:id="#+id/t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Q"
android:layout_alignRight="#+id/Q"
android:layout_below="#+id/Q"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="t ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<CheckBox
android:id="#+id/custom3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/t"
android:layout_alignLeft="#+id/custom2"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<CheckBox
android:id="#+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/number_input_3"
android:layout_alignLeft="#+id/custom3"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Current_hint"
android:layout_alignRight="#+id/Current_hint"
android:layout_below="#+id/Equation_letter"
android:layout_marginTop="25dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/CurrentmainHelp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.31"
android:text="HELP! This equation can be used to calculate the current of an object in amps. The correct equation to discover amps is I=Q/t. Where I = current, Q = charge flowing past a point in the circuit, and t = time taken for the charge to flow. Please note, Q is measure in coulombs and t is measured in seconds, with I being measured in amperes.
Still need more help? "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/Yes_Please"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="36dp"
android:layout_weight="1.31"
android:onClick="CurrentHelp"
android:text="Yes Please"
android:textColor="#ffffff" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/current_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/Calculate_Current"
android:textColor="#ffffff"
/>
<EditText
android:id="#+id/number_input_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_above="#+id/t"
android:layout_alignLeft="#+id/number_input_1"
android:layout_alignRight="#+id/number_input_1"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/number_input_3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/t"
android:layout_alignLeft="#+id/number_input_2"
android:layout_alignRight="#+id/number_input_2"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" />
<CheckBox
android:id="#+id/custom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/number_input_1"
android:layout_marginLeft="45dp"
android:layout_toRightOf="#+id/number_input_1"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<EditText
android:id="#+id/number_input_1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_above="#+id/Q"
android:layout_alignRight="#+id/current_calculate"
android:ems="10"
android:focusable="false"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/Q"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Equation_letter"
android:layout_below="#+id/I"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="Q ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/I"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Q"
android:layout_alignRight="#+id/Q"
android:layout_below="#+id/Current_hint"
android:layout_marginTop="65dp"
android:gravity="right|top"
android:text="I ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
okay, that was the xml, and this is the java so far
package com.kieran.whetherfieldphysicscalculator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
// i=q/t this will be the first calculation on the list
public class Current extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
// Show the Up button in the action bar.
setupActionBar();
Button calc1 = (Button)findViewById(R.id.current_calculate);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
}
public void CurrentHelp(View view) {
// Do something in response to button
Intent intent = new Intent(this, CurrentHelp.class);
startActivity(intent);
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am asking if there is an easier way to formate the program to find the values, rather than have to code each individual segment?
cheers
In your case I guess that you want the user to check only one checkbox and only answer corresponding to that checkbox should be displayed to the user.
so for that you would need to set OnClickListener on checkbox so that the corresponding operation is selected.
Read here on how to set listener on checkbox and use it. Paticularly look for the addListenerOnChkIos() method as it has the code you want.
Also set another OnClickListener on the calculate button and in that check which checkbox is checked depending on which you will perform the operation and update the textview.

Android NullPointer Exception in Activity's onCreate

I have a simple application to multiply two numbers from text boxes and display result in third box. There is no any syntax errors in the code but when I am running an application i get this error: application has stopped unexpectedly.
Here is the java code:
package c.example.rectangle;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
EditText l = (EditText) findViewById(R.id.length);
EditText w = (EditText) findViewById(R.id.width);
TextView a = (TextView) findViewById(R.id.lblarea);
Button b = (Button) findViewById(R.id.calculate);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b.setOnClickListener(this);
}
public void onClick(View v) {
calculateRectangle(l.getText().toString(), w.getText().toString());
}
private void calculateRectangle(String clength, String cwidth){
int area = Integer.parseInt(clength)*Integer.parseInt(cwidth);
b.setText(String.valueOf(area));
}}
And here is my XML file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#8B4513"
android:orientation="vertical" >
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/rect"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/cm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/length"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:ems="10"
android:gravity="center"
android:inputType="number" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2F4F4F"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="#string/breadth"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/width"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="33dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:inputType="number"
android:ems="10"
android:gravity="center"
>
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="fill_parent"
android:layout_marginLeft="100dip"
android:layout_marginRight="100dip"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:layout_marginTop="20dp" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:text="#string/area"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblarea"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
Please help.
I know, that you got correct answer, but I just want to explain you why you need to write code as #Mr.Me says.
You describe your View elements in start of class, and trying to initialize them there. It is not correct. Because you have not attached layout file to activity at the moment when constructor will run your initialization of Views objects. As you can see, you are using findViewById() method, but before use it you should call setContentView().
For better understanding, read Activity Lifecycle, pay attention to rendering proccess.
Rearrange your code to look like this:
EditText l;
EditText w;
TextView a;
Button b;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (EditText) findViewById(R.id.length);
w = (EditText) findViewById(R.id.width);
a = (TextView) findViewById(R.id.lblarea);
b = (Button) findViewById(R.id.calculate);

Categories