I want to display the following output:
Name : Sangeetha
Usn : 4AL09IS025
Address : Anderi West, Mubai.
Name and usn are displaying properly. But my Address is not displaying. What's the problem. Here's my code. Can anyone tell me what's the problem?MainActivity.java is the my main activity class.
package com.example.assignment3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button btn;
EditText etext1;
EditText etext2;
EditText etext3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etext1 = (EditText) findViewById(R.id.edit_name);
etext2 = (EditText) findViewById(R.id.edit_usn);
etext3 = (EditText) findViewById(R.id.edit_add);
btn = (Button) findViewById(R.id.submit);
/** Called when the user clicks the Submit button */
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(this, Details.class);
intent.putExtra("name", etext1.getText().toString());
intent.putExtra("usn", etext2.getText().toString());
intent.putExtra("address", etext3.getText().toString());
startActivity(intent);
}
}
Display.java is my Display activity to receive the intent sent by Main Activity.java
package com.example.assignment3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class Details extends ActionBarActivity {
TextView text1;
TextView text2;
TextView text3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
text1=(TextView)findViewById(R.id.new_name);
text2=(TextView)findViewById(R.id.new_usn);
text3=(TextView)findViewById(R.id.new_add);
Intent intent=getIntent();
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("add");
text1.setText(Name);
text2.setText(Usn);
text3.setText(Address);
}
}
Two layout files. First one is for the first activity(input) and 2nd one is for 2nd activity(output).main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/name"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="20.3dp"
android:text="#string/name" />
<EditText
android:id="#+id/edit_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="85dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_name" />
<TextView
android:id="#+id/usn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="30dp"
android:paddingLeft="32dp"
android:text="#string/usn" />
<EditText
android:id="#+id/edit_usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_name"
android:layout_marginLeft="85dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_usn" />
<TextView
android:id="#+id/add"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="40dp"
android:text="#string/add" />
<EditText
android:id="#+id/edit_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_usn"
android:layout_marginLeft="85dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:inputType="textMultiLine"
android:hint="#string/edit_add" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:layout_centerHorizontal="true"
android:text="#string/submit"/>
</RelativeLayout>
activity_details.java's layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.assignment3.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/name"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="20.3dp"
android:text="#string/name"
android:textStyle="bold" />
<TextView
android:id="#+id/new_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="85dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/usn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="20dp"
android:paddingLeft="32dp"
android:text="#string/usn"
android:textStyle="bold" />
<TextView
android:id="#+id/new_usn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/new_name"
android:layout_marginLeft="85dp"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/add"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#+id/usn"
android:layout_marginTop="30dp"
android:text="#string/add"
android:textStyle="bold" />
<TextView
android:id="#+id/new_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/new_usn"
android:layout_marginLeft="85dp"
android:layout_marginTop="30dp"
android:singleLine="false" />
</RelativeLayout>
try that ;
Define same string for address
Intent intent=getIntent();
String Name=intent.getStringExtra("name");
String Usn=intent.getStringExtra("usn");
String Address=intent.getStringExtra("address");
text1.setText(Name);
text2.setText(Usn);
text3.setText(Address);
See lines intent.putExtra("address", etext3.getText().toString()); and String Address=intent.getStringExtra("add"); You need to change the add to address to make it work
Related
I have an activity and when I click on the "Register" TextView a fragment will open and in that fragment all the Registration detail is there.But when I click on the textview the Fragment is loaded successfully but it did'nt cover up the entire screen.
Here is my code:
MainActivity.java
Register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Fragments fragments=new Fragments();
fragmentTransaction.replace(R.id.Fragment_Register,fragments);
fragmentTransaction.commit();
fragmentTransaction.addToBackStack(null);
}
});
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.attendance.MainActivity"
android:background="#drawable/blurbackgound"
android:padding="10dp"
android:focusableInTouchMode="true"
>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="WELCOME"
android:textColor="#color/white"
android:textStyle="bold"
android:fontFamily="monospace"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:id="#+id/login_mobile_number"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:background="#drawable/edit_text_border"
android:padding="15dp"
android:textColor="#color/white"
android:inputType="number"
android:hint="Enter mobile number"
android:gravity="center"
android:maxLength="10"
android:textColorHint="#color/hint_color"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_local_phone_black_24dp"
android:layout_below="#id/title"
android:layout_marginLeft="30dp"
android:layout_marginTop="73dp"
android:id="#+id/phonelogo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mb_counter"
android:layout_below="#id/title"
android:layout_marginLeft="300dp"
android:layout_alignBaseline="#id/login_mobile_number"
android:text="abssh"
android:textColor="#color/white"
/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/login_mobile_number"
android:layout_marginTop="25dp"
android:background="#drawable/edit_text_border"
android:paddingRight="50dp"
android:hint="Password"
android:gravity="center"
android:textColorHint="#color/hint_color"
android:inputType="textPassword"
android:textColor="#color/white"
android:id="#+id/login_password"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="1dp"
android:layout_below="#id/login_password"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/white"
android:text="Student"
android:textColor="#color/white"
android:id="#+id/Radio_button_student"
android:layout_marginLeft="19dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:buttonTint="#color/white"
android:text="Teacher"
android:textColor="#color/white"
android:id="#+id/Radio_button_teacher"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_alignRight="#id/login_password"
android:layout_alignBaseline="#id/login_password"
android:id="#+id/password_visibility"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_lock_outline_black_50dp"
android:layout_marginLeft="30dp"
android:layout_below="#id/phonelogo"
android:layout_marginTop="39dp"
/>
<Button
android:stateListAnimator="#null"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="Login"
android:id="#+id/login"
android:background="#drawable/login_buttom"
android:textColorHint="#color/hint_color"
android:layout_below="#id/login_password"
android:layout_marginTop="50dp"
android:layout_marginLeft="35dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FORGOT PASSWORD"
android:textColor="#color/white"
android:layout_below="#id/login"
android:layout_marginTop="18dp"
android:layout_marginLeft="48dp"
android:id="#+id/forgot_password"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textColor="#color/white"
android:layout_alignBaseline="#id/forgot_password"
android:layout_marginLeft="170dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REGISTER"
android:textColor="#color/white"
android:layout_below="#id/login"
android:layout_marginLeft="185dp"
android:layout_marginTop="20dp"
android:id="#+id/Register"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Fragment_Register"></RelativeLayout>
</RelativeLayout>
Fragments.java
package com.example.user.attendance;
import android.app.FragmentManager;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import de.hdodenhof.circleimageview.CircleImageView;
public class Fragments extends Fragment {
Button Register_Cancel_fullname, Register_Cancel_email, Register_Cancel_phone,
Register_Cancel_password, Sign_up,Register_Cancel_Confirm_Password;
String Passcode,Confirm_Passcode;
EditText Full_Name, Email_Address, Phone_number, Password_register,Confirm_Password_register;
CircleImageView Register_add_photo;
LinearLayout select_photo_from_camera,select_photo_from_gallery;
TextView textView,hint_textview_circular_image_view;
// RadioButton Category_teacher,Category_student;
RadioGroup Teacher_student;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myfragmentVieew = inflater.inflate(R.layout.fragment_signup, container, false);
//Edit Text
Full_Name = (EditText) myfragmentVieew.findViewById(R.id.Register_name_and_surname);
Email_Address = (EditText) myfragmentVieew.findViewById(R.id.Register_Email_address);
Phone_number = (EditText) myfragmentVieew.findViewById(R.id.Register_Phone);
Password_register = (EditText) myfragmentVieew.findViewById(R.id.Register_Password);
Confirm_Password_register=(EditText)myfragmentVieew.findViewById(R.id
.Confirm_Password_Register);
//Button
Sign_up = (Button) myfragmentVieew.findViewById(R.id.Register_Signup);
Register_Cancel_fullname = (Button) myfragmentVieew.findViewById(R.id.Register_First_Cancel_Button);
Register_Cancel_email = (Button) myfragmentVieew.findViewById(R.id.Register_Second_Cancel_Button);
Register_Cancel_phone = (Button) myfragmentVieew.findViewById(R.id.Register_Third_Cancel_Button);
Register_Cancel_password = (Button) myfragmentVieew.findViewById(R.id.Register_fourth_Cancel_Button);
Register_Cancel_Confirm_Password=(Button)myfragmentVieew.findViewById(R.id
.Register_Cancel_Confirm_Password_Button);
Register_add_photo=(CircleImageView)myfragmentVieew.findViewById(R.id.Register_add_photo);
//TextView
textView = (TextView) myfragmentVieew.findViewById(R.id.Register_textview);
hint_textview_circular_image_view=(TextView)myfragmentVieew.findViewById(R.id
.hint_of_image_view);
return myfragmentVieew;
}
}
Here is what I am getting:
Remove this line from root layout.
android:padding="10dp"
This will solve the problem.
I am creating a diet app for myself and I do not know how to transfer result from one calculation which was done in a different activity and use it in another one.
To make things more complicated, I want my spinner in MoreLooseWeightDetails.class to have a list of different diet types which have different fat, proteins and carb ratio. Every time the user choose different type it should automatically change the ratio.
This is where the calculation is being done and has to be trnasfered to MoreLooseWeightDetails.class
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class LooseWeight extends AppCompatActivity {
TextView TotalCal;
EditText numb2;
Spinner ActLvl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loose_weight);
numb2 = (EditText) findViewById(R.id.value1);
ActLvl = (Spinner) findViewById(R.id.value2);
TotalCal = (TextView)findViewById(R.id.resultDisplay);
final Spinner ActLvl = (Spinner)findViewById(R.id.value2);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(LooseWeight.this, android.R.layout.simple_expandable_list_item_1,getResources().getStringArray(R.array.lvl));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ActLvl.setAdapter(myAdapter);
Button calcBtn = (Button)findViewById(R.id.result);
calcBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
float numb3 = Float.parseFloat(numb2.getText().toString());
float a = numb3 * 24;
float b = a * Float.parseFloat(ActLvl.getSelectedItem().toString());
float c = b - 500;
TotalCal.setText(Float.toString(c));
}
});
Button extra = (Button)findViewById(R.id.advance);
extra.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(),MoreLooseWeightDetails.class);
startActivity(intent);
}
});
}
}
Here is the MoreLooseWeightDetails.class
package com.fitup.fit_up;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MoreLooseWeightDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_more_loose_weight_details);
}
}
Here is the MoreLooseWeightDetails.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:id="#+id/activity_more_loose_weight_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.fitup.fit_up.MoreLooseWeightDetails">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/Title3"
tools:text="More"
android:textSize="36sp" />
<TextView
android:text="Select Type of Diet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Title3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp"
android:id="#+id/Info3"
android:textSize="30sp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Info3"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:id="#+id/spinner" />
<TextView
android:text="You Should Eat :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/Info4"
android:textSize="30sp"
android:layout_below="#+id/spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Protein (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit1"
android:textSize="24sp"
android:layout_below="#+id/Info4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="78dp" />
<TextView
android:text="Fat (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit3"
android:textSize="24sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/result1"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignLeft="#+id/result2"
android:layout_alignStart="#+id/result2" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/unit2"
android:id="#+id/result2"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/result3"
android:layout_alignStart="#+id/result3" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit2"
android:id="#+id/result3"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/unit2"
android:layout_toEndOf="#+id/unit2" />
<TextView
android:text="Carbohydrates (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/unit2"
android:textSize="24sp"
android:layout_below="#+id/unit1"
android:layout_alignRight="#+id/unit1"
android:layout_alignEnd="#+id/unit1" />
</RelativeLayout>
I am trying to develop a simple app to find the day for a given date
xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.dayfinder.MainActivity">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/bg" />
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Date:"
android:textColor="#android:color/black"
android:textSize="35dp" />
<TextView
android:id="#+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Month:"
android:textColor="#android:color/black"
android:textSize="35dp" />
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Year:"
android:textColor="#android:color/black"
android:textSize="35dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="#+id/editText1"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="38dp"
android:layout_weight="1"
android:background="#android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Date"
android:inputType="date"
android:maxLength="2"
android:textAlignment="gravity"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="40dp"
android:layout_weight="1"
android:background="#android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Month!"
android:inputType="date"
android:maxLength="2"
android:textAlignment="gravity"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="41dp"
android:layout_weight="1"
android:background="#android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Year!"
android:inputType="date"
android:maxLength="4"
android:textAlignment="gravity"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_bel="#android:color/white"
android:onClick="chow="#id/l1"
android:layout_centerHorizontal="true"
android:text="Find"
android:layout_margin="24dp"
android:backgroundeck"/>
<TextView
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_margin="48dp"
android:fontFamily="sans-serif-condensed"
android:text="Answer"
android:textColor="#android:color/black"
android:textSize="40dp" />
</RelativeLayout>
MainActivity.java
package com.example.android.dayfinder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import source.dayfinder;
public class MainActivity extends AppCompatActivity {
int e1;
String s1,s2,s3;
private EditText medit1;
private EditText medit2;
private EditText medit3;
private TextView mText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button mbutton=(Button)findViewById(R.id.button);
public void check(View view) {
medit1=(EditText)findViewById(R.id.editText1);
medit2=(EditText)findViewById(R.id.editText2);
medit3=(EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.answer);
s1=medit1.getText().toString();
s2=medit2.getText().toString();
s3=medit3.getText().toString();
dayfinder f=new dayfinder();
f.finder(s1,s2,s3);
mText.setText(f.finder(s1,s2,s3));
}
}
package dayfinder.java (which i use in mainactivity.java)
package source;
public class dayfinder {
public String finder(String a1, String a2, String a3) {
int q, w, x;
String e1;
q = Integer.parseInt(a1);
if ((q < 1000) || (q > 4000)) {
e1 = "error";
return e1;
}
else
{
e1="null";
return e1;
}
}
}
Reformat your MainActivity.java like below and if you have any your error after this again please update your question
Follow naming conventions
Took dayfinder object creation to onCreate to avoid creating new objects every time your check method gets called
package com.example.android.dayfinder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import source.dayfinder;
public class MainActivity extends AppCompatActivity {
int e1;
String s1,s2,s3;
private EditText medit1;
private EditText medit2;
private EditText medit3;
private TextView mText ;
private Button mbutton;
private dayfinder f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
f=new dayfinder();
}
public void initializeViews(){
mbutton=(Button)findViewById(R.id.button);
medit1=(EditText)findViewById(R.id.editText1);
medit2=(EditText)findViewById(R.id.editText2);
medit3=(EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.answer);
}
public void check(View view) {
s1=medit1.getText().toString();
s2=medit2.getText().toString();
s3=medit3.getText().toString();
f.finder(s1,s2,s3);
mText.setText(f.finder(s1,s2,s3));
}
}
I am new to android. I want to know how to switch from a simple activity to drawer activity. I read that there are 2 ways to do it.
The first is Intent i =new Intent("com.example.signin");
the second is Intent i =new Intent(MainActivity.this,signin.class);
I tried both. But in the 1st case, my layout which contains toolbar, drawer and the button inside is appearing perfect. But buttons functionality is not working. in 2nd case, buttons functionality is working but layout is disturbed.
Can anyone tell me solution for this problem?I'm using Android Studio.
here is my code:
MainActivity
package com.example.asus1.tlogin;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button signInBtn;
private Button signUpBtn;
private Button submit;
private EditText mUsername, mPassword;
private Layout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signInBtn = (Button) findViewById(R.id.signin);
signUpBtn = (Button) findViewById(R.id.signup);
submit = (Button) findViewById(R.id.submit);
mUsername = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.password);
//String userName = mUsername.getText().toString();
// String password = mPassword.getText().toString();
signUpBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.asus1.tlogin.SignUpActivity");
startActivity(intent);
}
});
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mUsername.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter username..", Toast.LENGTH_SHORT).show();
mPassword.setText("");
mUsername.setText("");
} else if (mPassword.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter password..", Toast.LENGTH_SHORT).show();
mPassword.setText("");
mUsername.setText("");
} else if (mUsername.getText().toString().equals("gaurav") && mPassword.getText().toString().equals("gaurav")) {
Toast.makeText(getApplicationContext(), "Login Successful..", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,ContentDrawer.class);
startActivity(intent);
setContentView(R.layout.content_drawer);
} else {
Toast.makeText(getApplicationContext(), "Wrong username or password..", Toast.LENGTH_LONG).show();
mPassword.setText("");
mUsername.setText("");
}
}
});
}
}
ContentDrawer.java:
package com.example.asus1.tlogin;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
public class ContentDrawer extends AppCompatActivity {
private Button createBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_drawer);
//Toolbar toolbar = (Toolbar)findViewById(R.id.draw);
createBtn = (Button)findViewById(R.id.createBtn);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(getApplicationContext(),CreateGroup.class);
startActivity(intent1);
}
});
}
}
ContentDrawer.xml:
<?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="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.asus1.tlogin.DrawerActivity"
tools:showIn="#layout/app_bar_drawer"
android:orientation="vertical"
android:weightSum="1"
android:background="#ffffff">
<Button
android:layout_width="match_parent"
android:layout_height="140dp"
android:text="Create your personal profile"
android:background="#d31313"
android:textColor="#ffffff"
android:textAllCaps="false"
android:id="#+id/createBtn"
android:clickable="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:orientation="horizontal"
android:background="#f29494"
android:clickable="true"
android:id="#+id/linear">
<Button
android:layout_width="180dp"
android:layout_height="match_parent"
android:text="Join group"
android:id="#+id/joinGroup"
android:background="#984848"
android:textAllCaps="false"
android:clickable="true" />
<Button
android:layout_width="195dp"
android:layout_height="match_parent"
android:text="Setup a group"
android:id="#+id/button2"
android:background="#4e7db2"
android:textAllCaps="false"
android:clickable="true" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="140dp"
android:text="Setup an eventk"
android:id="#+id/eventBtn"
android:background="#e7ed21"
android:textAllCaps="false"
android:clickable="true" />
<Button
android:layout_width="match_parent"
android:layout_height="140dp"
android:text="Notifications"
android:id="#+id/button4"
android:textAllCaps="false"
android:background="#8b9ddb" />
</LinearLayout>
MainActivity.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="match_parent"
android:layout_height="match_parent"
xmlns:com.facebook.widget.LoginButtonandroid="http://schemas.android.com/apk/res-auto"
tools:context="com.example.asus1.tlogin.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="#drawable/roomates1"
android:id="#+id/imageView" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:id="#+id/linearLayout">
<Button
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/signup"
android:background="#0000"
android:text="SIGN UP"
android:textAlignment="gravity" />
<Button
android:layout_width="200dp"
android:layout_height="40dp"
android:id="#+id/signin"
android:layout_alignBottom="#+id/imageView"
android:layout_toRightOf="#+id/signup"
android:layout_toEndOf="#+id/signup"
android:background="#0000"
android:text=" SIGN IN"
android:textAlignment="textStart"
android:textColor="#fefefe"
android:textStyle="bold" />
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<EditText
android:layout_width="272dp"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/username"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:hint="Username"
android:textAlignment="center" />
<EditText
android:layout_width="274dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/password"
android:layout_gravity="center_horizontal"
android:hint="Password"
android:textAlignment="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIGN IN"
android:id="#+id/submit"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#drawable/roundbutton"/>
</LinearLayout>
</RelativeLayout>
layout is disturbed
I'm assuming what you mean by that is that you are completely replacing the layout of the MainActivity?
That is fixable by just not doing so.
Intent intent = new Intent(MainActivity.this,ContentDrawer.class);
startActivity(intent);
// Remove this line
/* setContentView(R.layout.content_drawer); */
Other than that, it seems you should be able to start the other Activity just fine.
I created an application, my first, and am having problems running it on an AVD. Won't load the first activity, just says 'Unfortunately, FussyFestivalrV7 has stopped. This is the main starting activity:
package com.example.fussyfestivalr;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, News.class);
startActivity(intent);
}
});
}
}
And my layout
<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:background="#string/title"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/newsbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="#string/news" />
<Button
android:id="#+id/exit"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="17dp"
android:layout_toRightOf="#+id/title"
android:text="#string/exit" />
<Button
android:id="#+id/bands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/exit"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="#string/bands" />
<Button
android:id="#+id/fafq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bands"
android:layout_centerHorizontal="true"
android:layout_marginBottom="53dp"
android:text="#string/fafq" />
<Button
android:id="#+id/profiles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fafq"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp"
android:text="#string/festivals" />
Check whether you have </RelativeLayout>.
I hope to help.
Paul.