Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app working on emulator but not on real device

Just wrote this simple app for testing: one button that displays date and hour, and another button that selects a random color and shows it. It works fine on Emulator but the buttons do nothing (don't work) when i try to run the app on a real device.

Can someone help me understand why?

MainActivity.java:

package yuvallevy.allyouneedapp;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Date;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private Button btnShowTime;
    private Button btnRandomColor;
    private TextView timeText;
    private TextView randomColorView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnRandomColor = (Button) findViewById(R.id.btnRandomColor);
        btnShowTime = (Button) findViewById(R.id.btnShowTime);
        timeText = (TextView) findViewById(R.id.timeText);
        randomColorView = (TextView) findViewById(R.id.randomColorView);

        btnShowTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String currentDataTimeString = java.text.DateFormat.getDateTimeInstance().format(new Date());
                timeText.setText(currentDataTimeString);
            }
        });

        btnRandomColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random rnd = new Random();
                int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
                randomColorView.setBackgroundColor(color);
            }
        });
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:id="@+id/btnShowTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/randomColorView"
        android:layout_toStartOf="@+id/randomColorView"
        android:text="Show Time"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/timeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnRandomColor"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/btnRandomColor"
        android:layout_toRightOf="@+id/btnRandomColor" />

    <Button
        android:id="@+id/btnRandomColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/btnShowTime"
        android:text="Random Color"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/randomColorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btnRandomColor"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/btnRandomColor"
        android:layout_toRightOf="@+id/btnRandomColor" />


</RelativeLayout>

AndoirdManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="yuvallevy.allyouneedapp" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
like image 686
Yuval Levy Avatar asked Feb 08 '23 18:02

Yuval Levy


2 Answers

I suspect that this apparent problem is related to the attribute android:supportsRtl="true" and the different API levels of your device/emualtor.

From the official doc:

android:supportsRtl

Declares whether your application is willing to support right-to-left (RTL) layouts. If set to true and targetSdkVersion is set to 17 or higher, various RTL APIs will be activated and used by the system so your app can display RTL layouts. If set to false or if targetSdkVersion is set to 16 or lower, the RTL APIs will be ignored or will have no effect and your app will behave the same regardless of the layout direction associated to the user's Locale choice (your layouts will always be left-to-right).

The default value of this attribute is false.

This attribute was added in API level 17.

This may cause different behavior between the emualtor and your device.

You need to fix your layout accordingly to the flag, or try to remove this flag.

like image 62
bonnyz Avatar answered Feb 11 '23 14:02

bonnyz


I'm not convinced right-to-left support is the problem, but I agree that the contradictory placements attributes could cause unforeseen problems.

If I were you, I'd get rid of all the confusing relative placement attributes, and I'd replace the RelativeLayout with one simpler vertical LinearLayout containing two horizontal layouts (please don't just cut and paste them from the previous code, it's better to write those LinearLayouts from scratch or use the IDE to initially generate them)

like image 22
Stephan Branczyk Avatar answered Feb 11 '23 15:02

Stephan Branczyk