How to set Image fit to screen as wallpaper in devices? - java

I want to Set Image viewed in app as wallpaper.
While trying to do so image doesn't fit to screen of my device but its viewed properly using centerCrop.
This is my Code for the app.
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
app:srcCompat="#drawable/image" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#color/transparent"
android:text="Set Wallpaper"
android:textColor="#ffffff" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setWallpaper();
}
});
}
private void setWallpaper(){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
try {
manager.setBitmap(bitmap);
Toast.makeText(this, "Wallpaper Set", Toast.LENGTH_SHORT).show();
}
catch (IOException e){
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
}
This is image used in App
Live App Screen
After Setting Wallpaper in android Device
How can I set image as wallpaper perfectly as used in XML file (scale to centerCrop) for setting it as perfect fit wallpaper for any android devices?

Use:
android:background="#drawable/image"
instead of:
app:srcCompat="#drawable/image"

Related

Android start Activity 2 but rest at MainActivity

I have MainActivity which has got a button that opens Activity 2. Everything from Activity 2 should be calculated and be run but the user should rest at MainActivity? How do I do this?
I found a solution where I dont run the "setContentView()" but then my app crashes.
You want to change the view of application but does not want the user to change activity.
Use two different layout in xml file with id :
<?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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/first_view">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="#+id/button"
android:background="#358a32" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/second_view"
android:visibility="gone">
</LinearLayout>
<LinearLayout>
Then onclick of button :
public class MainActivity extends Activity {
boolean firstViewOff = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
LinearLayout first_view = (LinearLayout) findViewById(R.id.first_view);
LinearLayout second_view = (LinearLayout) findViewById(R.id.second_view);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
firstViewOff = true;
first_view.setVisibility(View.GONE);
second_view.setVisibility(View.VISIBLE);
}
});
}
#override
public void onBackPressed(){
super.onBackPressed();
if(firstViewOff){
second_view.setVisibility(View.GONE);
first_view.setVisibility(View.VISIBLE);
firstViewOff = false;
}
}
}
Why i attached backpressed :
Because when user backpress then it will just directly show firstview without closing,

display image in a splash screen

I am a novice android developer. I am trying to create a splash screen. I have a png image.
I have created an empty splash screen with a text box.I have 2 activities - MainActivity and SecondActivity.
code:
public class MainActivity extends AppCompatActivity {
private static int SPLASH_SCREEN_TIME_OUT=2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i=new Intent(MainActivity.this,
SecondActivity.class);
//Intent is used to switch from one activity to another.
startActivity(i);
//invoke the SecondActivity.
finish();
//the current activity will get finished.
}
}, SPLASH_SCREEN_TIME_OUT);
}
}
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = 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();
}
});
}
}
activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView"
android:layout_width="250dp"
android:layout_height="200dp"
app:srcCompat="#drawable/geeks"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.447" />
</androidx.constraintlayout.widget.ConstraintLayout>
But app:srcCompat="#drawable/geeks" is in red color. Means it is not resolved. Where shall I copy the png image for the splash screen? I have not copied the image anywhere in android studio. Without the image view, the splash screen runs fine showing hello world.
Few things to keep in mind.
The image size should be low (not more than 150-200kb)
Instead of adding text and image on the activity, design a splash screen using Illustrator or any other medium and save it in png format.
Store this in the res/drawable.
In the activity_main, just add line android:background ="#drawable/geeks".
In AndroidManifest.xml, add, android:theme="#style/Theme.<app name>.NoActionBar"
first you need to put your image in res/drawable folder:
and then access it by app:srcCompat or android:src attribute of ImageView inside your activity_main.xml:
app:srcCompat="#drawable/company_logo"
android:src="#drawable/company_logo"
Try writing android : src ="#drawable/geeks", instead.
put them in assets folder. It'll resolve your problem.

I am trying display the pause icon while playing the sound and then display the play icon when the sound finish playing

here I'm trying to show the pause icon when media is playing and I want to automatically change to play icon after the media finish playing. The first time whenever I click on the play icon it would change to pause icon but it wouldn't change back to play icon after the media finish playing.
public class MainActivity extends AppCompatActivity {
ImageButton playBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (ImageButton) findViewById(R.id.button1);
final MediaPlayer sound = MediaPlayer.create(this,R.raw.evumm);
playBtn.setBackgroundResource(R.drawable.playicon);
playBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (sound.isPlaying()) {
sound.pause();
playBtn.setBackgroundResource(R.drawable.playicon);
} else {
sound.start();
playBtn.setBackgroundResource(R.drawable.pauseicon);
}
}
});
}
}
XML code
<?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"
tools:context="develop.kokoson.playorstopapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play or Pause !"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:id="#+id/textView" />
<ImageButton
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/playicon"
android:layout_marginTop="81dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You could use the OnCompletionListener in MediaPlayer to do this. Just add a listener that sets the button icon back to the play icon when the sound is complete.
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
playBtn.setBackgroundResource(R.drawable.playicon);
}
});

How to create custom progressDialog

At any devices we have different position of progressBar inside of the progressDialog. So I need to customize it.
When I try like this:
public class CustomProgressDialog extends ProgressDialog {
public CustomProgressDialog(Context context) {
super(context);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_fragment_dialog);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_gravity="center"
android:background="#color/colorPrimary"
android:layout_height="match_parent"
android:id="#+id/progressBar" />
</LinearLayout>
This progress dialog shows only progressBar without dialog. But I need dialog too.
Create custom dialog with progress bar in it for making it consistent for different devices.
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - title, ProgressBar and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("COUSTOM PROGRESS TITLE");
ProgressBar prog = (ProgressBar) dialog.findViewById(R.id.myprogress);
prog.setVisibilty(VISIBLE);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
layout:
<?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" >
<ProgressBar
android:id="#+id/myprogress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/image"/>/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/myprogress"
/>
</RelativeLayout>

How to change ImageView source from different XML?

I want to change images in my popupwindow. I am trying to change ImageView image source which is in test.xml while my contentView is activity_main. When I try to just call imageView.setImageResource(android.R.drawable.ic_menu_help); it gives me nullpointerexception I realized i have to use inflate method, but when I use it it doesn't do what I want.I know there is a lot of questions regarding this, but none of them really helped me.
This is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.popupwindow.MainActivity">
<Button
android:text="Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
test.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:id="#+id/test1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear1">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#android:drawable/btn_star_big_on"
android:id="#+id/imageView1"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
my main.java:
public class MainActivity extends AppCompatActivity {
private PopupWindow popupWindow;
private RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test = (Button) findViewById(R.id.button);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
final View view1 = getLayoutInflater().inflate(R.layout.test, null);
ImageView imageView = (ImageView) view1.findViewById(R.id.imageView1);
imageView.setImageResource(android.R.drawable.ic_menu_help); //Image source is not changed in my popupWindow,
// but it is changed when I call relativeLayout.addView(view1);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//relativeLayout.addView(view1); // <-- this does the trick, but its not what i want
View container = getLayoutInflater().inflate(R.layout.test, null);
popupWindow = new PopupWindow(container, android.app.ActionBar.LayoutParams.MATCH_PARENT, android.app.ActionBar.LayoutParams.WRAP_CONTENT, true);
popupWindow.showAtLocation(relativeLayout, Gravity.BOTTOM, 0, 0);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popupWindow.dismiss();
return true;
}
});
}
});
}
}
You're inflating two separate views. Obviously what you do in one will have no impact on the other. You should set the popup view to your originally inflated view:
popupWindow = new PopupWindow(view1,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT,
true);

Categories

Resources