Having more than one button crashes my app - java

I am currently creating an app on Android Studio.
I use different buttons (in the same activity) that go to different activities. The problem is that, with one button, it all works, but when I add another one the emulator crashes.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
defineButtons();
}
public void defineButtons() {
findViewById(R.id.next1).setOnClickListener(buttonClickListener);
findViewById(R.id.next2).setOnClickListener(buttonClickListener);
findViewById(R.id.next3).setOnClickListener(buttonClickListener);
}
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.next1:
Intent Next1 = new Intent (Start.this, Next1.class);
startActivity(Next1);
break;
case R.id.next2:
Intent Next2 = new Intent (Start.this, Next2.class);
startActivity(Next2);
break;
case R.id.next3:
Intent Next3 = new Intent (Start.this, Next3.class);
startActivity(Next3);
break;
}
}
};
And here the XML:
<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=".Start"
android:background="#drawable/background">
<TextView
android:layout_width="match_parent"
android:layout_height="700px"
android:id="#+id/statement"
android:text="TEXT1"
android:background="#drawable/button_border"
android:textColor="#000000"
android:alpha="0.95"
android:layout_marginHorizontal="70px"
android:layout_marginTop="180px"
android:gravity="center"
android:paddingHorizontal="50px"
android:textSize="16sp"
/>
<Button
android:id="#+id/next1"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="150px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="160dp"
android:layout_marginHorizontal="70px"
android:alpha="0.9"
android:background="#drawable/button_border"
android:text="TEXT2"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textAllCaps="false"/>
<Button
android:id="#+id/next2"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="150px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp"
android:layout_marginHorizontal="70px"
android:alpha="0.9"
android:background="#drawable/button_border"
android:text="TEXT3"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textAllCaps="false"/>
<Button
android:id="#+id/next3"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="150px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginHorizontal="70px"
android:alpha="0.9"
android:background="#drawable/button_border"
android:text="TEXT4"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textAllCaps="false"/>
</RelativeLayout>
I know this is messy, I'm new at programming.

Your code is perfectly fine you just need to declare your Next2, and Next3 activities in manifest file like this
<activity android:name=".Next2"
android:screenOrientation="portrait"/>
<activity android:name=".Next3"
android:screenOrientation="portrait"/>
Android reads your app components from manifest file, refer this link for more details https://developer.android.com/guide/topics/manifest/manifest-intro

Related

Issues switching activities in Android Studio

I am new to Android Studio, and am creating an app for an event. I have the homepage, with ImageButton's that will open a new activity - Event Information, containing text and a button. The application has a bottom navigation, and once the Event Information has been opened, everytime I click a button on the bottom navigation, it opens the required activity, but the button from the Event Location is on top of the new activity. I can't click on anything on the required activity, just the button that seems to remain on top of all the activities. I am not receiving any errors fro this and have no idea on how to fix it.
Here is the java code for the Event Information page:
public class EventInformation extends AppCompatActivity {
Button eventLocationButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_information);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(navigationListener);
eventLocationButton = findViewById(R.id.btn_EventLocation);
eventLocationButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openEventInformation = new Intent(EventInformation.this, Location.class);
startActivity(openEventInformation);
}
});
}
public void setEvent(String event) {
this.event = event;
BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navigationListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navigationListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
selectedFragment = new HomeFragment();
break;
case R.id.navigation_contactInformation:
selectedFragment = new ContactFragment();
break;
case R.id.navigation_location:
selectedFragment = new LocationFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.container,
selectedFragment).commit();
return true;
}
};
}
The EventInformation layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
tools:context="com.example.u1854455.kimmisquickbook.EventInformation">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="8dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="Event Inofmation"
android:textColor="#android:color/white"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#000000"
app:itemIconTint="#color/backgroundBlue"
app:itemTextColor="#color/backgroundBlue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginTop="8dp"
android:text="The event you have chosen is: "
android:textColor="#android:color/white"
app:layout_constraintTop_toBottomOf="#+id/message"
tools:layout_editor_absoluteX="16dp"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/lb_Artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Name: Drake"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="#+id/textView17"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="87dp" />
<TextView
android:id="#+id/lb_Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Date: 5th October 2019"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="#+id/lb_Artist"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="87dp" />
<TextView
android:id="#+id/lb_Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Start tiime: 19:30"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="#+id/lb_Date"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="87dp" />
<TextView
android:id="#+id/lb_City"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End time: Late"
android:textColor="#android:color/white"
android:textSize="18sp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="87dp"
tools:layout_editor_absoluteY="255dp" />
<TextView
android:id="#+id/lb_Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Location: First Direct Arena, Leeds"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="#+id/lb_City"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="87dp" />
<Button
android:id="#+id/btn_EventLocation"
android:layout_width="154dp"
android:layout_height="37dp"
android:text="Event Location"
app:layout_constraintStart_toStartOf="#+id/lb_Location"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="372dp" />
</android.support.constraint.ConstraintLayout>
The location layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

Can i use startActivityForResult , with one activity?

EDIT: updated my code , posted more of the code for you to see .
EDIT: why am i being downvoted ?
EDIT2: broke my code lol , im going to reverse all you're suggestions , and try the xml option provided
i would like to know if i can use startActivityForResult in my main activity.
i'm opening a scannerview with the code below ,
what i see as my SECOND activiy .
how could i get that result because it's not officially another activity it's only a method.
Button sendButton;
//EditText edt4;
EditText edt2;
#SuppressLint("CutPasteId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt4 = findViewById(R.id.editText4);
ZXingScannerView mScannerView = findViewById(xmlScannerView););
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
public void onClick(View v){
ZXingScannerView mScannerView = new ZXingScannerView(this);
mScannerView.setVisibility(View.VISIBLE);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
//EditText editText4;
EditText edt4;
#Override
public void handleResult(final Result result) {
//handle result
Log.v("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Ordernummer of Locatie");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
//edt4.setText(result.getText());
//edt4 = findViewById(editText4);
//edt4.setText(String.valueOf(result.getText()));
runOnUiThread(new Runnable() {
#Override
public void run() {
updateScannerData(1,result.getText());
}
});
}
private void updateScannerData(int scanType, String scannedCode){
//startActivity(new Intent(this,MainActivity.class));
//this.finish();
edt4.setText(scannedCode);
}
#Override
public void onBackPressed()
{
startActivity(new Intent(this,MainActivity.class));
this.finish();
}
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:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="#mipmap/ic_launcher_foreground">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="#string/scan_locatie"
android:inputType="text"
android:text=""
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText4"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="61dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="197dp"
android:ems="10"
android:hint="#string/scan_order"
android:inputType=""
android:visibility="visible"
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<Button
android:id="#+id/sendButton"
android:layout_width="157dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="72dp"
android:background="#android:color/holo_red_light"
android:text="#string/button"
tools:text="Versturen.." />
<Button
android:id="#+id/button3"
android:layout_width="40dp"
android:layout_height="38dp"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
<Button
android:id="#+id/button4"
android:layout_width="39dp"
android:layout_height="37dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="#+id/xmlScannerView"
android:visibility="gone"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</FrameLayout>
No you can't, either use multiple layout groups in your activity's layout or use multiple fragments.
if you go for the first option, you should add the ZXingScannerView in you XML layout and simple tangle it's visibility if you want to use it
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:width="match_parent"
height="match_parent"
android:id="someId"
android:visibility="gone"
/>
And then in your code
private ZXingScannerView mScannerView;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(someLayout);
mScannerView = findViewById(SomeId);
mScannerView.setFormats(ZXingScannerView.ALL_FORMATS);// dont forget this
mScannerView.setResultHandler(this);
mScannerView.startCamera()
}
public void onClick(View v){
mScannerView.setVisibility(View.Visible);
}
EDIT
Call setFormats see above and start the camera is the onCreate() and change the visibility in the XML to INVISIBLE
EDIT 2
Your XML is supposed to be like this
<FrameLayout 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:background="#mipmap/ic_launcher_foreground">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="#+id/xmlScannerView"
android:visibility="gone"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<RelativeLayout
android:id="#+id/someId"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="#string/scan_locatie"
android:inputType="text"
android:text=""
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText4"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="61dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="197dp"
android:ems="10"
android:hint="#string/scan_order"
android:inputType=""
android:visibility="visible"
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<Button
android:id="#+id/sendButton"
android:layout_width="157dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="72dp"
android:background="#android:color/holo_red_light"
android:text="#string/button"
tools:text="Versturen.." />
<Button
android:id="#+id/button3"
android:layout_width="40dp"
android:layout_height="38dp"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
<Button
android:id="#+id/button4"
android:layout_width="39dp"
android:layout_height="37dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
</RelativeLayout>
</FrameLayout>
And then when calling the onClick
public void onClick(View v){
yourRelativeLayout.setVisibility(View.Invisible);
mScannerView.setVisibility(View.Visible);
}
Well, I think we need to take a look at your current code and the ZXingScannerView. If we take a look at the Github repository of the ZXingScannerView, there is an example which differs a bit from your implementation.
First of all, you should call setContentView(mScannerView);, only in your Activity's onCreate() once. Right now, when you click, the entire view gets redrawn, which is not needed. You also set the resultHandler each time when you click on the button, which might cause problems as well.
This code is taken from the Github repo of the element you are using, so please make your Activity look like this.
https://github.com/dm77/barcodescanner
public class SimpleScannerActivity extends Activity implements ZBarScannerView.ResultHandler {
private ZBarScannerView mScannerView;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZBarScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView); // Set the scanner view as the content view
}
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
public void handleResult(Result rawResult) {
// Do something with the result here
Log.v(TAG, rawResult.getContents()); // Prints scan results
Log.v(TAG, rawResult.getBarcodeFormat().getName()); // Prints the scan format (qrcode, pdf417 etc.)
// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}
}
Also note that you will need permissions to access the phone's camera.

Android Animation only works the first time

I am testing an app that I am in the process of making. When the application starts up, the splash screen is supposed to do a fade in/fade out animation into the login screen. When the application starts up FOR THE FIRST TIME, the animation works fine. But once I clear the application from task manager and restart it, the animation doesn't appear and it goes straight to the login screen with no animation occuring. Attached are the parts of the code dealing with the animation. If any other piece of the code is needed I will provide that as well. I just want it so the animation runs EVERY TIME the application runs.
SplashScreen.java
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timerThread = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}
};
timerThread.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
LoginActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Set up the login form.
registerButton = (Button)findViewById(R.id.button);
registerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this,Register.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
}
});
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="2000" />
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="2000" />
activity_login.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:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.drinkuprewards.julian.drinkuprewards.LoginActivity"
android:background="#97007C">
<!-- Login progress -->
<ProgressBar
android:id="#+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="false">
</ScrollView>
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/imageView"
android:src="#drawable/logosm"
android:contentDescription="#string/mainlogo" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_email"
android:maxLines="1"
android:singleLine="true"
android:autoText="false"
android:background="#fef500"
android:textColor="#000000"
android:inputType="textEmailAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spacer" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_password"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:autoText="false"
android:background="#fef500"
android:textColor="#000000" />
<Button
android:id="#+id/email_sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/action_register"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:background="#fef500"
android:textColor="#000000" />
<Button
style="?android:textAppearanceSmall"
android:id="#+id/button"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/action_sign_in"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:background="#fef500"
android:onClick="Register"
android:textColor="#000000" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/version_number"
android:id="#+id/textView"
android:textColor="#000000" />
</LinearLayout>
do not use sleep() to delay starting another activity. You can do what you want like
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
finish();
}
}, 3000);
this should work.

Why am I unable to get a Dialog when clicking the image?

I want the user to choose that whether they'd like to take picture or choose one from gallery.
I set up an OnClickListener on my Image but when I'm clicking the image, nothing is happening.
Here is my SettingUpUserProfile.java file's code:
public class SettingUpUserProfile extends AppCompatActivity {
public static final int TAKE_PHOTO_REQUEST = 0;
protected ImageView userProfilePicture;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting_up_user_profile);
userProfilePicture = (ImageView) findViewById(R.id.userProfilePicture);
userProfilePicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(SettingUpUserProfile.this);
builder.setTitle(null);
builder.setItems(R.array.pickImage_options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int position) {
switch (position) {
case 0:
Intent intentCaptureFromCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentCaptureFromCamera, TAKE_PHOTO_REQUEST);
break;
case 1:
// Choose from gallery.
}
}
});
}
});
}
}
and here is my activity_setting_up_user_profile.xml file's code:
<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="#color/light_purple"
tools:context="com.abc.xyz.SettingUpUserProfile">
<TextView
android:id="#+id/settingUpUserProfileText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="#string/settingUpUserProfileText1"
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="30sp"
android:gravity="center_horizontal|center_vertical"/>
<ImageView
android:id="#+id/userProfilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/settingUpUserProfileText1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="100dp"
android:clickable="true"
android:src="#drawable/ic_face_white_48dp" />
<TextView
android:id="#+id/settingUpUserProfileText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userProfilePicture"
android:layout_marginTop="5dp"
android:text="#string/settingUpUserProfileText2"
android:textColor="#color/white"
android:textSize="15sp"
android:gravity="center_horizontal|center_vertical"/>
<EditText
android:id="#+id/userName"
android:background="#drawable/phone_number_edit_text_design"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/settingUpUserProfileText2"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:gravity="center_horizontal|center_vertical"
android:hint="#string/hint_userName"
android:textColor="#color/white"
android:textColorHint="#E0E0E0"
android:textCursorDrawable="#null"
android:inputType="textPersonName"/>
<Button
android:id="#+id/buttonAllSet"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_marginTop="20dp"
android:text="#string/button_allSet"
android:textStyle="bold"
android:textColor="#color/light_purple"
android:layout_marginEnd="120dp"
android:layout_marginStart="120dp"
android:gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
I'm really unable to figure out what is wrong here.
Kindly, let me know.
I'm new to StackOverflow so please cooperate.
Thanks in advance.
Add Below lines to your onClick() method
AlertDialog alertDialog = builder.create();
alertDialog.show();

One button working Buttons not working in android

Sorry guys, But I am new, This is my homepage java class.
IntroButton is Working well, but Feedback is not working, i am
touching feedback button, but it is untouched. mean no output.
Created by AwaisButt awais.b7solpk#gmail.com on 4/15/2015.
public class HomePage extends Activity { protected void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
Button introButton = (Button) findViewById(R.id.bIntro);
Button feedBackButton = (Button) findViewById(R.id.bFeedBack);
introButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(HomePage.this,Introduction.class);
startActivity(intent);
}
});
feedBackButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(HomePage.this,FeedBack.class);
startActivity(intent);
}
});
}
XML File
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/homepage">
<Button
android:layout_width="139dp"
android:layout_height="44dp"
android:id="#+id/bIntro"
android:layout_marginLeft="23dp"
android:layout_marginTop="180dp"
android:background="#android:color/transparent"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="139dp"
android:background="#android:color/transparent"
android:layout_height="44dp"
android:text="FeedBack"
android:id="#+id/bFeedBack"
android:layout_marginLeft="23dp"
android:visibility="invisible"
android:layout_marginTop="1dp"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="139dp"
android:background="#android:color/transparent"
android:layout_height="44dp"
android:visibility="invisible"
android:text="Order"
android:id="#+id/bOrder"
android:layout_marginLeft="23dp"
android:layout_marginTop="-4dp"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="139dp"
android:visibility="invisible"
android:layout_height="44dp"
android:text="Catalogue"
android:background="#android:color/transparent"
android:id="#+id/bCatalogue"
android:layout_marginLeft="23dp"
android:layout_marginTop="-7dp"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="139dp"
android:visibility="invisible"
android:layout_height="44dp"
android:text="Web"
android:id="#+id/bWeb"
android:layout_marginLeft="23dp"
android:layout_marginTop="-10dp"
android:background="#android:color/transparent"
android:layout_gravity="center_vertical" /> </LinearLayout>
Mistake:
You did a mistake, visibility of the feedback button is Invisible! Intro button is getting clicked because it's visible in your layout!
FYI
View.GONE This view is invisible, and it doesn't take any space for
layout purposes.
View.INVISIBLE This view is invisible, but it still takes up space for
layout purposes.
Check your OnClickListener() is of type View.OnClickListener().
It's because you set that button as Invisible.
<Button
android:layout_width="139dp"
android:background="#android:color/transparent"
android:layout_height="44dp"
android:text="FeedBack"
android:id="#+id/bFeedBack"
android:layout_marginLeft="23dp"
android:visibility="invisible" // here
android:layout_marginTop="1dp"
android:layout_gravity="center_vertical" />
remove that line. It will work.
One more solution is,
you can use this line in onCreate method after you find the button id.
feedBackButton.setVisibility(View.VISIBLE);
Good luck!

Categories