There is a good link on how to load large Bitmaps efficiently, so that you don't get an OutOfMemory-Error.
Here ist the link: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
But my problem is, that I am a very newbie and I don't know how to understand the link. Could you please help me and modify my codes?
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="Menue"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="100"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/pfeilrechts"
android:onClick="Picture2"/>
</RelativeLayout>
JAVA-Code:
package com.example.xxx;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class PictureOne extends Activity {
public ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pictureone);
iv = (ImageView)findViewById(R.id.imageView2);}
{
ImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.imageView2, 100, 100));}
public void Picture2 (View view){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);}}
Related
I'm a newbie in programming and stuff.. I'm using the android-youtube-player package from Pierfrancesco Soffritti.. and when I try to Get a reference to YouTubePlayer and run the app it crashes..
`
package com.example.youtuuuuuuuuuuuuuuube;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.YouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.options.IFramePlayerOptions;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.ui.DefaultPlayerUiController;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.ui.PlayerUiController;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.ui.menu.YouTubePlayerMenu;
public class MainActivity extends AppCompatActivity {
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void playvideo(View v){
youTubePlayer.loadVideo("S0Q4gqBUs7c", 1f);
}
}
`
my XML code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="#+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:videoId="S0Q4gqBUs7c"
app:autoPlay="true"
/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="playvideo"
android:text="Button" />
I tried to get the reference using OnStart() method, added a button but all of that don't work :(
Why am getting can not find the symbol? but I already defined "slider1" as a id. Therefore I clean the project and rebuild it again. But it does not work. How to solve that? I put the half of the code and an error was coming from
"sliderLayout = (SliderLayout)findViewById(R.id.slider1);"
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.google.firebase.messaging.FirebaseMessaging;
import com.moraspirit.moraspiritapp.about.AboutActivity;
import com.moraspirit.moraspiritapp.article.ArticleActivity;
import com.moraspirit.moraspiritapp.homemenu.HomeMenuAdapter;
import com.moraspirit.moraspiritapp.homemenu.HomeMenuItem;
import com.moraspirit.moraspiritapp.homemenu.OnItemClickListener;
import com.moraspirit.moraspiritapp.points.RankingActivity;
import com.moraspirit.moraspiritapp.sports.MatchActivity;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class NewHomeActivity extends AppCompatActivity implements BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener {
RecyclerView homeTiles;
List<HomeMenuItem> menuItems;
SliderLayout sliderLayout;
HashMap<String,String> Hash_file_maps ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_home);
Hash_file_maps = new HashMap<String, String>();
sliderLayout = (SliderLayout)findViewById(R.id.slider1);
Here is the activity_new_home.xml file.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
tools:context=".NewHomeActivity">
<LinearLayout
android:id="#+id/textHome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="8dp"
android:gravity="left"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary">
<ImageView
android:id="#+id/logo1"
android:layout_width="75dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:elevation="6dp"
android:src="#drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="#+id/textHome"
android:orientation="horizontal"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/home_tiles"
android:layout_width="401dp"
android:layout_height="382dp"
android:layout_below="#+id/slider1"
android:layout_marginTop="-1dp"
android:layout_marginRight="10dp"
android:scrollbars="vertical"
app:layout_constraintTop_toBottomOf="#+id/slider1">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
Here is the pic about the error which am getting.
enter image description here
listview.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:id="#+id/lstText">
</ListView>
</LinearLayout>
ListView Code
package saint.animaltracking;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.List;
import saint.animaltracking.helper.AnimalAdapter;
import saint.animaltracking.helper.DatabaseHelper;
/**
* Created by Kodie on 3/28/2016.
*/
public class selectAnimal extends AppCompatActivity
{
private ListView lv;
private List<animal> animal;
private DatabaseHelper db;
AnimalAdapter adapter;
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
db = new DatabaseHelper(this.getApplicationContext());
setContentView(R.layout.main);
animal = db.getAllAnimal();
lv = (ListView) findViewById(R.id.lstText);
adapter = new AnimalAdapter(this, R.layout.list_item, animal);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
animal anim = (animal) lv.getItemAtPosition(position);
Intent intent = new Intent(getApplicationContext(), specific.class);
intent.putExtra("animal", anim);
startActivity(intent);
}
});
}
}
specific.class xml (Generic as I am trying to debug the issue)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="#+id/btnButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_toRightOf="#+id/btnButton1"/>
<Button
android:id="#+id/btnButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_below="#+id/btnButton1"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnButton3"
android:layout_marginTop="94dp"
android:text="User :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView1"
android:layout_toRightOf="#+id/btnButton3" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/editText1"
android:text="Submit" />
</RelativeLayout>
Specific.java code
package saint.animaltracking;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
/**
* Created by Kodie on 5/2/2016.
*/
public class specific extends AppCompatActivity {
String id;
private SQLiteOpenHelper AT;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.specific);
Intent intent = getIntent();
}
}
The issue I am running into is that when I click the specific listview item to go to the specific animal, all it shows is a blank page. I'm not really sure why though, but I have a strong feeling it has something to do with the way I am writing the OnItemClickListener and OnItemClick or how it is passing the intent to the specific.java class? I've done just about everything I can think of (I've been working in android for about three weeks) to resolve this, outside of trying to rewrite the OnItemClickListener section. Any tips in the right direction are greatly appreciated. Thanks in advance.
UPDATE:
I get the following messages repeatedly when clicking the listitem
W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent...
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40d53a0
Try to define some textview with random text in spesific class.. or something like Toast, to prove that spesific class is running or not.
How can I change or add more code to make this app to stop crashing? This activity is called "WhatBin", I simply wanted to make a button listen to when a person would click and change a text view. (VIEW PICTURES BELOW FOR MORE DETAILS.)
PICTURES: http://imgur.com/a/ODvHb
package com.example.tiffany.whatbin;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class WhatBin extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_what_bin);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView txt_answer = (TextView) findViewById(R.id.txt_answer);
Button btn_go = (Button) findViewById(R.id.btn_go);
btn_go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_answer.setText("Marker - Recycle Bin");
}
});
};
}
HERE IS THE XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_padddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_what_bin" tools:context=".WhatBin">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
android:id="#+id/btn.go"
android:layout_marginBottom="113dp"
android:textColor="#drawable/abc_dialog_material_background_dark"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textEdit"
android:layout_alignBottom="#+id/btn.go"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/btn.go"
android:layout_toStartOf="#+id/btn.go" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/instructiontext"
android:id="#+id/textinstruc"
android:layout_below="#+id/textEdit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/btn.go"
android:layout_toStartOf="#+id/btn.go" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/txt.1"
android:id="#+id/txt.answer"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
UPDATE
You are currently using abc_dialog_material_background_dark as textcolor. As you can see in the source of this resource, it is basically a shape. And, shape cannot be used for textcolor.
In your XML file at line 17 you have used a color. That color is of wrong type. My guess, you have used a drawable instead of a color.
Post your activity_what_bin.xml for further explanation.
I have 3 activities which I want to use different backgrounds with. but when i do this i get this i get this error however if i only use different backgrounds on 2 activities then the app works
In the logcat:-
11-20 13:40:25.855: E/AndroidRuntime(849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.medepad.community_virtual_ward/com.medepad.community_virtual_ward.Temperature}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
the activities are called main welcome and temperature.
code for main
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="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"
tools:context=".MainActivity"
android:background="#drawable/main" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="400dp"
android:layout_marginTop="115dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="40dp" />
</RelativeLayout>
java code
package com.medepad.community_virtual_ward;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
int a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView start = (TextView)findViewById(R.id.textView1);
start.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent welcome= new Intent (this,Welcome.class);
startActivity(welcome);
}
}
code for temperature
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/temperature" >
</LinearLayout>
java code
package com.medepad.community_virtual_ward;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Temperature extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.temperature);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
welcome xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/welcome" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="400dp"
android:layout_marginTop="115dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="40dp" />
</RelativeLayout>
java code
package com.medepad.community_virtual_ward;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class Welcome extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
TextView next= (TextView)findViewById(R.id.textView2);
next.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent temperature= new Intent(this, Temperature.class);
startActivity(temperature);
}
}
why am i getting this error and what can i do to use the amount of backgrounds i want?