I'm trying to create an Android game using Android Studio.
However when I'm testing the app from the Android Studio(using my phone as a testing device), there's a white bar which appears at the bottom of the screen. If I switch to another activity and then go back to my main activity, the white bar will dissappear.
Here is my Main Activity 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context="acevix.gladiators3.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#drawable/splash"
android:id="#+id/imageView"
android:scaleType="centerCrop"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="140dp"
android:id="#+id/play"
android:background="#drawable/play" />
<TextView
android:text="GLADIATORS PRE-ALPHA V0.0.1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/textView" />
</RelativeLayout>
And here's my styles.xml file, which I've edited in order to remove some UI:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Activity code:
package acevix.gladiators3;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
MediaPlayer splashbgmusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button playButton = (Button) findViewById(R.id.play);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
playButton.setY((float) (height/4.4));
playButton.getLayoutParams().height = (int) (height / 5.14);
playButton.getLayoutParams().width = (int) (width / 3.47);
playButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
//Log.d("SIZE", "Height " + view.getHeight() + ", Width: " + view.getWidth());
Intent intent = new Intent(MainActivity.this, PlayStage.class);
startActivity(intent);
}
}
);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onResume() {
super.onResume();
splashbgmusic = MediaPlayer.create(MainActivity.this, R.raw.login);
splashbgmusic.setLooping(true); // Set looping
splashbgmusic.setVolume(1.0f, 1.0f);
splashbgmusic.start();
}
#Override
protected void onPause() {
super.onPause();
splashbgmusic.stop();
}
#Override
protected void onStop() {
super.onStop();
splashbgmusic.stop();
}
#Override
public void onWindowFocusChanged(boolean hasFocas) {
super.onWindowFocusChanged(hasFocas);
View decorView = getWindow().getDecorView();
if(hasFocas) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
}
}
UPDATE: Apparently the problem is caused by the styles.xml file. If I change the "Theme.AppCompat.Light.NoActionBar" to "Theme.AppCompat.Light.DarkActionBar", the white bar won't appear anymore BUT when I switch between activities the blue navigation bar at the top showing the name of the game will appear for a short time and then dissappear.
And here's a youtube video which shows you exactly the problem (annotations included): https://www.youtube.com/watch?v=lEdkXZRdsu0
Try doing the following
<?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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:src="#drawable/splash"
android:id="#+id/imageView"
android:scaleType="fitXY"
/>
<!-- <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash"
android:id="#+id/imageView"
/> --> //If above dosent work try this.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="140dp"
android:id="#+id/play"
android:background="#drawable/play" />
<TextView
android:text="GLADIATORS PRE-ALPHA V0.0.1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/textView" />
</RelativeLayout>
P.S With Reference to your comments
in onCreate(...) of your activity write setContentView(R.layout.splash);
getActionBar().hide();
or
setContentView(R.layout.splash);
getSupportActionBar().hide();
to hide the actionbar
Related
I am quite new to MaterialAlertDialogBuilder in android and I getting a purple default color for actively selected TextIputEditText. I need to change the color of inputs when active to my own color, say maybe teal or green... and also the color for the action buttons on the dialog.
Here below is some code:
package com.indupendo.landing.ui.dialogs;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.indupendo.R;
import com.indupendo.databinding.DialogLoginBinding;
import com.indupendo.globals.utilities.Utils;
public class LoginDialog extends DialogFragment {
DialogLoginBinding binding;
View view;
public LoginDialog(View view) {
this.view = view;
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
binding = DialogLoginBinding.inflate(getLayoutInflater());
MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(requireActivity(),
R.style.MaterialAlertDialog_rounded);
dialogBuilder.setView(binding.getRoot());
dialogBuilder.setNegativeButton("Cancel", (dialog, which) -> {
Utils.INSTANCE.showLoginCancellationSnackBar(view, getLayoutInflater());
dialog.cancel();
});
dialogBuilder.setPositiveButton("Login", (dialog, which) -> Toast.makeText(
getActivity(),
"Logged In",
Toast.LENGTH_LONG).show());
return dialogBuilder.create();
}
#Override
public void onCancel(#NonNull DialogInterface dialog) {
super.onCancel(dialog);
Utils.INSTANCE.showLoginCancellationSnackBar(view, getLayoutInflater());
dialog.cancel();
}
}
Layout XML:
<?xml version="1.0" encoding="UTF-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tv_login_dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/login_dialog_title"
android:textAlignment="center"
android:textColor="#color/maroon"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/til_email_user_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
app:boxBackgroundColor="#android:color/transparent"
app:startIconDrawable="#drawable/ic_baseline_email_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_login_dialog_title">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/ti_et_email_user_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:hint="#string/field_hint_email"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/til_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
app:boxBackgroundColor="#android:color/transparent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/til_email_user_id"
app:startIconDrawable="#drawable/ic_baseline_lock_24"
>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/ti_et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:hint="#string/field_hint_password" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
And here is the Theme
<!--- Alert Dialog Theme -->
<style name="MaterialAlertDialog_rounded" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="shapeAppearanceOverlay">#style/DialogCorners</item>
</style>
I'm still new to the Android and I want to add a CardView to the activity each time a button is clicked. The CardView has text on it and a background image. I already have the XML file that can add this, but because I want to be able to add more than one, I can't use <include.>. The first image is when the button is clicked once and the second is when the button is clicked 3 times. I already have the onClick for the TextView that says "Click To Add Block" and the XML for the CardView, but I can't make it so that you can add them and change the text in the TextView in each and every one of them. I also can't seem to find a way to programmatically add an onClick listener to the programmatically created CardView. Later down the line, I would also like to be able to delete the CardView from a click of a button too.
Here is the CardView XML file (Before it was inside a Relative Layout)
<androidx.cardview.widget.CardView
android:id="#+id/cardviewClassesBlock1"
android:layout_width="330dp"
android:layout_height="75dp"
android:layout_marginTop="90dp"
android:layout_centerHorizontal="true"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_background">
<TextView
android:id="#+id/textviewClassesBlock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="3dp"
android:textSize="22sp"
android:fontFamily="#font/amiko_semibold"
android:textColor="#color/white"
android:text="Block A"/>
<ImageView
android:layout_width="60dp"
android:layout_height="6dp"
android:layout_marginStart="10dp"
android:layout_below="#+id/textviewClassesBlock1"
android:background="#drawable/rounded_corner_edittext" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="5dp"
android:textColor="#color/white"
android:text="P - 0 | T - 0 | A - 0"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
I have created a sample project for You.
First You have to create a layout for Your CardView. In res/layout create card_base.xml. In this layout add:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardviewClassesBlock1"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
app:cardCornerRadius="10dp"
android:layout_margin="10dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_background"
>
<TextView
android:id="#+id/textviewClassesBlock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="3dp"
android:text="Block A"
android:textSize="22sp"
/>
<ImageView
android:layout_width="60dp"
android:layout_height="6dp"
android:layout_below="#+id/textviewClassesBlock1"
android:layout_marginStart="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="5dp"
android:text="P - 0 | T - 0 | A - 0"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
This is basically Your CardView with small changes.
Next, in Your activity_main.xml add this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<Button
android:id="#+id/butAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add New Card"
/>
<Button
android:id="#+id/butDoSth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Do something"
/>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/cards"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="#+id/includedLayoutFirst"
layout="#layout/card_base"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
This is a starter (very simple) look of Your app. It has one button and one CardView which is already inserted.
Now in Your MainActivity.java paste this:
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
public class MainActivity extends AppCompatActivity
{
private int starter = 66; //ASCII code for `B`
LinearLayoutCompat cards;
Button buttonAdd;
Button buttonDoSth;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = findViewById(R.id.cards);
buttonAdd = findViewById(R.id.butAdd);
buttonDoSth = findViewById(R.id.butDoSth);
buttonAdd.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
CardView newCard = new CardView(MainActivity.this);
getLayoutInflater().inflate(R.layout.card_base, newCard);
TextView t = newCard.findViewById(R.id.textviewClassesBlock1);
String current = Character.toString((char) starter++);
t.setText("Block " + current);
newCard.setTag(current); //
cards.addView(newCard);
}
});
buttonDoSth.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
findBlockAndDoSomething("B");
}
});
}
private void findBlockAndDoSomething(String name)
{
Log.d("MyTAG", "CLICK");
for (int i = 0; i < cards.getChildCount(); i++)
{
CardView selected = (CardView) cards.getChildAt(i);
if (selected.getTag() != null && selected.getTag().toString().equals(name))
{
// do something. E.g change block name
TextView textViewClassesBlock1 = selected.findViewById(R.id.textviewClassesBlock1);
textViewClassesBlock1.setText("Block XXX");
return;
}
}
}
}
Result (starter code and with adding new CardView):
i'm developing android App which is full-screen App,
in the main activity there are 2 buttons
The issue is when i click in the About button the pop-out activity appear
after click on dismiss the title bar appear in the main activity, see the screen capture:
The main activity
After clicking About button and clicking on dismiss
about_popout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="60"
android:background="#android:color/transparent"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:background="#drawable/about_shape"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12pt"
android:textColor="#FFFFFF"
android:text="It's a PopupWindow" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/common_ic_googleplayservices" />
<Button
android:id="#+id/btn_dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="Dismiss"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#ff5722"
android:orientation="vertical"
tools:context="com.game.circle.thecirclegame.MainMenu">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="To the Game"
android:id="#+id/startButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btn_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20px"
android:layout_marginLeft="100px"
android:layout_marginRight="100px"
android:text="About"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.game.circle.thecirclegame">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainMenu">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GamePanel"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
The class MainMenu.java where the title bar appears, after clicking btn_about
package com.game.circle.thecirclegame;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.app.ActionBar.LayoutParams;
public class MainMenu extends AppCompatActivity {
Button btn_startGame, btn_about;
final Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
setContentView(R.layout.activity_main_menu);
addListinerToButtons();
}
public void addListinerToButtons(){
btn_startGame = (Button)findViewById(R.id.startButton);
btn_about = (Button)findViewById(R.id.btn_about);
btn_startGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,GamePanel.class);
startActivity(intent);
}
});
btn_about.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater)
getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.about_popout, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
Button btn_dismiss = (Button)popupView.findViewById(R.id.btn_dismiss);
btn_dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btn_about, 50, -30);
}
});
}
}
i need to get red of the blue title bar.
You can remove the title bar forever by calling:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
before the setContentView(R.layout.x) in your activity class.
To make the app fullscreen, you can do:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Again, put the above code before the setContentView method.
And make sure, if you are doing this, you should extend your Activity class by Activity and not AppCompatActivity or any other class.
Problem solved.
Because i didn't continue using the popup window,
i create a new activity instead and make the button redirect from main menu to About activity
This is my first attempt with TypedArrays and I believe I've written my code correctly, but for some reason I am only able to see the first image in my array. I have a loop that should be displaying the images repeatedly.
My activity.xml:
<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="wrap_content" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".HomeScreen"
android:background="#android:drawable/screen_background_dark_transparent"
android:clickable="true"
android:id="#+id/homescreen_view">
<Button
android:layout_width="100.0dp"
android:layout_height="25.0dp"
android:text="Help"
android:id="#+id/instructionsButton"
android:layout_alignParentTop="false"
android:layout_alignParentStart="true"
android:textColor="#05ffda"
android:background="#android:color/holo_purple"
android:longClickable="true" />
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerCrop"
android:id="#+id/soundEnable_button"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:longClickable="true"
android:nestedScrollingEnabled="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="25dp"
android:text="Highscores"
android:id="#+id/highscores_button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_purple"
android:textColor="#05ffda" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Play Game"
android:id="#+id/startGame_button"
android:background="#android:color/holo_purple"
android:textColor="#05ffda"
android:layout_marginTop="175dp"
android:singleLine="true"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="musicIcon">
<item>#drawable/musicoff</item>
<item>#drawable/musicon1</item>
<item>#drawable/musicon2</item>
<item>#drawable/musicon3</item>
<item>#drawable/musicon4</item>
<item>#drawable/musicon5</item>
</array>
</resources>
and my activity.java
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
public class HomeScreen extends AppCompatActivity {
ImageButton musicEnable_ImageButton;
Handler musicIcon_animate = new Handler();
int ArrayPos = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
final TypedArray musicIcons = getResources().obtainTypedArray(R.array.musicIcon);
for(ArrayPos =0;ArrayPos<5;ArrayPos++) {
musicIcons.getResourceId(ArrayPos, -1);
}
ArrayPos = 0;
musicEnable_ImageButton = (ImageButton) findViewById(R.id.soundEnable_button);
musicEnable_ImageButton.setImageResource(musicIcons.getResourceId(ArrayPos,-1));
musicIcons.recycle();
musicEnable_ImageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
ArrayPos = 1;
Runnable runnable = new Runnable() {
#Override
public void run() {
musicEnable_ImageButton.setImageResource(musicIcons.getResourceId(ArrayPos,-1));
musicIcons.recycle();
if(ArrayPos < 5) {
ArrayPos++;
}
else{
ArrayPos = 1;
}
Log.d("test", "timer fired");
musicIcon_animate.postDelayed(this, 1);
}
};
musicIcon_animate.postDelayed(runnable, 1);
}
});
}
You can only see the first image because you are recycling your musicIcons TypedArray after you set the first image. TypedArray.recycle() removes all of the references to the resources in the array, making it an empty array. You want to do this, but only after you've gotten what you need from the array.
In your case, since you are using the resources over a period of time, I would suggest doing something like this:
List<Integer> musicIcons = new ArrayList<>();
final TypedArray typedArray = getResources().obtainTypedArray(R.array.musicIcon);
for(int i = 0; i < typedArray.length(); i++) {
musicIcons.add(typedArray.getResourceId(i, -1);
}
typedArray.recycle();
Then set the ImageView resource like this:
musicEnable_ImageButton.setBackgroundResource(musicIcons.get(ArrayPos));
But, I also noticed that it looks like you are just trying to animate the icon, in which case, you should look into using an AnimationDrawable. In this way you can just create a drawable XML file:
<animation-list android:id="#+id/selected" android:oneshot="true">
<item android:drawable="#drawable/musicoff" android:duration="50" />
<item android:drawable="#drawable/musicon1" android:duration="50" />
<item android:drawable="#drawable/musicon2" android:duration="50" />
<item android:drawable="#drawable/musicon3" android:duration="50" />
<item android:drawable="#drawable/musicon4" android:duration="50" />
<item android:drawable="#drawable/musicon5" android:duration="50" />
</animation-list>
Then set the image resource to this animation-list, and animate it:
musicEnable_ImageButton.setImageResource(R.id.sound_button);
AnimationDrawable frameAnimation = (AnimationDrawable) musicEnable_ImageButton.getBackground();
frameAnimation.start();
On API 21+ it is even easier, you can create an AnimatedStateListDrawable to wrap your animation-list (or lists). Then set android:button attribute of a RadioButton to the AnimatedStateListDrawable and watch it animate as you call setChecked().
I'm new to android.
I tried to create a simple toggle button and catch it's state change. But it crashes when I run it on my device.
Here is the MainActivity.java code:
package com.example.addimagebutton;
import android.os.Bundle;
import android.app.Activity;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private ToggleButton tbSwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tbSwitch = (ToggleButton) findViewById(R.id.swButton);
tbSwitch.setOnCheckedChangeListener(toggleListener);
}
OnCheckedChangeListener toggleListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
Toast.makeText(getApplicationContext(), "On",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Off",
Toast.LENGTH_SHORT).show();
}
}
};
}
Here is activity_main.xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Switch
android:id="#+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />
</LinearLayout>
I tried to run this code without the listener objects.(commenting those) But it gave me the same result.
You are casting a switch to Toggle Button so it gives you ClassCastException..
Change this
<Switch
android:id="#+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />
into
<ToggleButton
android:id="#+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />
Used ToggleButton instead of Switch like
<ToggleButton
android:id="#+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On"" />
Becoz your layout contains Switch and you cast it with ToggleButton. That's wrong
Change switch to ToggleButton
<ToggleButton
android:id="#+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />
in your xml you should use ToggleButton instead of Switch - it should look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ToogleButton
android:id="#+id/swButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:textOff="Off"
android:textOn="On" />