Android Dialog cropping problem on Android 12 devices - java

We have a number of Apps on the market that use DialogFragment (the AppCompatDialogFragment version). We are finding that on certain devices, Samsung TAB S7 for one, dialogs are truncated so that information and action buttons at the bottom are not displayed. Nothing I have tried will resolve this issue in a way that is consistent across all devices. I have produces a simple layout and code to demonstrate. This code when inserted in to the live app displays as per the truncated image, in a newly created test app it displays normally on the same device! All theming, manifest settings appear similar. I have spent many days on this so any suggestions would be much appreciated.
package com.example.dialogclipping;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.DialogFragment;
public class DlgTest extends AppCompatDialogFragment {
static DlgTest newInstance() {
return new DlgTest();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
}
#Override
public void onStart() {
super.onStart();
int targetWidth = 1000;
int targetHeight = 0;
// check if specific size setting is required
if (targetWidth != 0 || targetHeight != 0) {
var dialog = getDialog();
if (dialog == null) return;
var window = dialog.getWindow();
if (window == null) return;
var lp = window.getAttributes();
// overwrite dimensions as appropriate
if (targetWidth > 0) lp.width = targetWidth;
if (targetHeight > 0) lp.height = targetHeight;
window.setAttributes(lp);
}
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dlg_test_layout, container, false);
return view;
}
int index = 2;
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(0xFF92580C);
final var closeButton = view.findViewById(R.id.yesButton);
closeButton.setOnClickListener(v -> {
/*
var message = "Height is " + view.getHeight() + "; bottom view at " + bottomField.getBottom();
legend.setText(message);
var afterField = view.findViewById(fields[index]);
afterField.setVisibility(View.VISIBLE);
index = (index + 1) % fields.length;
*/
//dismiss();
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/SettingsDialog"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/header"
style="#style/DialogText.Heading"
android:minEms="16"
android:layout_gravity="left"
android:text="Header line"/>
<View
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#color/blue"/>
<View
android:id="#+id/middleView"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginTop="10dp"
android:background="#color/black">
</View>
<LinearLayout
android:id="#+id/ButtonBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="right">
<Button
android:id="#+id/yesButton"
style="#style/DialogButton.Large"
android:textSize="20dp"
android:layout_width="0dip"
android:layout_weight="2"
android:text="Close"
android:visibility="visible"/>
</LinearLayout>
<View
android:id="#+id/bottomView"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#color/blue">
</View>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.dialogclipping">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="false"
android:xlargeScreens="true"/>
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:resizeableActivity="false"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppBaseTheme"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.NoActionBar">
</style>
<style name="DialogStyle" parent="Theme.AppCompat.Dialog">
</style>

I think the problem is in onStart() method.
Can you try to change like following?
#Override
public void onStart() {
super.onStart();
Window win = getDialog().getWindow();
if (win == null)
return;
DisplayMetrics dm = new DisplayMetrics();
((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
WindowManager.LayoutParams params = win.getAttributes();
int contextRect = getContextRect((Activity) mContext);
params.gravity = Gravity.CENTER; //you can change Gravity in here
params.width = ViewGroup.LayoutParams.MATCH_PARENT; //you can change width in here
int screenHeight = contextRect == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : contextRect;
params.height = screenHeight;
win.setAttributes(params);
}
private int getContextRect(Activity activity) {
Rect outRect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
return outRect.height();
}

this may help
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.FullscreenDialogStyle)
dialog?.let {
it.window?.setLayout(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT
)
}
}
<style name="FullscreenDialogStyle">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

This problem has been resolved by an update by Samsung! I have no idea what the underlying problem was but it no longer is an issue. Thanks to all those who put some thought into it.

Related

How to change the image's coordinates by using java

Goal:
When click on the button, I would like to change the picture's X and Y coordinate to
android:layout_x 35dp android:layout_y: 541dp by using java code and not XML code.
Questions:
I cannot find the correct syntax code in order do it.
Do you know how to do it?
Thank you!
Info:
*I'm newbie in android
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="158dp"
android:layout_y="77dp"
android:src="#drawable/cat" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="testtest"
android:text="Button" />
</AbsoluteLayout>
package com.jfdimarzio.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView myImageView = (ImageView) findViewById(R.id.imageView2);
myImageView .setImageResource(R.drawable.cat);
}
public void testtest(View view)
{
AbsoluteLayout relativeLayout = new AbsoluteLayout(this);
ImageView myImageView = (ImageView) findViewById(R.id.imageView2);
myImageView.layout(10,10,10,10);
//myImageView.layout(3,3,3,3);
}
}
Haven't seen AbsoluteLayout for years o.o
In Android, a View's location isn't actually specified by either the View itself or the ViewGroup that contains the View. Instead, it's specified by the LayoutParams that accompanies the View.
In your xml file, when creating your View, the width, height, margins, and the x and y values are all within a LayoutParams object that is automatically created and set onto the View.
The LayoutParams is the component that tells the container how it should position the View.
So if you want to update the x and y of your ImageView, you'll need to edit from it's LayoutParams.
ImageView myImageView = (ImageView) findViewById(R.id.imageView2);
AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) myImageView.getLayoutParams();
params.x = NEW_X_VALUE; //Please enter this yourself
params.y = NEW_Y_VALUE; //Be careful of DP to PX conversions
myImageView.setLayoutParams(params);
myImageView.requestLayout();
So what I'm doing here is fetching the LayoutParams that was created through the xml and changing the x and y values within it. Afterwards, I set the modified LayoutParams back into the ImageView.
I believe setLayoutParams() is already enough to cause the parent container to update the View's location, but just incase it doesn't... requestLayout() should.

Android Studio - Custom ListView Crashing

So, recently followed this video's instructions on how to make custom ListView: https://www.youtube.com/watch?v=D0or0X12FMM
The program is crashing on startup, its DOA and show's nothing before it crashes.
It's not logging any errors to the console.
Here's my code for all parts shown in video + mainifest:
I have a feeling it's a problem with the manifest. Just based on other posts. Thanks in advance!
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.doubl.my_application">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".ListTest">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_list_test.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:padding="16dp"
tools:context="com.example.doubl.my_application.ListTest">
<ListView
android:id="#+id/list1"
android:layout_width="312dp"
android:layout_height="519dp"
android:layout_alignParentTop="true"/>
</android.support.constraint.ConstraintLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:id="#+id/icon"
android:src="#mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#33CC33"
android:id="#+id/text1"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:text="Medium Text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text2"
android:layout_marginLeft="10dp"
android:text="TextView"
/>
</LinearLayout>
</LinearLayout>
ListTest.java
package com.example.doubl.my_application;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class ListTest extends AppCompatActivity {
ListView list;
String [] titles;
String [] description;
int[] imgs = {R.drawable.facebook, R.drawable.instagram, R.drawable.twitter, R.drawable.google};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_test);
Resources res = getResources();
titles = res.getStringArray(R.array.titles);
description = res.getStringArray(R.array.description);
list = (ListView) findViewById(R.id.list1);
MyAdapter adapter = new MyAdapter(this, titles, imgs, description);
list.setAdapter(adapter);
}
class MyAdapter extends ArrayAdapter<String>{
Context context;
int[] imgss;
String myTitles[];
String myDescription[];
MyAdapter(Context c, String []titles, int[] img, String[]description){
super(c, R.layout.row, R.id.text1, titles);
this.context = c;
this.imgss = img;
this.myDescription = description;
this.myTitles = titles;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.row,parent,false);
ImageView images = (ImageView) findViewById(R.id.icon);
TextView myTitle = (TextView) findViewById(R.id.text1);
TextView myDescription = (TextView) findViewById(R.id.text2);
images.setImageResource(R.drawable.facebook);
images.setImageResource(imgs[position]);
myTitle.setText(titles[position]);
myDescription.setText(description[position]);
return row;
}
}
}
Just Goto :
Android Studio --> File --> Setting --> Build, execution, deploy --> Instant run.
and disable instant run .

Android SeekBar Application - Unfortunately, project has stopped

I am new to Android dev and for my project, I am creating two seekbars. The first seekbar has a minimum of 50 and a maximum of 180 while the second seekbar has a minimum of 180 and a maximum of 400. I also added textview labels below each seekbar to show the current value of each. My code compiled fine without any errors and displayed fine on the Android emulator, but when I clicked on the slider to change the value of the seekbar, my app ended up crashing and saying, "Unfortunately, your project has stopped." Below is my code:
MainActivity.java
package com.example.myname.projectname;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.R.*;
public class MainActivity extends ActionBarActivity implements OnSeekBarChangeListener {
private SeekBar lowBar;
private SeekBar highBar;
private TextView lowtext;
private TextView hightext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializevariables();
lowBar = (SeekBar)findViewById(R.id.lowbar);
highBar = (SeekBar)findViewById(R.id.highbar);
lowtext = (TextView)findViewById(R.id.lowval);
hightext = (TextView)findViewById(R.id.highval);
lowBar.setOnSeekBarChangeListener(this);
highBar.setOnSeekBarChangeListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void initializevariables(){
lowBar = (SeekBar) findViewById(R.id.lowbar);
highBar = (SeekBar) findViewById(R.id.highbar);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progress = 0;
int lowbarmin = progress + 50;
int highbarmin = progress + 180;
lowtext.setText(lowbarmin);
hightext.setText(highbarmin);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
activity_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: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=".MainActivity">
<EditText android:text="CGM Alarm System" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lowbar"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:indeterminate="false"
android:progress="0"
android:max="180"/>
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/highbar"
android:layout_below="#+id/lowbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="66dp"
android:layout_alignRight="#+id/lowbar"
android:layout_alignEnd="#+id/lowbar"
android:progress="0"
android:max="400"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="50"
android:id="#+id/lowval"
android:layout_below="#+id/lowbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="180"
android:id="#+id/highval"
android:layout_below="#+id/highbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myname.projectname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myname.projectname.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you in advance!
Your seekbar's minimum value is 50, you can not increase +50;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
int lowbarmin = progress + 1;
int highbarmin = progress + 1;
lowtext.setText(lowbarmin);
hightext.setText(highbarmin);
}
Crash is probably because of these lines :
lowtext.setText(lowbarmin);
hightext.setText(highbarmin);
When you do this and the value of lowbarmin and highbarmin are integers, android looks for string resources with those values as their resourceId and it cannot find one.
So what you instead want to do is, pass string values to setText() :
lowtext.setText(Integer.toString(lowbarmin));
hightext.setText(Integer.toString(highbarmin));

Set image in app as wallpaper

I am not able to set an image from the app as wallpaper. On running it shows unfortunately testandroid stopped working.
I have set the permission in the manifest file and set minimum SDK to 5.
Androidmanifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="19" />
<uses-permission android:maxSdkVersion="19" android:name="android.permission.SET_WALLPAPER"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.testandroid.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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="com.example.testandroid.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ImageView
android:id="#+id/imageview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/bing"
android:adjustViewBounds="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="27dp"
android:text="#string/btn"
android:onClick="goForNext"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn1"
android:onClick="setWallpaper"/>
</LinearLayout>
MainActivity.java
package com.example.testandroid;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imgview = (ImageView) findViewById(R.id.imageview1);
imgview.setImageResource(R.drawable.bing);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void goForNext(View view){
Button but = (Button) findViewById(R.id.button1);
Intent i = new Intent(MainActivity.this, Activity.class);
startActivity(i);
}
public void setWallpaper(View view)
{
Button bt = (Button) findViewById(R.id.button2);
WallpaperManager wm = WallpaperManager.getInstance(getApplicationContext());
try
{
wm.setResource(R.drawable.bing);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
here is the activity_main.xml file. please help me wiht it.
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testandroid.MainActivity"
tools:ignore="MergeRootFrame" />
Try removing the following from onCreate()
ImageView imgview = (ImageView) findViewById(R.id.imageview1);
imgview.setImageResource(R.drawable.bing);
And add this to your onCreateView() method in your Fragment
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
ImageView imgview = (ImageView) rootView.findViewById(R.id.imageview1);
imgview.setImageResource(R.drawable.bing);
return rootView;

"Unfortunately app has stopped" error?

I'm trying to write a basic drawing Android app for android and test it on Genymotion, but I can't click on the app icon (it says "Unfortunately [app] has stopped.")
I'm not sure if the three private variables should be under the MainActivity class? Or that it has to do with my AndroidManifest.xml file. Can someone look over my code?
My MainActivity.java file:
package edu.berkeley.cs160.opalkale.prog2;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private Paint myPaint;
private ImageButton currPaint;
private myView drawView;
public void paintClicked(View view) {
if (view != currPaint) {
// update color
ImageButton imgView = (ImageButton) view;
String color = view.getTag().toString();
drawView.setColor(color);
imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
currPaint = (ImageButton)view;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView mV = new myView(this, null);
setContentView(mV);
drawView = (myView) findViewById(R.id.drawing);
LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);
currPaint = (ImageButton) paintLayout.getChildAt(0);
currPaint.setImageDrawable(getResources().getDrawable(
R.drawable.paint_pressed));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
My AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.berkeley.cs160.opalkale.prog2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="edu.berkeley.cs160.opalkale.prog2.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat:
[2014-02-11 23:04:43 - DeviceMonitor] Adb connection Error:EOF
[2014-02-11 23:04:43 - DeviceMonitor] Sending jdwp tracking request failed!
[2014-02-11 23:04:43 - DeviceMonitor] Connection attempts: 1
[2014-02-11 23:04:44 - DeviceMonitor] Connection attempts: 2
[2014-02-11 23:04:45 - DeviceMonitor] Connection attempts: 3
[2014-02-11 23:04:46 - DeviceMonitor] Connection attempts: 4
[2014-02-11 23:04:47 - DeviceMonitor] Connection attempts: 5
[2014-02-11 23:04:48 - DeviceMonitor] Connection attempts: 6
[2014-02-11 23:04:49 - DeviceMonitor] Connection attempts: 7
[2014-02-11 23:04:50 - DeviceMonitor] Connection attempts: 8
[2014-02-11 23:04:51 - DeviceMonitor] Connection attempts: 9
[2014-02-11 23:04:52 - DeviceMonitor] Connection attempts: 10
[2014-02-11 23:04:53 - DeviceMonitor] Connection attempts: 11
[2014-02-11 23:07:14 - Logcat] device not found
com.android.ddmlib.AdbCommandRejectedException: device not found
at com.android.ddmlib.AdbHelper.setDevice(AdbHelper.java:774)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:396)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:347)
at com.android.ddmlib.Device.executeShellCommand(Device.java:444)
at com.android.ddmuilib.logcat.LogPanel$3.run(LogPanel.java:531)
activity_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"
android:screenOrientation="portrait"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCCCCCC"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/new_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/brush"
android:src="#drawable/brush" />
</LinearLayout>
<edu.berkeley.cs160.opalkale.prog2.myView
android:id="#+id/drawing"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FFFFFFFF" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/paint_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF787878"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/paint"
android:tag="#FF787878" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
myView class:
package edu.berkeley.cs160.opalkale.prog2;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class myView extends View {
private Path drawPath;
private Paint drawPaint, canvasPaint;
private int paintColor = 0xFF787878;
private Canvas drawCanvas;
private Bitmap canvasBitmap;
public void setColor(String newColor){
//set color
invalidate();
paintColor = Color.parseColor(newColor);
drawPaint.setColor(paintColor);
}
public myView(Context context, AttributeSet attrs) {
super(context, attrs);
setupDrawing();
}
private void setupDrawing() {
drawPath = new Path();
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(20);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// view given size
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
#Override
protected void onDraw(Canvas canvas) {
// draw view
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// detect user touch
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;
default:
return false;
}
invalidate();
return true;
}
}
setContentView(Id); is missing in your code.So it is giving you NullPointerException
Set some of the view to your Activity like
setContentView(R.layout.layout);
It is just a common problem with manifest your program "Unfortunately app has stopped" may be caused due to Manifest file, Because you have two classes:
So use this:
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".myView"
android:label="#string/app_name">
</activity>
</application>
NPE- Null Pointer Exception. No call to setContentView.
Also,
drawView = (myView) findViewById(R.id.drawing);
what is your myView ?
Please set myView as the view for your activity or call setContentView with the layout where R.id.drawing is.
Where is your layout.Your layout is missing. Use setContentView(R.layout.yourlayout);
Just remove comments from this line ,
// setContentView(mV);

Categories