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.
Related
I am a starter in Android Studio and I am working on the program that reads user input and process the information. I ran into this problem that user input is not getting processed at all even though I did follow the tutorials and videos. Here's the code in MainActivity.java.
package com.example.postcodechecker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int postalcodeinputvalue;
EditText postalcodeinput;
Button submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postalcodeinput = (EditText) findViewById(R.id.postalcodeinput);
Button submit = findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
postalcodeinputvalue = Integer.valueOf(postalcodeinput.getText().toString());
showToast(String.valueOf(postalcodeinputvalue));
}
});
}
private void showToast(String text) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
}
note that new View.OnClickListener() isn't doing anything as the font colour is gray in the editor.
This is the layout file.
<?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"
android:scaleX="1"
android:scaleY="1"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout2"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:fontFamily="sans-serif-condensed"
android:text="Welcome to the Postal Code checker"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="20sp"
android:textStyle="bold"></TextView>
<EditText
android:id="#+id/postalcodeinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter your postal code here"
android:inputType="number" />
<Button
android:id="#+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="check" />
</LinearLayout>
</RelativeLayout>
'''
Thanks a lot if you would like to give some help.
You are passing an Integer to the function ShowText but the constructor expects a String. Do this.
Change
showToast(String.valueOf(postalcodeinputvalue));
To
showToast(postalcodeinput.getText().toString());
I know it have been asked many times but this time i tried many things such as reinstall android studio, update android studio, change the graphics and so on,but it occured everytime.
Here I will also provide my coding if any mistakes were made that may have any relation with my emulator got killed.
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="162dp"
android:layout_marginTop="16dp"
android:text="Login"
android:textSize="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textview"
android:layout_centerHorizontal="true"
android:layout_marginStart="168dp"
android:layout_marginTop="20dp"
android:text="PSIS"
android:textColor="#000000"
android:textSize="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview" />
<EditText
android:id="#+id/editText"
android:layout_width="212dp"
android:layout_height="46dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="100dp"
android:layout_marginTop="76dp"
android:focusable="true"
android:hint="Enter Name"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:layout_alignRight="#+id/editText"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="100dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColorHint="#ffff299f"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="41dp"
android:layout_marginTop="11dp"
android:text="Attempts Left:"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:layout_toEndOf="#+id/textview"
android:layout_toRightOf="#+id/textview"
android:text="New Text"
android:textSize="25dp"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/button2" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="113dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="30dp"
android:layout_toStartOf="#+id/textview"
android:layout_toLeftOf="#+id/textview"
android:text="login"
app:layout_constraintEnd_toStartOf="#+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="30dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="88dp"
android:layout_toEndOf="#+id/textview"
android:layout_toRightOf="#+id/textview"
android:text="Cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.task;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static String EXTRA_TEXT;
Button b1, b2;
EditText ed1, ed2;
TextView tx1;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
ed1 = (EditText) findViewById(R.id.editText);
ed2 = (EditText) findViewById(R.id.editText2);
b2 = (Button) findViewById(R.id.button2);
tx1 = (TextView) findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ed1.getText().toString().equals("mohdnoor") &&
ed2.getText().toString().equals("123456")) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
EditText editText1 = (EditText) findViewById(R.id.editText);
String text = editText1.getText().toString();
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra(EXTRA_TEXT, text);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
activity_second.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="272dp"
android:text="!!WELCOME!!"
android:textSize="35dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="109dp"
android:layout_height="34dp"
android:layout_marginTop="28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview" />
</androidx.constraintlayout.widget.ConstraintLayout>
secondActivity.java
package com.example.task;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class secondActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
b1 = (Button) findViewById(R.id.button2);
Toast.makeText(getApplicationContext(),
"Successful!", Toast.LENGTH_SHORT).show();
Intent intent = getIntent();
String text = intent.getStringExtra(MainActivity.EXTRA_TEXT);
TextView textView1 = (TextView) findViewById(R.id.textview);
textView1.setText(text);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(secondActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
i have been trying to create a QR Code generator that generates the QR Code on a separate activity on button click. Below is my code but it crushes on button click. Where did i go wrong?
First class is MainActivity.class:
package com.example.profmox.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;
public class MainActivity extends AppCompatActivity {
TextView tx1;
Button btn1, btn2;
EditText edt1,edt2;
CheckBox ch;
String text2qr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1 = (EditText)findViewById(R.id.edit1);
edt2 = (EditText)findViewById(R.id.edit2);
btn1 = (Button)findViewById(R.id.login);
btn2 = (Button)findViewById(R.id.signup);
ch = (CheckBox)findViewById(R.id.check);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("EditText", edt1.getText().toString());
Log.v("EditText", edt2.getText().toString());
// text2qr = edt1.getText().toString().trim();
// MultiFormatWriter mfw = new MultiFormatWriter();
// try{
// BitMatrix btm = mfw.encode(text2qr, BarcodeFormat.QR_CODE,250,250);
// BarcodeEncoder ben = new BarcodeEncoder();
// Bitmap bmap = ben.createBitmap(btm);
// UserScreen.qr.setImageBitmap(bmap);
// }catch (WriterException e){
// e.printStackTrace();
// }
startActivity(new Intent(MainActivity.this, UserScreen.class));
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SignUp.class));
}
});
}
}
The layout file for the MainActivity class:
<?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:paddingLeft="18dp"
android:paddingRight="18dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/art_background"
tools:context="com.example.profmox.myapplication.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="300dp"
android:layout_height="230dp"
android:layout_gravity="center_horizontal"
android:padding="16dp"
android:layout_marginTop="20dp"
android:src="#mipmap/logo"/>
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress"
android:ems="10"
android:background="#android:color/transparent"
android:drawablePadding="12dp"
android:padding="8dp"
android:hint="Username"
android:textColorHint="#fff"
android:maxLines="1"
android:drawableLeft="#drawable/girl"
android:layout_marginTop="70dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F2CC8F"/>
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:background="#android:color/transparent"
android:drawablePadding="12dp"
android:padding="8dp"
android:hint="Password"
android:textColorHint="#fff"
android:maxLines="1"
android:layout_marginTop="4dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F2CC8F"/>
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#fff"
android:text="Remember Me"
android:padding="9dp"
/>
<Button
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/login_art"
android:text="Login"
android:textColor="#3D405B"
android:textAllCaps="false"
android:padding="16dp"
android:clickable="true"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:layout_marginTop="23dp"
android:textSize="18sp"/>
<Button
android:id="#+id/signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/signuo_art"
android:text="Sign up"
android:textColor="#fff"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:textAllCaps="false"
android:textSize="18sp"
android:layout_marginTop="16dp"
android:clickable="true"
android:padding="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot your password?"
android:clickable="true"
style="#style/Base.TextAppearance.AppCompat.Body2"
android:padding="16dp"
android:layout_marginBottom="12dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
The UserScreen.class is where i want to generate the QR Code in:
package com.example.profmox.myapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class UserScreen extends AppCompatActivity {
static ImageView qr;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_screen);
qr = (ImageView)findViewById(R.id.qrCode);
}
}
The layout file for the UserScreen.class:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/art_background"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.profmox.myapplication.UserScreen">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/qrCode"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
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
I want to make some buttons that write a letter to an EditText. My java code:
package com.koostamas.keyboard;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
public class MainActivity extends Activity implements OnClickListener {
Button buttona;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttona = (Button) findViewById(R.id.buttona);
buttona.setOnClickListener(this);
buttona.getText();
}
public void onClick(View v) {
Button buttona = (Button)v;
editText.setText(editText.getText().toString()+buttona.getText().toString());
}
}
My layout file:
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/buttonb"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/buttona"
android:text="#string/buttonb"
android:textSize="50sp"
android:textStyle="bold" />
<Button
android:id="#+id/buttona"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/buttona"
android:textSize="50sp"
android:textStyle="bold" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="58"
android:textSize="20sp"
android:textStyle="bold"
android:inputType="text" >
<requestFocus />
</EditText>
</RelativeLayout>
It doesn't work and I don't know why. I found this solution on the Internet and I'm not an expert. Could you write me clearly how to do it?
Thanks in advance.
Take reference of EditText id in the onCreate():
editText = (EditText) findViewById(R.id.editText);