Monday, 28 May 2018

Android buttons are only clickable on the corners

I have a simple LinearLayout with several buttons, whos state color/text change based on the state of an underlying service, thats working fine.
However the buttons, are only clickable on the right corner ???
The button allSystemServicesToggleButton which i have included the implementation for in this post and only be clicked on the right side/right corner???
Here is my fragment xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_services"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="All System Services"
            android:textColor="#000000"
            android:textSize="20sp" />

        <Button
            android:id="@+id/allSystemServicesToggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="64dp"
            android:text="@string/stopped"
            android:layout_weight="1"
            android:backgroundTint="@color/stoppedServiceColor"
            android:enabled="true"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <!--<TextView
            android:layout_width="163dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="Paired Bluetooth Devices"
            android:textColor="#000000"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/textViewNumberOfConnectedDevices"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/btDevicesToggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="64dp"
            android:text="Pair"
            android:layout_weight="4"
            />-->

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:text="Networks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:textColor="#000000"/>

        <View
            android:id="@+id/viewServicesDivider1"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#808080"
            android:layout_gravity="center"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="82dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Bluetooth Service"
            android:textColor="#000000"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btServicesToggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="48dp"
            android:text="@string/stopped"
            android:layout_weight="1"
            android:backgroundTint="@color/stoppedServiceColor"/>

    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="82dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="MQTT Service"
            android:textColor="#000000"
            android:textSize="20sp" />

        <Button
            android:id="@+id/MQTTserviceToggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:backgroundTint="@color/stoppedServiceColor"
            android:minWidth="48dp"
            android:text="@string/stopped" />

    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:text="Location Services"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:textColor="#000000"/>

        <View
            android:id="@+id/viewServicesDivider3"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#808080"
            android:layout_gravity="center"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="79dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="GPS"
            android:textColor="#000000"
            android:textSize="20sp" />

        <Button
            android:id="@+id/gpsServiceToggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="48dp"
            android:text="@string/stopped"
            android:layout_weight="1"
            android:backgroundTint="@color/stoppedServiceColor"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:text="Command Services"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:textColor="#000000"/>

        <View
            android:id="@+id/viewServicesDivider4"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#808080"
            android:layout_gravity="center"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="82dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Voice Recognition"
            android:textColor="#000000"
            android:textSize="20sp" />

        <Button
            android:id="@+id/voiceRecognitionToggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/stopped"
            android:minWidth="48dp"
            android:layout_weight="1"
            android:backgroundTint="@color/stoppedServiceColor"
            />

    </LinearLayout>

</LinearLayout>

Relevant fragment java:
package x.core.fragments;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import x.core.Application.NgfrApp;
import x.core.R;
import x.core.helpers.Util;
import x.core.services.BluetoothService;
import x.core.services.LocationService;
import x.core.services.MqttBrokerService;
import x.core.services.ServicesStateBroadcastReceiver;
import x.core.services.SpeechRecognitionService;
import x.core.services.UIService;


public class ServicesFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "ServicesFragment";

    public static ServicesFragment newInstance() {
        return new ServicesFragment();
    }

    private static Button btServicesToggleButton;
    private static Button mqttServicesToggleButton;
    private static Button gpsServiceToggleButton;
    private static Button voiceServiceToggleButton;
    private static Button allServiceToggleButton;


    private static String stopped = null;
    private static String running = null;
    private static int runningColorId, stoppedColorId = -1;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_services, container, false);
        btServicesToggleButton = rootView.findViewById(R.id.btServicesToggleButton);
        mqttServicesToggleButton = rootView.findViewById(R.id.MQTTserviceToggleButton);
        gpsServiceToggleButton = rootView.findViewById(R.id.gpsServiceToggleButton);
        voiceServiceToggleButton = rootView.findViewById(R.id.voiceRecognitionToggleButton);
        allServiceToggleButton = rootView.findViewById(R.id.allSystemServicesToggleButton);
        stopped = getResources().getString(R.string.stopped);
        running = getResources().getString(R.string.running);
        runningColorId = getResources().getColor(R.color.runningServiceColor);
        stoppedColorId = getResources().getColor(R.color.stoppedServiceColor);
        allServiceToggleButton.setEnabled(true);
        allServiceToggleButton.setClickable(true);
        allServiceToggleButton.setOnClickListener(this);

        return rootView;


    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.allSystemServicesToggleButton:
                if (ServicesStateBroadcastReceiver.BT_SERVICE_STATE_VALUE==false ||  
ServicesStateBroadcastReceiver.MQTT_STATE_VALUE==false
 || ServicesStateBroadcastReceiver.NGFR_GPS_SERVICE_STATE_VALUE==false 
|| ServicesStateBroadcastReceiver.VOICE_SERVICE_STATE_VALUE==false)
                {

         Toast.makeText(NgfrApp.getContext(),
NgfrApp.getContext().getResources().getString(R.string.restarting_services),
Toast.LENGTH_SHORT).show();    
                 //restartingServices(); 
        }
        else
                {
                    Toast.makeText(NgfrApp.getContext(),
NgfrApp.getContext().getResources().getString(R.string.all_already_running),
Toast.LENGTH_SHORT).show();
                }
                break;
                default: 
                    break; 
        }
    }

}

MainActivity.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/"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <android.support.design.widget.TabLayout
        android:id="@+id/activity_main_tabLyout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed" />

    <android.support.v4.view.ViewPager
        android:id="@+id/activity_main_viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />



</LinearLayout>

MainActivity.java, I only included relevant code:
public class MainActivity extends  AppCompatActivity {
    private static String TAG = "Main";

    private static final int CHECK_BT_CODE = 1;
    private static final int CHECK_TTS_CODE = 2;
    //global boolean flags that will communicate the state of the system at all times
    //bluetooth related flags
    public boolean isBleSupported = false;
    public boolean isBluetoothEnabled = false;
    public boolean accessBluetoothManager= false;
    public boolean nearbyDevices = false;
    //configuration data related
    public boolean isConfigurationLoadedCorrectly = false;
    //text to speech related
    public boolean isTextToSpeechSupported = false;


    private Context context = null;
    private ServicesStateBroadcastReceiver servicesStateBroadcastReciever = null;
    private ViewPager mainViewPager;
    private TabLayout tabLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Activity started!!");
        context = this;
        setContentView(R.layout.activity_main);

        MainActivityViewPager adapter = new MainActivityViewPager(getSupportFragmentManager());

        mainViewPager = (ViewPager) findViewById(R.id.activity_main_viewPager);
        mainViewPager.setAdapter(adapter);

        tabLayout = (TabLayout)  findViewById(R.id.activity_main_tabLyout);
        tabLayout.setupWithViewPager(mainViewPager );
}

}

The adapter for my fragments, FragmentStatePagerAdapter:
package x.core.views;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import x.BiometricsFragment;
import x.ServicesFragment;

public class MainActivityViewPager extends FragmentStatePagerAdapter {
    public MainActivityViewPager(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment returnFragment;

        switch(position) {
            case 0:
                returnFragment = ServicesFragment.newInstance();
                break;
            case 1:
                returnFragment = BiometricsFragment.newInstance();
                break;
            default:
                return null;
        }

        return returnFragment;
    }

    @Override
    public int getCount() {
        return 2;
    }

    public CharSequence getPageTitle(int position) {
        CharSequence title;
        switch (position) {
            case 0:
                title = "Services";
                break;
            case 1:
                title = "Biometrics";
                break;
            default:
                return null;
        }

        return title;


    }
}

Thanks


from Android buttons are only clickable on the corners

No comments:

Post a Comment