Activity is opening very slowly - java

for some reason my activity is opening slowly, do you think that you can help me here?
Now, this is not the only code in Java class, I have 30 buttons that are exactly the same like the one below, only their ID is different since I have 30 card views (widget) in the activity, here is the picture of the design. I also reduced the pictures to the right size for the card, but that did not help. May you please help me?
Java Class:
private Button crkvaspip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_zavrsenilista);
//Dashboard
crkvaspip = (Button) findViewById(R.id.crkvaspip);
crkvaspip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { openCrkvaSvetogPetraIPavla();
}
});
public void openCrkvaSvetogPetraIPavla(){
Intent intent = new Intent(zavrseni_category.this, bioskop_class.class);
startActivity(intent);
}
Android widget card view code:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="210dp"
app:cardCornerRadius="20dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/bioskop1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/thumbnail"
android:maxLines="1"
android:padding="8dp"
android:fontFamily="#font/baloo"
android:text="3D Bioskop"
android:textColor="#222"
android:textSize="20dp" />
<Button
android:id="#+id/crkvaspip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
UPDATE LOGCAT INFO CODE:
2022-08-28 20:21:51.524 4585-4629/com.visit.bp I/OpenGLRenderer: Davey! duration=4111ms; Flags=1, IntendedVsync=38769851610176, Vsync=38773584943360, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=38773585749700, AnimationStart=38773585821500, PerformTraversalsStart=38773586648100, DrawStart=38773756771500, SyncQueued=38773757001800, SyncStart=38773757217800, IssueDrawCommandsStart=38773825367800, SwapBuffers=38773938657200, FrameCompleted=38773963626600, DequeueBufferDuration=332000, QueueBufferDuration=535000,

If the emulator is slow, have you tried disabling your antivirus? Another possibility is to optimize the local images, there are programs such as the free website: https://www.iloveimg.com/ and speed up loading.
Another idea would be to distribute the images in other tabs or activities, because your activity may be overloaded, or load them through other libraries like picasso (I believe it has already been mentioned).

Related

setBackgroundColor is crashing the android application

I recently made a small app that should change the background color & text of the application when a certain amount of money is generated (here 100000). However, whenever I go past 10000, the application crashes. I am not sure why and can not get around it. I saw that this problem is with background color only because it is otherwise displaying text correctly.
Here is the XML for activity_main
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/Trial"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EC5353"
tools:context=".MainActivity">
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:gravity="center"
android:text="#string/Title"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:text="#string/moneyPresent"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Title" />
<Button
android:id="#+id/moneyGenerator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:background="#FFFFFF"
android:backgroundTint="#732424"
android:backgroundTintMode="multiply"
android:text="#string/generateMoneyButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/money" />
<TextView
android:id="#+id/richTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="#string/grind_boi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/moneyGenerator" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my Java Code (Omitted the packages as it would take a lot of space. Do tell me if anyone wants to read it.)
public class MainActivity extends AppCompatActivity {
private Button makeMoney;
private int totalMoney=0;
private TextView moneyText;
private TextView richAlert;
private ConstraintLayout background;
NumberFormat currency=NumberFormat.getCurrencyInstance(new Locale("en","in"));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
makeMoney.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
totalMoney+=1000;
moneyText.setText(currency.format(totalMoney));
Log.d("MoneyTag", "Money Made: "+totalMoney);
Toast.makeText(getApplicationContext(),"Total Money= \u20B9"+totalMoney,Toast.LENGTH_LONG)
.show();
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
background.setBackgroundColor(16776960);
}
}
});
}
}
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
You're finding all the views, except background(ConstraintLayout), that's why it is throwing NullPointerException (Most likely, because you haven't attached logs).
You need to find the layout first
background = findViewById(R.id.Trial);
in onCreate.
you haven't defined "background" view in the above.
to use .setBackgroundColor you need to use it with some view
try making object of layout view then change the color of that layout
mConstraintLayout =
(ConstraintLayout)findViewById(R.id.Trial);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow));
background = findViewById(R.id.Trial);
just find layout and null pointer excpetion will removed

onClick method not working in android studio

I made a side navigation drawer and it works fine but whenever I click any option in navigation drawer nothing happens.It's like it is not taking any input. Onclicking any option I want to redirect user to a new activity but unfortunately it doesn't happens.
XML code is as follows
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="ClickTournament_info"
android:focusable="true">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tournament Info"
android:padding="12dp"
android:layout_marginStart="16dp"/>
<ImageView
android:layout_marginTop="-10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:src="#drawable/tournament_info"/>
</LinearLayout>
Java code is as follows
public void ClickTournament_info(View view){
redirectActivity(this,TournamentInfo.class);
}
public static void redirectActivity(Activity activity,Class aClass) {
//Initialize intent
Intent intent = new Intent(activity,aClass);
//set flag
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//start activity
activity.startActivity(intent);
}
I think you need to use the onNavigationItemSelected method when you want to handle click events on the navigation drawer.
Learn more: https://developer.android.com/reference/com/google/android/material/navigation/NavigationView.OnNavigationItemSelectedListener#onNavigationItemSelected(android.view.MenuItem)
Firstly, try to always create an Onclicklistener instead of adding an onclick attribute in xml.
In your xml, add an ID attribute to your linear layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearlayout"
android:orientation="horizontal"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="ClickTournament_info"
android:focusable="true">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tournament Info"
android:padding="12dp"
android:layout_marginStart="16dp"/>
<ImageView
android:layout_marginTop="-10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:src="#drawable/tournament_info"/>
</LinearLayout>
Now in your classfile inside the Oncreate Method,
LinearLayout LinearLayout = (LinearLayout )findViewById(R.id.linearlayout);
LinearLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
redirectActivity(this,TournamentInfo.class);
}
});
You already have an onClick method. start navigating from that method only.
Try like this,

Game development - how to?

I am creating a game and I have assigned three image views of (balls) at the top of my screen and code an animation for them. The balls are going down to the bottom from the top, but I want the balls from the top of the screen to not show at the start of the animation... any advice?
Here is the relevant Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball1 = (ImageView) findViewById(R.id.ball1);
ball2 = (ImageView) findViewById(R.id.ball2);
ball3 = (ImageView) findViewById(R.id.ball3);
}
private void startBallAnimation() {
final TranslateAnimation animation = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.86f
);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
mhandle.post(new Runnable() {
#Override
public void run() {
setBallColors();
}
});
}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(final Animation animation) {}
}
}
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.example.hp.colorspoof.MainActivity">
<!-- ball imageviews -->
<ImageView
android:id="#+id/ball1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="left"
android:layout_marginLeft="40dp"
app:srcCompat="#drawable/ball_0" />
<ImageView
android:id="#+id/ball2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_horizontal"
app:srcCompat="#drawable/row2_2" />
<ImageView
android:id="#+id/ball3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:layout_marginRight="40dp"
app:srcCompat="#drawable/row3_1" />
</FrameLayout>
Below is a simple untested implementation of what you want to do. I unfortunately no longer have an environment for programming Android setup, so I can't test the code for you, but this should work.
Please note that I made use of negative margins because that is the easiest way to implement it, but it isn't necessarily the best way to achieve it. See here for more: Is it a bad practice to use negative margins in Android?
To provide a summary of the changes in the XML below from what you provided:
I provided a RelativeLayout to contain your playfield. It's generally a good practice to include a FrameLayout at the top level and then use a RelativeLayout or LinearLayout below it to contain the views that will need to be manipulated. RelativeLayout and LinearLayout provide good means of placing views in relation to each other. There is also a newer type of Layout called the ConstraintLayout that I'm not personally familiar with but you may want to look at instead of RelativeLayout. See more here: What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?
I removed the android:layout_gravity statements. You don't need those with a RelativeLayout.
I utilized android:layout_alignParentTop and android:layout_marginTop to place the balls above the top of your layout by exactly the size of the ball.
I utilized android:layout_marginLeft, android:layout_centerHorizontal, and android:layout_marginRight to place the balls horizontally on the screen as you did.
XML below:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.example.hp.colorspoof.MainActivity">
<RelativeLayout
android:id="#+id/play_area"
android:layout_width="match_parent"
android:layout_height"match_parent">
<!-- ball imageviews -->
<ImageView
android:id="#+id/ball1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginTop="-30dp"
android:layout_marginLeft="40dp"
app:srcCompat="#drawable/ball_0" />
<ImageView
android:id="#+id/ball2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="-30dp"
app:srcCompat="#drawable/row2_2" />
<ImageView
android:id="#+id/ball3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="-30dp"
android:layout_marginRight="40dp"
app:srcCompat="#drawable/row3_1" />
</RelativeLayout>
</FrameLayout>

Constraint layout admob issue

I am new to android programming and I do not know many things yet. What I am trying to achieve here is something like this. If the user does not have internet connection the ads dont show up or if the ads for some reason does not load then the ads dont show up. But the layout does remains the same meaning the ad space is left empty. What I did was warp the ad view in inside of a Relative Layout and then created a function which checks if the ads are loaded or not then changes the visibility of the layout, this seems to work and solve the blank space issues when ads does not load. But I think that this is not best way to do this and there must be a better way.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admobTest.MainActivity"
tools:layout_editor_absoluteY="81dp">
<Button
android:id="#+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="#string/btnExit"
ads:layout_constraintBaseline_toBaselineOf="#+id/btnViewBuildProp"
ads:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/btnViewBuildProp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="#string/btnViewBuildProp"
ads:layout_constraintBottom_toTopOf="#+id/relativeAdsLayout"
ads:layout_constraintStart_toStartOf="parent" />
/>
<TextView
android:id="#+id/txvStatus"
android:layout_width="368dp"
android:layout_height="0dp"
android:text="TextView"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
<TextView
android:id="#+id/txvDeviceInfo"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
ads:layout_constraintBottom_toTopOf="#+id/btnViewBuildProp"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintTop_toBottomOf="#+id/txvStatus" />
<RelativeLayout
android:id="#+id/relativeAdsLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:visibility="gone"
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintTop_toBottomOf="#+id/btnExit">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
ads:adSize="#string/adSize"
ads:adUnitId="#string/adUnitId"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="457dp" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java only relevant bits
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showAds(true); // this displays the ads
}
private void showAds(Boolean doShowAds) {
RelativeLayout relativeAdsLayout = findViewById(R.id.relativeAdsLayout);
if (doShowAds.equals(true)) {
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
relativeAdsLayout.setVisibility(View.VISIBLE);
} else if (doShowAds.equals(false)) {
relativeAdsLayout.setVisibility(View.GONE);
}
} //end showAds
} //end class
Don't add the AdView at all if the user has no internet connection.
Also an ad request should be avoided, if you know the user has no proper internet connection.
If the ad request fails you could remove the AdView from the Layout or try an other add request as often as you want.
You might also add some progress or an other background view (or image, progress etc.), wile the ad loads or if the user has no internet

Creating a new instance of an Imagebutton with a button click

The app I am programming has a menu in which the user can pick a new building to build. The user will press "Buy" I would like this to create a new instance of the imagebutton that corresponds to that particular building. My current problem is that I only understand how to do this by setting up these in the xml file and setting them invisible until they buy it. I would like to dynamically create these buildings when they are bought. Is there a way to dynamically create an instance of an image button is this way? I don't really think including my source code is necessary here since I just need an example that works (most examples I have seen just don't work out). But if you would like to see some source code let me know.
Any help is appreciated. Thanks.
Code inside buy building button:
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setImageResource(R.drawable.mainhut);
newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(newColonyHut);
Here is the xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="0dp"
android:layout_marginRight="20dp" >
<Button
android:id="#+id/buyColonyHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_below="#+id/colonyHutPrice" />
</LinearLayout>
You can create ImageView and dynamically add to layout you specified.
sample:
in oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(your_image_here);
image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
test.addView(image);
}
});
the activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/layer"
android:text="Button" />
</LinearLayout>
As you can see ImageView is created when the button is clicked in the layout and then add it to the LinearLayout dynamically.
The LayoutParams is where you set the height and width of your ImageView respected the its parent which is the LinearLayout

Categories

Resources