1

I want my imagebutton to hightlight it self, as the normal glow effect when a "normal" button is pressed. So i tried to put the image in, but then there is a "gray field" in the back of the image so i made it transparent so only the image will show but then the glow effect disappeared. Is there a solution to this? :)

please see imgur image as a example- Example image here

if it is to "hard" I can live with the gray field behind the image but is it possible then to make it green with the same highlight effect?

Should i put the code in MainActivity or my fragment java? Fragment java

public class FirstFragment extends Fragment {



    private View view ;

    public FirstFragment(){


    }
    Button sendSMS;
    Button sendSMSaon;
    Button sendSMSaoff;
    Button sendSMSrela1;
    Button sendSMSrela2;
    EditText msgTxt;

   EditText aonTxt;
    EditText aoffTxt;
    EditText rela1txt;
    EditText rela2txt;
    Button taframnummer;
    TextView nyanumtxt;
//TextView nya;
//TextView nsp;


    TextView tstnr;


    @SuppressWarnings("ResourceType")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.first_layout, container, false);
    return inflater.inflate(R.layout.first_layout, container, false);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        view = getActivity().findViewById(R.id.content_frame);

        nyanumtxt = (TextView) getActivity().findViewById(R.id.raderanumtxt);
        sendSMS = (Button)view.findViewById(R.id.skicka);
       sendSMSaon = (Button)view.findViewById(R.id.skickaaon);
        sendSMSaoff = (Button)view.findViewById(R.id.skickaaoff);
        sendSMSrela1 = (Button)view.findViewById(R.id.skickarela1);
        sendSMSrela2 = (Button)view.findViewById(R.id.skickarela2);







        msgTxt = (EditText)view.findViewById(R.id.Textmeddelande);

        aonTxt = (EditText)view.findViewById(R.id.aon);
        aoffTxt = (EditText)view.findViewById(R.id.aoff);
        rela1txt = (EditText)view.findViewById(R.id.rela1txt);
        rela2txt = (EditText)view.findViewById(R.id.relä2txt);
        taframnummer = (Button) view.findViewById(R.id.taframnummer);
//nsp = (TextView) view.findViewById(R.id.nummertestsp);
      //  tstnr = (TextView) view.findViewById(R.id.numretladd);

       // view = getActivity().findViewById(R.id.content_frame);


        taframnummer.setVisibility(INVISIBLE);
        msgTxt.setVisibility(INVISIBLE);
        aonTxt.setVisibility(INVISIBLE);
        aoffTxt.setVisibility(INVISIBLE);
        rela1txt.setVisibility(INVISIBLE);
        rela2txt.setVisibility(INVISIBLE);



        sendSMSaoff.setOnClickListener(new OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               String mymsgaoff = aoffTxt.getText().toString();
                                              String theNumberr = nyanumtxt.getText().toString();
                                             sendMsg(theNumberr, mymsgaoff);
                                        }

                                     }
        );

        sendSMSaon.setOnClickListener(new OnClickListener() {
                                        @Override
                                          public void onClick(View v) {
                                             String mymsgaon = aonTxt.getText().toString();
                                              String theNumberr = nyanumtxt.getText().toString();
                                             sendMsg(theNumberr, mymsgaon);
                                         }

                                     }







      );
        sendSMS.setOnClickListener(new OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           String myMsg = msgTxt.getText().toString();
                                           String theNumberr = nyanumtxt.getText().toString();
                                           sendMsg(theNumberr, myMsg);
                                       }

                                   }
        );

        sendSMSrela1.setOnClickListener(new OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                String myMsgrela1 = rela1txt.getText().toString();
                                                String theNumberr = nyanumtxt.getText().toString();
                                                sendMsg(theNumberr, myMsgrela1);
                                            }

                                        }
        );
        sendSMSrela2.setOnClickListener(new OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                String mymsgrela2 = rela2txt.getText().toString();
                                                String theNumberr = nyanumtxt.getText().toString();
                                                sendMsg(theNumberr, mymsgrela2);
                                            }

                                        }







        );

       // return view;
    }




    private void sendMsg(String theNumber, String myMsg)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(theNumber, null, myMsg, null, null);

       

    }


    }
Here my MainActivity
public class MainActivity extends AppCompatActivity


        implements NavigationView.OnNavigationItemSelectedListener {
//TextView tstnr;
    TextView radertst;

    Button sendSMSaon;
    EditText aonTxt;
    //TextView nrladd;








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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


       // tstnr = (TextView) findViewById(R.id.nummertestsp);
        radertst = (TextView) findViewById(R.id.raderanumtxt);

        sendSMSaon = (Button)findViewById(R.id.skickaaon);
        aonTxt = (EditText)findViewById(R.id.aon);
     //  nrladd = (TextView)findViewById(R.id.numretladd);


    }
//This is where the call for the value in the setttings are.
//Här är så att man kan lägga in values från inställningar till mainactivity.

    public void displayData(View view){

        SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(this);
    String name = prefs.getString("example_text", "");
   radertst.setText(name + " ");
    }

//downbelow is where the onresume so the value boots up with the app.
//nedanför är för att appen ska ladda koden i settings direkt när man startar

   @Override
    protected void onResume() {
       super.onResume();
      SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(this);

        String name = prefs.getString("example_text", "");
       radertst.setText(name + " ");}



    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);



            return true;
        }




        return super.onOptionsItemSelected(item);
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        android.app.FragmentManager fragmentManager = getFragmentManager();


        if (id == R.id.nav_first_layout) {

            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                    , new FirstFragment())
                    .commit();
            // Handle the camera action
        } else if (id == R.id.nav_second_layout) {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            , new SecondFragment())
                    .commit();

        } else if (id == R.id.nav_third_layout) {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            , new ThirdFragment())
                    .commit();
        }

        else if (id == R.id.nav_share) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://hellmertz.se"));
            startActivity(browserIntent);

            return true;





        }




        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;








}
}
Tim
  • 507
  • 1
  • 6
  • 17
  • You can use **ImageView** instead of ImageButton? – Vishal Puri Sep 17 '16 at 19:05
  • Possible duplicate of [Android button select and press drawable](http://stackoverflow.com/questions/13443534/android-button-select-and-press-drawable) – OneCricketeer Sep 17 '16 at 19:07
  • Maybe this ... Basically, you need a selector to say when the button is pressed. https://stackoverflow.com/questions/5624609/android-how-to-make-a-drawable-selector – OneCricketeer Sep 17 '16 at 19:08

2 Answers2

1

Put the imageView that you want to highlight above a View with bright color and set the visibility of the view equal to View.GONE. Now with your ImageButton

((ImageButton)findViewById(R.id.myImageBtn)).setOnTouchListener(new OnTouchListener() {

  @Override
    public boolean onTouch(View v, MotionEvent event) {
      switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN: {
          myView.setVisibility(View.VISIBLE);
          break;
      }
      case MotionEvent.ACTION_UP:
          myView.setVisibility(View.GONE);
          break;
          // Your action here on button click

      case MotionEvent.ACTION_CANCEL: {
          myView.setVisibility(View.GONE);
          break;
      }
      }
      return true;
    }
});
  • Hi thanks for helping me! Where should i put the code in my Mainactivity or in my fragment code? have no idea where to put the public boolean.. – Tim Sep 17 '16 at 19:15
  • @Tim OnTouchListener() is interface with onTouch method. So just create new instance of it. BTW check my answer it's the right one for your question – Anton Kazakov Sep 18 '16 at 14:24
1

You can user selector drawable and have 3 different button drawables for each state. Create these file is drawables folder and add this line to button android:background="@drawable/button_selector"

button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- pressed -->
    <item android:drawable="@drawable/camera_button_hold" android:state_pressed="true"/>
    <!-- focused -->
    <item android:drawable="@drawable/camera_button_hold" android:state_focused="true"/>
    <!-- default -->
    <item android:drawable="@drawable/camera_button_normal"/>
</selector>

camera_button_hold.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <solid
            android:color="#673ab7"/>
    </shape>

camera_button_normal.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="2dp"
        android:color="#673ab7"/>
</shape>
Anton Kazakov
  • 2,560
  • 3
  • 20
  • 33