I've a simple form page that I want to use to fetch user data. It contains three fields.
1.Name Field (Edit Text type)
2.Radio Button "Male or Female" (Radio Group within Radio Button)
3.Drop Down Menu "choose your country" (Spinner)
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1px">
<TextView
android:id="#+id/tasks_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/tasks"/>
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tasks_title"
android:layout_above="#+id/add_button"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#id/android:empty"
android:text="#string/no_tasks"
android:gravity="center_vertical|center_horizontal"
android:layout_below="#id/tasks_title"
android:layout_above="#+id/add_button"/>
<Button
android:id="#id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add_task"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
I've created another Activity called it FormActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class FormActivity extends Activity {
private Button submitbutton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
submitbutton=(Button)findViewById(R.id.submit_button);
submitbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(FormActivity.this, DisplayActivity.class);
startActivity(intent);
}
});
/**Implements DropDownMenu (spinner) by pushing items into an array an displaying them**/
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.countries_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
How can I use the data from the xml file to be displayed on another page. Please give me instructions as I am learning android now.
Thanks and have a good day
Intent intent = new Intent(FormActivity.this, DisplayActivity.class);
EditText inputName = (EditText) findViewById(R.id.inputText);
String name = inputName.getText().toString();
intent.putExtra("name" , name );
startActivity(intent);
In the new Intent you use this snip:
Bundle extras = getIntent().getExtras();
String name = extras.getString("name");
You can pass data as Extras in the intent but you can also have a custom class, inherited from Application, make it visible and fill it with values that you need. This is useful when you have a constant domain across your application (the same user, the initialization values, etc.) or 'global' functions (read-write local data, test any resource, etc.).
You add the application:name to the Manifest and then you can add your custom code in your own application class.
Related
My app doesn't response when i type
Android app project that has two (2) text fields and one (1) button. The button will
compare the input from the text fields and display a response (SAME if values are the same
and NOT THE SAME if they are not) if it is clicked. You may need to create a new activity
for this.
package com.demesaict203.fieldchecker;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button checkbtn = (Button)findViewById(R.id.checkBtn);
checkbtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
EditText firstttextEditText = (EditText) findViewById(R.id.firsttextEditText);
EditText secondtextEditText = (EditText) findViewById(R.id.secondtextEditText);
if (firstttextEditText.equals(secondtextEditText)){
Intent sameTextIntent = new Intent(getApplicationContext(),SameText.class);
startActivity(sameTextIntent);
}
else{
Intent notsameTextIntent = new Intent(getApplicationContext(),NotTheSame.class);
startActivity(notsameTextIntent);
}
}
});
}
}
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="#+id/checkButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/firsttextEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_word"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="" tools:targetApi="o" />
<EditText
android:id="#+id/secondtextEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_word"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/firsttextEditText" />
<Button
android:id="#+id/checkBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/check"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/secondtextEditText" />
</androidx.constraintlayout.widget.ConstraintLayout>
first of all, you can't compare two EditText references together to get the result of text values equality.
you can get the text wrote in the editText using getText() method
then start to compare both strings values.
also, I suggest declaring EditText out of scope setOnClickListener so that not declare new instances every time the user click button.
so your final java code can be like :
package com.demesaict203.fieldchecker;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText firstttextEditText = (EditText) findViewById(R.id.firsttextEditText);
EditText secondtextEditText = (EditText) findViewById(R.id.secondtextEditText);
Button checkbtn = (Button) findViewById(R.id.checkBtn);
checkbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String firstValue = firstttextEditText.getText().toString();
String secondValue = secondtextEditText.getText().toString();
if (firstValue.equals(secondValue)) {
Intent sameTextIntent = new Intent(getApplicationContext(), SameText.class);
startActivity(sameTextIntent);
} else {
Intent notsameTextIntent = new Intent(getApplicationContext(), NotTheSame.class);
startActivity(notsameTextIntent);
}
}
});
}
}
I suggest you learn more about EditText from here
you are comparing the EditText, not the text inside the edit text. maybe change your codes to something like this.
String firstttextEditText = findViewById(R.id.firsttextEditText)).getText().toString();
String secondtextEditText = findViewById(R.id.secondtextEditText).getText().toString();
if (firstttextEditText.equals(secondtextEditText)){
Intent sameTextIntent = new Intent(getApplicationContext(),SameText.class);
startActivity(sameTextIntent);
}
else{
Intent notsameTextIntent = new Intent(getApplicationContext(),NotTheSame.class);
startActivity(notsameTextIntent);
}
Main activity
List View returns a null value
package com.example.dell.ab;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.lang.reflect.Array;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button a, b, c, d;
String deleteelement;
ListView listView;
String dataarray;
Myadapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
listView=findViewById(R.id.listview);
}
public void sh(View view)
{
Log.d("am"," "+textView);
Log.d("am"," "+listView);
dataarray=textView.getText().toString();
adapter= new Myadapter(this,dataarray);
listView.setAdapter(adapter);
}
}
List activity. x ml
<TextView
android:id="#+id/texttobedisplayed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="score" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview"
android:layout_marginTop="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
The problem here is ListView gives a null value
whereas text view which is a part of activity.xml whose code I did not put return a proper value
CHANGE INFLATED XML and TEXTVIEW ID
setContentView(R.layout.activity_list);
textView = (TextView)findViewById(R.id.texttobedisplayed);
listView= (ListView)findViewById(R.id.listview);
it seems that your xml file name is differ from activity_main that you pass to setContentView. change the parameter you pass to this method to real file name.
Your declaration is wrong listview and text
textView = (TextView)findViewById(R.id.texttobedisplayed );
listView= (ListView)findViewById(R.id.listview);
I have a simple Activity/ LinearLayout combination that has two buttons and changes the background colour when each button is clicked. The code is as below
It appears to me that there is a lot of high-level duplication of code. For example, I should not need to define and make variables out of the buttons and the LinearLayout background in my Java file. When I express my intention to couple them, I should be getting them automatically
So, lines like btnBlue = (Button) findViewById(R.id.btnBlue); should not be necessary. A good framework should allow me to use some conventions (like give me a Java variable btnBlue if my resource file #+id/ resource is named as btnBlue, etc
Where can I find a framework that achieves these for me in Android programming?
Thanks
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
android:weightSum="1"
android:id="#+id/background">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Linear Layout Tutorial"
android:textColor="#ff33ff"
android:textSize="32sp"
/>
<Button
android:id="#+id/btnGreen"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:text="Change to Green"
android:layout_weight="0.07" />
<Button
android:id="#+id/btnBlue"
android:layout_width="400sp"
android:layout_height="wrap_content"
android:text="Change12 to Blue"
android:layout_weight="0.19" />
</LinearLayout>
Activity Java code
package com.example.xxx.testandroid;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private final String TAG = "TKT";
LinearLayout background;
Button btnGreen, btnBlue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.som_linear);
Log.d(TAG, "onCreate");
background = (LinearLayout) findViewById(R.id.background);
btnBlue = (Button) findViewById(R.id.btnBlue);
btnGreen = (Button) findViewById(R.id.btnGreen);
btnGreen.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// click button code here
background.setBackgroundColor(Color.parseColor("#00ff00"));
}
});
btnBlue.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// click button code here
background.setBackgroundColor(Color.parseColor("#006699"));
}
});
}
}
ButterKnife by Jake Wharton seems a good solution for your problem.
I am trying to make a edit text field where i enter a string and then by pressing the button beside it this string is added into a list view on a fragment.
When i compile the code i get the following error:
10-19 15:04:14.620 21147-21147/com.example.gasper.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.gasper.test.TopRatedFragment.onCreateView(TopRatedFragment.java:57)
this is the line 57
b.setOnClickListener(listener);
Does anybody now what is wrong here? I am a beginer in android development so dont judge me :)
I also tried somethong like this but still not working:
if(listener !=null){
b.setOnClickListener(listener);}
package com.example.gasper.test;
Here is the full code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import static com.example.gasper.test.R.layout.fragment_top_rated;
public class TopRatedFragment extends Fragment {
String[] myString =new String[] {"0ne","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"};
List list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(fragment_top_rated, container, false);
for(String activity : myString){
list.add(activity);
}
adapter = new ArrayAdapter<String>(this.getActivity(), fragment_top_rated,R.id.textView,list);
ListView listView = (ListView) rootView.findViewById(R.id.listView);
Button b = (Button) getActivity().findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText edit = (EditText) getActivity().findViewById(R.id.editText);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
getActivity().setContentView(R.layout.fragment_top_rated);
b.setOnClickListener(listener);
listView.setAdapter(adapter);
return rootView;
}
}
Here is the fragment_top:rated.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"
android:orientation="vertical"
android:background="#ffefe4fa" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:layout_below="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="#string/edit_message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick=""/>
</RelativeLayout>
replace this line of your code
Button b = (Button) getActivity().findViewById(R.id.button);
with this line and give it a try
Button b = (Button) rootView.findViewById(R.id.button1);
Edit 1:
adapter = new ArrayAdapter<String>(this.getActivity(), fragment_top_rated,R.id.textView,list);
this line of code shows you are using same xml...is there any textView defined in your this xml..just create a new xml for your list view..and use it here..
It seems that you mistyped the id:
Button b = (Button) getActivity().findViewById(R.id.button);
Your layout has button1 not button.
Edit:
Also the button is found in your fragment's layout not your activity's, so look for it there like this:
Button b = (Button) rootView.findViewById(R.id.button1);
ArrayAdapter has a an internal List of its own. The strings list you pass to its constructor is merely copied to the internal list to initialize the adapter.
User adapter object's add() or addAll() to actually add items to ArrayAdapter.
Change
list.add(edit.getText().toString());
to
adapter.add(edit.getText().toString());
Update 1:
Button b = (Button) getActivity().findViewById(R.id.button);
You are using a fragment and its not yet attached to activity, so activity can't find anything from a view that's not attached to it:
Button b = (Button) rootView.findViewById(R.id.button);
I'm creating my first android app. It seems fairly simple. But I'm a total noob with Java and Android (I'm more familiar with C, C++ and the like). I'm sorry if this is the dumbest question ever. Anyway, I followed the steps on the android dev website.
The app is supposed to have the person enter their Name and click on 1st, 2nd, or 3rd shift radio buttons and when they click on Downtime Button, they'll be brought to another page (activity) that displays their name and the shift they picked and then displays another textbox and a time input.
So far, I got the MainActivity.java done like this:
package com.cyapps.downtime;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.cyapps.downtime.MESSAGE";
public void clickedButton1(View view) {
Intent intent = new Intent(this, WinderDTActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void clickedButton2(View view) {
Intent intent = new Intent(this, ClamperDTActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void clickedButton3(View view) {
Intent intent = new Intent(this, OtherDTActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and the activity_main.xml like this:
<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:orientation="horizontal" >
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="#string/edit_message" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_message"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="#string/radio_button1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton1"
android:layout_centerHorizontal="true"
android:text="#string/radio_button2" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton2"
android:layout_centerHorizontal="true"
android:text="#string/radio_button3" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:text="#string/button_send1"
android:onClick="clickedButton1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button3"
android:layout_centerHorizontal="true"
android:text="#string/button_send2"
android:onClick="clickedButton2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:text="#string/button_send3"
android:onClick="clickedButton3" />
</RelativeLayout>
I now have another activity that shows up when you finish entering in your name and clicking on a shift. This page is supposed to show your name and the shift number and have a textbox to write some other stuff in it and a time input and a submit button. I know how to do buttons and I see the time input on the interface of Eclipse. But I don't understand how to make the radio buttons be able to be "submitted" and shown on the page and how to edit the activity to show certain stuff. I'm confused.
This is how the WinderDTActivity.java looks like:
package com.cyapps.downtime;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class WinderDTActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
}
And this is what the activity_winder_dt.xml looks like:
<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" >
</RelativeLayout>
Thank you so much in advance if you help me. You have no idea how grateful I'll be. I've been trying really hard to understand this, but I'm thoroughly confused on how to get xml and java working together. Please help!
You're already sending the contents of the text box to the second activity just fine, right? Then you only need to do two more things to send the radio button state through as well:
Implement onClick handlers for the radio buttons. You've already done that for the submit buttons, so I assume you've figured out onClick stuff. In the radio button onClick, set a variable in MainActivity.
Package the variable the radio flags set into the intent's extra data, like so:
intent.putExtra("radioButtonState", radioButtonState);
You can read the result back in your second activity using the appropriate intent.getxxxxExtra() function (getIntExtra if you saved an int, for example).
you have to not written Button b1=(Button)findViewById(R.id.Button1) in onCreate methid
this is the reason your are not getting button click event
similarly write code for button2 and button3