i am asking you for help. As you see from the picture it is a result that i should have, but, at the moment i have information orinted on my left corner. what i am doing wrog?
MainActivity.java
public class MainActivity extends ListActivity {
/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
TextView mTvSDate;
TextView mTvSName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.SecondElementActivity");
startActivityForResult(intent, 1);
}
};
ImageButton addBtn = (ImageButton) findViewById(R.id.addBtn);
addBtn.setOnClickListener(listener);
Log.d("Suceess1","Sucess1");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Defining the ArrayAdapter to set items to ListView
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
// Getting reference to TextView tv_sage of the layout file activity_student
// mTvSDate = (TextView) findViewById(R.id.editDate);
// Getting reference to TextView tv_sname of the layout file activity_student
mTvSName = (TextView)findViewById(R.id.editName);
Log.d("Suceess5","Sucess5");
// Fetching data from a parcelable object passed from MainActivity
NoteElement drug = getIntent().getParcelableExtra("drug");
// MyAdapter adapter = new MyAdapter(this, generateData());
Log.d("Suceess6","Sucess6");
list.add(mTvSName.getText().toString());
mTvSName.setText(drug.mSName);
adapter.notifyDataSetChanged();
}
#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;
}
}
SecondElementActivity.java
public class SecondElementActivity extends Activity{
EditText mEtSDate;
EditText mEtSName;
Button btnSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_element);
// Getting a reference to EditText et_sname of the layout activity_main
mEtSName = (EditText)findViewById(R.id.editName);
// Getting a reference to EditText et_sage of the layout activity_main
mEtSDate = (EditText)findViewById(R.id.editDate);
// Getting a reference to Button btn_ok of the layout activity_main
btnSave = (Button)findViewById(R.id.btnSave);
// Setting onClick event listener for the "OK" button
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Creating an instance of NoteElement class with user input data
NoteElement drug = new NoteElement(
mEtSDate.getText().toString(),
mEtSName.getText().toString());
// Creating an intent to open the activity MainActivity
Intent intent = new Intent(getBaseContext(), MainActivity.class);
// Passing data as a parecelable object to MainActivity
intent.putExtra("drug",drug);
// Opening the activity
startActivity(intent);
}
});
}
}
Parcelable class NoteElement.java
public class NoteElement implements Parcelable{
String mSDate;
String mSName;
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
/**
* Storing the NoteElement data to Parcel object
**/
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mSDate);
dest.writeString(mSName);
}
/**
* A constructor that initializes the NoteElement object
**/
public NoteElement(String sDate, String sName){
this.mSDate = sDate;
this.mSName = sName;
}
/**
* Retrieving NoteElement data from Parcel object
* This constructor is invoked by the method createFromParcel(Parcel source) of
* the object CREATOR
**/
private NoteElement(Parcel in){
this.mSDate = in.readString();
this.mSName = in.readString();
}
public static final Parcelable.Creator<NoteElement> CREATOR = new Parcelable.Creator<NoteElement>() {
#Override
public NoteElement createFromParcel(Parcel source) {
return new NoteElement(source);
}
#Override
public NoteElement[] newArray(int size) {
return new NoteElement[size];
}
};
}
And my activity_main have such a .xml file
<?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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_new"
android:contentDescription="#string/desc"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_alignParentLeft="true"
android:layout_marginLeft="53dp"
android:text="#string/mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- Student version -->
<TextView
android:id="#+id/tv_sname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<TextView
android:id="#+id/tv_sdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<!-- List -->
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/addBtn" />
</RelativeLayout>
activity_second.xml
<?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" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_new"
android:contentDescription="#string/desc"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_alignParentLeft="true"
android:layout_marginLeft="53dp"
android:text="#string/mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- Date -->
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/textView1"
android:layout_marginRight="36dp"
android:layout_marginTop="14dp"
android:layout_toLeftOf="#+id/editDate"
android:text="#string/date" />
<EditText
android:id="#+id/editDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editName"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="date"
android:hint="#string/str_hnt_date" />
<!-- Name -->
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBottom="#+id/editName"
android:layout_alignLeft="#+id/date"
android:text="#string/name" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/date"
android:ems="10"
android:hint="#string/str_hnt_name" />
<!-- Dosage -->
<TextView
android:id="#+id/dosage"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText3"
android:layout_alignBottom="#+id/editText3"
android:layout_alignLeft="#+id/notes"
android:text="#string/dosage" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editName"
android:layout_below="#+id/editName"
android:ems="10"
android:hint="#string/str_hnt_dosage" />
<!-- Notes -->
<TextView
android:id="#+id/notes"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignBottom="#+id/editText4"
android:layout_alignRight="#+id/name"
android:text="#string/notes" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:ems="10"
android:hint="#string/str_hnt_notes"/>
<!-- buttons: Save and Selete-->
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText4"
android:layout_below="#+id/notes"
android:layout_marginTop="20dp"
android:text="#string/btnSave" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnSave"
android:layout_marginLeft="20dp"
android:layout_toLeftOf="#+id/addBtn"
android:layout_toRightOf="#+id/btnSave"
android:text="#string/btnDelete" />
</RelativeLayout>
Okay so you have two activities total right?
Here's the deal: Look at your image. Activity 1 starts activity 2 right? And you're expecting to get information from your activity 2 back to activity 1, correct? Alright.
First thing to learn:
startActivity(intent);
This method states that you will just initiate an activity but expect nothing back from it. So even if you want to send information back to activity 1 THROUGH activitiy 2 it will not work. Instead you must do this:
startActivityForResult(intent, 1);
The second parameter is an integer that can help you differentiate between different activity calls, it is not important for you right now.
Now, because you say "ForResult" in your method above, in your MainActivity now you must implement this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// here you will work with the code.
// data is your intent data sent from activity 2
// where you say this in your own code:
// Passing data as a parecelable object to MainActivity
// intent.putExtra("drug",drug);
}
Now the last thing to note:
In SecondElementActivity.java where you have this:
// Opening the activity
startActivity(intent);
It is wrong. You know why? because you're saying that you want to start a new activity. But in Android you already have a parent for this activity, which is activity 1. So your activity 1 called 2, when you end activity 2 it will go back to 1. So, replace that line for this:
setResult(RESULT_OK, intent);
finish();
EDIT:
Also I don't know if this is correct, I don't do it this way, so here is my fix:
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.SecondElementActivity");
startActivity(intent);
}
};
When you say you want to make a new intent you should pass two parameters:
Intent intent = new Intent(this, SecondElementActivity.class);
The second parameter is the name of the class you want to call.
You can try like this :
activity_main.xml
<?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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/hello_world"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_centerHorizontal="true"
android:text="mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- List -->
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp" />
</RelativeLayout>
Follow This tutorial for populating ListView just change adapter and row.xml from that tutorial
<?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="wrap_content" >
<TextView
android:id="#+id/tv_sdate "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="5dp"
android:textSize="12sp" >
</TextView>
<TextView
android:id="#+id/tv_sname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/tv_sdate "
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="12sp" >
</TextView>
</RelativeLayout>
http://hmkcode.com/android-custom-listview-items-row/
Related
I am very new at android development, and I am trying to make an app which has 4 buttons in its main activity and when I click on one of its button it takes me to another activity and displays its xml file, what should I write in the 2nd activity? Here is my code so far.
main 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:orientation="vertical">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorAccent"
android:text="Overview"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Information" />
<TableRow
android:id="#+id/hr1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Work Experience" />
<TableRow
android:id="#+id/hr3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
</LinearLayout>
</LinearLayout>
........................
main activity
package com.lakshay.display.piechart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
Button btn1 , btn2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
String nextAct = null ;
String shield = "com.lakshay.display.piechart";
Integer flag= -1;
switch (v.getId())
{
case (R.id.one ):
nextAct = shield + "ContactActicity";
break;
default:
Toast.makeText(MainActivity.this , "Item Currently Unavailable"
, Toast.LENGTH_SHORT).show();
}
try {
if (nextAct!=null)
{
intent = new Intent(MainActivity.this , Class.forName(nextAct));
flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
if (flag != -1 ){
intent.setFlags(flag);
} startActivity(intent);
}
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
...................
xml file for 2nd activity
<?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"
android:background="#color/colorAccent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
tools:context="com.lakshay.display.piechart.ContactActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:inputType="text"
android:paddingBottom="20dp"
android:paddingLeft="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Address"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:id="#+id/address"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="#+id/number"
android:paddingBottom="20dp"
android:background="#android:color/background_light"
android:paddingLeft="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
</LinearLayout>
...................
My answer as a checklist:
1.- If you are using android studio you should create 2nd activity with the assistant so you dont get into more complications.
The 2nd activity must have an xml file and a class file.
2.- You should add android:onClick propertie for your button in the xml file on the activity.
3.- Your 2nd activity must have an onCreate method in the class file to fill the contents of the 2nd activity.
3b.- You can leave onCreate with default content but your xml file must then have al the info of your textviews.
I can elaborate further if needed.
Let's say you have two activities : Activity1.java and Activity2.java.
to start Activity2 from Activity1 just do :
final Intent intent = new Intent(this, Activity2.class);>startActivity(intent)
If you want to start activity on button click you have to write this codein an onClickListener. To do that, write the following attribute in your button definition in Activity1 xml file.
android:onCLick="onButtonClick"
Then in your activity write the following listener :
public void onButtonClick(final View v) {
// put your intent here
}
This code help you ....
public class MainActivity extends AppCompatActivity {
Button btn1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn1);
final String name = editText.getText().toString();
button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
public void onClick(View v) {
Intent intent = new Intent(this, Activity2.class);//this is call your second activity
startActivity(intent);//start activity
}
});
}
}
First of all, you must get your Button id from your xml. Or you will get NullPointerException so change your onCreate like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
And if you want to call intent with Class, you can see this solution
OR
You can simply call another activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstActivity.this, secondActivity.class));
}
});
}
I have a little problem in my android app. I want to put a word in an AutoCompleteTextView and then jump to a specific Activity(depend on the word that the user gave).
The problem is that, when i give a specific word, the program does not correspond with the correct answer as expected but returns a wrong toast message. However with the log that i put i see the correct answer. I think that the solution is silly but i stuck and need to solve this.
the code:
Activity.java
public class paralActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paral);
final String [] temp = {"one","two","three"};
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.clearListSelection();
final RelativeLayout myRelative = (RelativeLayout) findViewById(R.id.find);
myRelative.setVisibility(View.INVISIBLE);
ImageView myImage = (ImageView) findViewById(R.id.aktoploika);
myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(paralActivity.this, topParalActivity.class);
startActivity(intent);
}
});
ImageView myOtherImage = (ImageView) findViewById(R.id.aeroporika);
myOtherImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myRelative.setVisibility(View.VISIBLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(paralActivity.this, android.R.layout.select_dialog_item, temp);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.RED);
ImageView findBeach = (ImageView) findViewById(R.id.find_beach);
findBeach.setOnClickListener(new View.OnClickListener() {
#Override
** public void onClick(View view) {
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
String choice = actv.getText().toString();
Log.i("Answer",choice);
if (choice == "one"){
Intent firstIntent = new Intent(paralActivity.this, nauagioActivity.class);
startActivity(firstIntent);
}else if (choice == temp[1]){
Intent secondIntent = new Intent(paralActivity.this, gerakasActivity.class);
startActivity(secondIntent);
}else if (choice == temp[2]){
Intent thirdIntent = new Intent(paralActivity.this, limnionasActivity.class);
startActivity(thirdIntent);
}else{
Toast.makeText(paralActivity.this,"wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
To help u understand, in this activity a have 2 imageViews. When the user presses the second, a relativeLayout appears (with an AutoCompleteTextView and a button inside). After the user writes the word when presses the button it must go to the specific activity. I declared a String Array (temp[3]) with three words inside and 3 activities for each of the words.
The problem starts in the last onclick method that i put ** . Every time i put a correct word from the Array i take the Toast message but in the log i see the correct.
here is Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_paral"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/main"
android:scaleX="2"
tools:context="com.example.billy.zakynthosapp.paralActivity">
<TextView
android:id="#+id/categories"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/welcome"
android:text="#string/categories"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="11sp"
android:textStyle="bold"
android:textColor="#color/welcome"
android:textAllCaps="true"/>
<ImageView
android:id="#+id/aktoploika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="#id/categories"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="#drawable/par1" />
<TextView
android:id="#+id/aktoploika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aktoploika"
android:layout_alignTop="#+id/aktoploika"
android:layout_alignRight="#+id/aktoploika"
android:layout_alignBottom="#+id/aktoploika"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/paralies"
android:textSize="22sp"
android:textColor="#color/categories"
android:textStyle="bold"/>
<ImageView
android:id="#+id/aeroporika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="#id/aktoploika"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="#drawable/par2" />
<TextView
android:id="#+id/aeroporika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aeroporika"
android:layout_alignTop="#+id/aeroporika"
android:layout_alignRight="#+id/aeroporika"
android:layout_alignBottom="#+id/aeroporika"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/search"
android:textSize="22sp"
android:textColor="#color/categories"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/aeroporika"
android:layout_marginTop="50dp"
android:layout_centerInParent="true"
android:background="#color/categories"
android:scaleX="0.5">
<TextView
android:id="#+id/textView_1"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:text="#string/find_paral"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/find_beach"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="200dp"
android:src="#drawable/find"
android:onClick="find"/>
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView_1"
android:layout_marginTop="10dp"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
Can anyone help me with this?
Thanks a lot!
Try choice.equals("one") instead of choice == "one"
i've a problem. I want to make a app with a loggin activity and a main activity. (To the OnClickListiner later)
fist:
What ive done so far:
i've created a login. java and login.xml
login.java:
public class Login extends Activity implements OnClickListener {
Button btnStartAnotherActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
boolean hasLogedIn = true;
if (hasLogedIn) {
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
finish();
} else {
}
}
public void onClick(View view) {
//calling an activity using <intent-filter> action name
Intent inent = new Intent("android.name.MainActivity ");
startActivity(inent);
}
}
i've created a MainActivity.java and activity_main.xml
moreover i've some other java and xml files to make a materiel designed Tab view for my MainActivity.
i have 3 tabs thats how it looks so far
[![tab1 with buttons][1]][1]
-now i'be added to the first tab 3 buttons.
example of one button: (the others are the same just a other id )
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kommt"
android:clickable="false"
android:textColor="#190707"
android:id="#+id/kommt"
android:layout_alignParentStart="true"
android:layout_below="#+id/space" />
-So the app is running now without problems. ( All Tabs and there content is showing like i want it )
when i build the project the mainactivity is open first( see that link of the picture )
HOW TO SET: the login interface to be started once, when starting the app for the first time. After login is succesfully, then the mainactivity will always open. Thats my first problem. What should i add to the Login.java ? and how to set that the login.xml starts before the mainactivity ?
Second:
As i told you ive added some buttons. To test the buttons i've tried to implement a code for toast notification when clicking on button. But every time i build the project with the toast notifitcation code, the app doenst start anymore. Here the code for the toast notification i'm using:
public class MainActivity extends ActionBarActivity implements OnClickListener {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // but my main_activity doesnt have buttons ... tab1.xml have b
Button kommtbutton;
kommtbutton= (Button) findViewById(R.id.kommt);
kommtbutton.setOnClickListener(this);}
#Override
public void onClick(View v) {
setContentView(R.layout.tab1); // not sure if this is right ive did it cause the buttons are in the tab1 layout and not main_activity
switch(v.getId())
{
case R.id.kommt:
{
Toast toast1 = Toast.makeText(getApplicationContext(),
"Eingestempelt",
Toast.LENGTH_SHORT);
toast1.show();
break;
} ....}
here i have implemented all 3 buttons in a switch case.
It looks right but the onclicklistinier seems to kill my application before it can start. Maybe someone can help me.
i have following files:
Login.java, MainActivity.java, Tab1.java,Tab2.java,Tab3.java, SlidingTabsLayout.java, SlidingTabStrip.java and ViewPagerAdaper.
and i have this layouts.
acitivy_main.xml, login.xml, tab1,tab2,tab3.xml, toolbar.xml.
I'm not allowed to send pictures cause i'm new here.
Where have i do implement the code for the toast notification ?
thats my activity_main: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:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<android_package.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="#color/ColorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"/>
and this is my tab1.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Tabs"
android:background="#FDFDFE"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/buchungen"
android:textColor="#190707"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Geht"
android:clickable="false"
android:textColor="#190707"
android:id="#+id/geht"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/textClock"
android:layout_marginBottom="36dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name:"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:textColor="#190707"
android:id="#+id/name"
android:layout_alignParentStart="true"
android:layout_below="#+id/buchungen" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#190707"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Status:"
android:id="#+id/status"
android:layout_below="#+id/name"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#190707"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Letzte Buchung:"
android:paddingBottom="4dp"
android:paddingTop="7dp"
android:id="#+id/letzteBuchung"
android:layout_below="#+id/status"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="#+id/buchungen"
android:weightSum="1"
android:id="#+id/linearLayout">
<ImageView
android:layout_width="314dp"
android:layout_height="310dp"
android:id="#+id/profilbild"
android:layout_gravity="center_horizontal"
android:src="#drawable/time" />
</LinearLayout>
<Space
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_above="#+id/geht"
android:id="#+id/space" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#190707"
android:id="#+id/textClock"
android:layout_alignTop="#+id/name"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kommt"
android:clickable="false"
android:textColor="#190707"
android:id="#+id/kommt"
android:layout_alignParentStart="true"
android:layout_below="#+id/space" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Pause An/Aus"
android:textColor="#190707"
android:id="#+id/textView"
android:layout_alignTop="#+id/textView3"
android:layout_alignStart="#+id/pause" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Einstempeln"
android:textColor="#190707"
android:id="#+id/textView2"
android:layout_above="#+id/kommt"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Ausstempeln"
android:textColor="#190707"
android:id="#+id/textView3"
android:layout_alignBottom="#+id/geht"
android:layout_alignStart="#+id/geht"
android:layout_alignTop="#+id/textView2" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:textColor="#190707"
android:id="#+id/pause"
android:layout_alignBottom="#+id/kommt"
android:layout_centerHorizontal="true"
android:checked="false" />
</RelativeLayout>
The buttons are in the tab1.xml. Where have i to acces them to make a Interaction ( show toast when pressing the button ) ? in the MainActivity.java or the Tab1.java or somewhere else ?
When i try to add toast notification my app just kills itself ...
You have to put all the findViewById and setContentView in the onCreate method or they won't do the job.
First point :
public class MainActivity extends ActionBarActivity
{
// Layout elements
private Button kommtbutton = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Attach layout
setContentView(R.layout.activity_main); // but my main_activity doesnt have buttons ... tab1.xml have b
// Retrieve layout elements
kommtbutton= (Button) findViewById(R.id.kommt);
// Attach listeners
kommtbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
// Do not use getApplicationContext(), this is an activity
Toast.makeText(MainActivity.this, "Eingestempelt", Toast.LENGTH_SHORT).show();
}
});
}
[...]
}
Second point :
public class LoginActivity extends Activity
{
// Layout elements
private EditText edit_login = null;
private EditText edit_password = null;
private Button btn_login = null;
// Class variables
private SharedPreferences prefs = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Check if the user is already logged in
prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
if (prefs.getBoolean("isLoggedIn", false))
{
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
return;
}
// Attach layout
setContentView(R.layout.login);
// Retrieve layout elements
edit_login = (EditText) findViewById(R.id.edit_login);
edit_password = (EditText) findViewById(R.id.edit_password);
btn_login = (Button) findViewById(R.id.btn_login);
// Attach listeners
btn_login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
// Retrieve information
String login = edit_login.getText().toString();
String password = edit_password.getText().toString();
// Do job
boolean canConnect = true; // TODO
if (canConnect)
{
// Update prefs
prefs.edit().putBoolean("isLoggedIn", true).commit();
// Move to activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
else
{
// Update prefs
prefs.edit().putBoolean("isLoggedIn", false).commit();
// Display error message
Toast.makeText(LoginActivity.this, "Wrong crendentials", Toast.LENGTH_LONG).show();
}
}
});
}
}
I'm having 2 activities in my project where 1st activity has a list view and a button and 2nd activity has EditText and other info and a Button. i want to to pass data from 2nd activity when the user enters details to 1st activity's ListView. Here when I'm entering the data it is not getting displayed in the ListView.. what is the problem ?
Main Activity :-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ListView lv = (ListView) findViewById(R.id.theListView);
if(requestCode==1) {
if (resultCode == RESULT_OK) {
String det_rec = data.getStringExtra("Details");
ArrayList<String> strArr = new ArrayList<String>();
for(int i=0;i<det_rec.length();i++) {
strArr.add("Row :" + i);
}
ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strArr);
lv.setAdapter(adapter);
}
if (resultCode == RESULT_CANCELED) {
return;
}
}
// ListView lv = (ListView) findViewById(R.id.theListView);
// String det_rec = data.getStringExtra("Details");
// ArrayList<String> strArr = new ArrayList<String>();
// for(int i=0;i<det_rec.length();i++){
// strArr.add("Row :"+i);
//}
//ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strArr);
// lv.setAdapter(adapter);
}
}
Second Activity :-
public void add_usr_tsk_btn(View view){
EditText et = (EditText)findViewById(R.id.task_name_edit_txt);
String detls = String.valueOf(et.getText());
Intent goback = getIntent();
goback.putExtra("Details",detls);
setResult(RESULT_OK,goback);
finish();
}
public void cancl_btn(View view) {
Intent goBack = getIntent();
finish();
}
}
activity main :-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/theListView">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Task"
android:id="#+id/button1"
android:background="#drawable/addtask"
android:onClick="onAddCLick"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shwmap"
android:text="Show Map"
android:id="#+id/button2"
android:onClick="onMapbtnClck"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="#+id/button3"
android:onClick="onSetngClck"
/>
</LinearLayout>
second activity :-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left">
<EditText
android:layout_width="364dp"
android:layout_height="wrap_content"
android:id="#+id/task_name_edit_txt"
android:hint="Enter your Details" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Set Location"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Set_loc_btn"
android:onClick="set_usr_loc"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add_task_btn"
android:onClick="add_usr_tsk_btn"
android:id="#+id/add_task"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel_btn"
android:onClick="cancl_btn"
android:id="#+id/cancel_btn"/>
</LinearLayout>
</LinearLayout>
you pass your data with following code:
goback.putExtra("Details",detls);
and you got that with following code:
String det_rec = data.getStringExtra("details");
as you see Details is not same as details, change one of them ,
as you use onActivityResult you must check requestCode and resultCode, for using that field you can read
This
i think you need read creating list tutorial, you can start with This, because i this implementation not worked at all:
ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Integer.parseInt(det_rec));
you must pass one ArrayList and in your code ArrayList<String> to your adapter instead of Integer.parseInt(det_rec)
Hey guys im making an app using zxing integrater I have the scanner working propelry and show the results fine but would like the reslts to showon a separate class activity any ideas?
JavaActivity
public class QRGOLFActivity extends Activity {
TextView contents = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contents = (TextView) findViewById(R.id.contents);
}
public void doScan(View v) {
IntentIntegrator.initiateScan(this);
}
public void onActivityResult(int request, int result, Intent i) {
IntentResult scan = IntentIntegrator.parseActivityResult(request,
result, i);
if (scan != null) {
contents.setText(scan.getContents());
}
}
#Override
public void onSaveInstanceState(Bundle state) {
state.putString("contents", contents.getText().toString());
}
#Override
public void onRestoreInstanceState(Bundle state) {
contents.setText(state.getString("contents"));
}
}
and the 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:orientation="vertical" >
<Button
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="doScan"
android:text="Scan!" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/contents"
android:layout_width="fill_parent"
android:layout_height="116dp"
android:textSize="20dp" />
</ScrollView>
<Button
android:id="#+id/score"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:text="Scorecard" />
<Button
android:id="#+id/about"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="About Us" />
<Button
android:id="#+id/contact"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Contact Us" />
Call startActivity() on "a separate class activity", passing whatever data you want (such as the ZXing results) via extras on the Intent you supply to startActivity(). Your "separate class activity" can then use getIntent() to retrieve the Intent and the various get...Extra() methods to retrieve those values.