As the title says, I have a simple application, with a ImageButton
in the center. The fact is that by pressing the button, sometimes it does not work the first time I press it, but it works the second time.
The problem occurs randomly, but always after using the menu
EDIT: I modified the MainActivity and added the xml code of the menu.
Main Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ImageButton boto;
CoordinatorLayout principal;
boolean flash;
int seleccionat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Publicidad!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//Publicidad!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
flash=false;
seleccionat = 1;
//Inicialitzacions
boto = findViewById(R.id.imageButton);
principal = findViewById(R.id.mainlayout);
//Iniciem amb fons negre i botó apagat
principal.setBackgroundResource(R.drawable.apagat_gradient_selector);
boto.setImageResource(R.mipmap.sk0);
boto = findViewById(R.id.imageButton);
boto.setOnClickListener(this);
//botó de compartir
FloatingActionButton share = (FloatingActionButton) findViewById(R.id.share);
share.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();
}
});
}
@Override
public void onClick(View view) {
//canviem el boleà segons si tenim el flash engegat o apagat
if (flash){
flash = false;
}else {
flash = true;
}
//switch per posar diferents skins
switch (seleccionat){
case 1:
//al encendre la llum posem el gradient
if (!flash){
boto.setImageResource(R.mipmap.sk0);
principal.setBackgroundResource(R.drawable.apagat_gradient_selector);
}else{
boto.setImageResource(R.mipmap.sk1);
principal.setBackgroundResource(R.drawable.skin1_gradient_selector);
}
//encenem el flash
/*codi per encendre flash*/
break;
case 2:
//al encendre la llum posem el gradient
if (!flash){
boto.setImageResource(R.mipmap.sk0);
principal.setBackgroundResource(R.drawable.apagat_gradient_selector);
}else{
boto.setImageResource(R.mipmap.sk2);
principal.setBackgroundResource(R.drawable.skin2_gradient_selector);
}
//encenem el flash
/*codi per encendre flash*/
break;
case 3:
//al encendre la llum posem el gradient
if (!flash){
boto.setImageResource(R.mipmap.sk0);
principal.setBackgroundResource(R.drawable.apagat_gradient_selector);
}else{
boto.setImageResource(R.mipmap.sk3);
principal.setBackgroundResource(R.drawable.skin3_gradient_selector);
}
//encenem el flash
/*codi per encendre flash*/
break;
case 4:
Toast.makeText(this, "Skin444", Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(this, "Skin555", Toast.LENGTH_SHORT).show();
break;
case 6:
Toast.makeText(this, "Skin666", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
switch (id){
case R.id.action_skin1:
seleccionat = 1;
if (!flash){
boto.setImageResource(R.mipmap.sk0);
principal.setBackgroundResource(R.drawable.apagat_gradient_selector);
}else{
boto.setImageResource(R.mipmap.sk1);
principal.setBackgroundResource(R.drawable.skin1_gradient_selector);
}
break;
case R.id.action_skin2:
seleccionat = 2;
if (!flash){
boto.setImageResource(R.mipmap.sk0);
principal.setBackgroundResource(R.drawable.apagat_gradient_selector);
}else{
boto.setImageResource(R.mipmap.sk2);
principal.setBackgroundResource(R.drawable.skin2_gradient_selector);
}
break;
case R.id.action_skin3:
seleccionat = 3;
if (!flash){
boto.setImageResource(R.mipmap.sk0);
principal.setBackgroundResource(R.drawable.apagat_gradient_selector);
}else{
boto.setImageResource(R.mipmap.sk3);
principal.setBackgroundResource(R.drawable.skin3_gradient_selector);
}
break;
case R.id.action_skin4:
seleccionat = 4;
Toast.makeText(this, "Skin4", Toast.LENGTH_SHORT).show();
break;
case R.id.action_skin5:
seleccionat = 5;
Toast.makeText(this, "Skin5", Toast.LENGTH_SHORT).show();
break;
case R.id.action_skin6:
seleccionat = 6;
Toast.makeText(this, "Skin6", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
the XML
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:background="@android:color/transparent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@null"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="?attr/actionModeSplitBackground"
app:srcCompat="@android:drawable/ic_menu_share" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
<menu 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"
tools:context="gmustudios.threshlantern.MainActivity">
<item
android:id="@+id/action_skin1"
android:orderInCategory="100"
android:title="@string/Skin1"
app:showAsAction="never" />
<item
android:id="@+id/action_skin2"
android:orderInCategory="100"
android:title="@string/Skin2"
app:showAsAction="never" />
<item
android:id="@+id/action_skin3"
android:orderInCategory="100"
android:title="@string/Skin3"
app:showAsAction="never" />
<item
android:id="@+id/action_skin4"
android:orderInCategory="100"
android:title="@string/Skin4"
app:showAsAction="never" />
<item
android:id="@+id/action_skin5"
android:orderInCategory="100"
android:title="@string/Skin5"
app:showAsAction="never" />
<item
android:id="@+id/action_skin6"
android:orderInCategory="100"
android:title="@string/Skin6"
app:showAsAction="never" />
It seems as if by using the menu, the activity will stay in the background until you click and then it works. Does anyone know where the problem might come from?