I'm doing the Google Android App Course, and I encountered a weird error. If I call initVariables(), the layout is not appearing, if I comment it out, it appears. If I return after calling getResources(), I see the layout, if I return after calling getString(), no layout.
in the logcat i only see is some idle timeout reached.
i currently having some problems with my environment (eclipse crash and such), so i can't post a full logcat.
This is the code and the xml:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.advanced);
initLayout();
initVariables();
}
protected void initLayout() {
m_vwJokeButton = (Button) findViewById(R.id.addJokeButton);
m_vwJokeEditText = (EditText) findViewById(R.id.newJokeEditText);
m_vwJokeLayout = (LinearLayout) findViewById(R.id.jokeListLayout);
}
protected void initVariables() {
Resources resources = getResources();
author = resources.getString(R.string.author_name);
m_nDarkColor = resources.getColor(R.color.dark);
m_nLightColor = resources.getColor(R.color.light);
m_arrJokeList = new ArrayList<Joke>();
}
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/InputLayoutViewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/addJokeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Joke" />
<EditText
android:id="#+id/newJokeEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
<ScrollView
android:id="#+id/jokeListViewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/jokeListLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</ScrollView>
</LinearLayout>
If R.string.author_name isn't found at runtime, I'll be throwing an exception. You could catch the exception
try {
author = resources.getString(R.string.author_name);
} catch (Resources.NotFoundException e) {
}
but the question should really be why is your R.string.author_name missing? Are you able to successfully get any string resources?
Related
I'm trying to create an activity where I have a GridLayout that is populated dynamically:
Here's the xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="GoalsActivity" >
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/goals_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="2"
android:orientation="horizontal"
android:layout_gravity="center" >
</GridLayout>
</ScrollView>
</LinearLayout>
And here's the activity:
public class GoalsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goals);
try {
JSONArray goals = new JSONArray(getIntent().getStringExtra("GOAL_LIST"));
GridLayout goalsGrid = (GridLayout) findViewById(R.id.goals_grid);
for(int i = 0; i <= goals.length(); i++) {
JSONObject goal = goals.getJSONObject(i);
TextView goalView = new TextView(GoalsActivity.this);
goalView.setHeight(125);
goalView.setWidth(125);
goalView.setText(goal.getString("name"));
goalsGrid.addView(goalView);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
As you can see I'm trying to display the goals using a GridLayout but whenever I run this code, the following happens:
Apparently all TextViews are stacking on top of each other, but if I add 2 of these:
<TextView
android:height="125dp"
android:width="125dp" />
under the GridLayout in my xml layout manually, they show up just like I need them to:
I can't figure out how to fix this, any help is greatly appreciated!
I created a list of checkboxes, and I was trying to increase an int value proportionally to the number of the selected ones. Instead, every time I try to check them (through .isChecked()) the app crashes. I've tried to search everywhere for some solutions, but nothing worked. I really can't understand where is the problem.
Here's the code
XML file:
<?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"
tools:context="com.example.deadlybanana.myfirstapp.DisplayPicture">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp">
<LinearLayout
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/unicorn" />
<TextView
android:id="#+id/frasenome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<CheckBox
android:id="#+id/checkbox1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Beatiful?" />
<CheckBox
android:id="#+id/checkbox2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Fabolous?" />
<CheckBox
android:id="#+id/checkbox3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Unique?" />
<CheckBox
android:id="#+id/checkbox4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you a horse?" />
<CheckBox
android:id="#+id/checkbox5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Do you eat grass?" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="checkbutton"
android:text="Check if you are a unicorn" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Java file
package com.example.deadlybanana.myfirstapp;
public class DisplayPicture extends AppCompatActivity {
List<Boolean> checkboxes = new ArrayList<Boolean>();
CheckBox press1;
CheckBox press2;
CheckBox press3;
CheckBox press4;
CheckBox press5;
int points = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_picture);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1);
CheckBox press2 = (CheckBox)findViewById(R.id.checkbox2);
CheckBox press3 = (CheckBox)findViewById(R.id.checkbox3);
CheckBox press4 = (CheckBox)findViewById(R.id.checkbox4);
CheckBox press5 = (CheckBox)findViewById(R.id.checkbox5);
TextView NameString = (TextView) findViewById(R.id.frasenome);
String phrase = name + ", are you some of these things?";
NameString.setText(phrase);
}
public void checkbutton(View view){
if (press1.isChecked()){
points += 4;
}
if (points > 3){
Toast.makeText(this, "You are a unicorn", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(this, "You are not a unicorn", Toast.LENGTH_LONG).show();
}
}
}
(right now it is being examined just the first checkbox to simplify the code)
Try this: in onCreate()
press1 = (CheckBox)findViewById(R.id.checkbox1);
press2 = (CheckBox)findViewById(R.id.checkbox2);
press3 = (CheckBox)findViewById(R.id.checkbox3);
press4 = (CheckBox)findViewById(R.id.checkbox4);
press5 = (CheckBox)findViewById(R.id.checkbox5);
error occurs as press1.isChecked() is refers to the global CheckBox press1;
which is not yet initialized..
AS you are initializing the checkbox like:
CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1); inside onCreate() which only gives it a local instance which is not accessable in your checkbutton(View view) function
Solution
remove the local initialization just use: press1 = (CheckBox)findViewById(R.id.checkbox1);
Set id for your button lets assume it be button and add the following code
Button btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
checkbutton();
}
});
Note:remove the view parameter
Implementing Listeners is the best way to handle the events
I never use the method isChecked() because it never works for me. Do you want to increase the points when you select a checkbutton and maybe show the points when you press a button?
Here is the class code:
public class ButtonActivity extends AppCompatActivity
{
int points=0;
boolean check1=false;
boolean check2=false;
boolean check3=false;
boolean check4=false;
boolean check5=false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),points+"",Toast.LENGTH_SHORT).show();
}
});
}
public void onClick(View view)
{
switch(view.getId())
{
case R.id.checkBox:
if(check1)
{
check1=false;
points--;
}
else
{
check1=true;
points++;
}
break;
case R.id.checkBox2:
if(check2)
{
check2=false;
points--;
}
else
{
check2=true;
points++;
}
break;
case R.id.checkBox3:
if(check3)
{
check3=false;
points--;
}
else
{
check3=true;
points++;
}
break;
case R.id.checkBox4:
if(check4)
{
check4=false;
points--;
}
else
{
check4=true;
points++;
}
break;
case R.id.checkBox5:
if(check5)
{
check5=false;
points--;
}
else
{
check5=true;
points++;
}
break;
}
}
}
Here is the layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_button"
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:orientation="vertical"
tools:context="com.example.utente.stackoverflow.ButtonActivity">
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox2"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox3"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox4"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox5"
android:onClick="onClick"/>
<Button
android:text="Points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" />
I tested it and it works. I hope this helps you.
I am stuck at this Issue since couple of days. What I want is only part of the camera to be shown on screen. This is what I want:
This is what I have done:
<?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="200dp">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="160dp"
android:scrollbars="none"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/linearLayoutBeautyContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/surfaceViewBeautyCamera"/>
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/scanText"
android:text="Scanning..."
android:layout_height="wrap_content"
android:layout_width="match_parent">
</TextView>
</LinearLayout>
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LayoutInflater inflator = getLayoutInflater();
final LinearLayout inflate = (LinearLayout) inflator.inflate(R.layout.main, null);
final SurfaceView surfaceView = (SurfaceView) inflate.findViewById(R.id.surfaceViewBeautyCamera);
final SurfaceHolder holder = surfaceView.getHolder();
WindowManager mW = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int screenWidth = mW.getDefaultDisplay().getWidth();
int screenHeight = mW.getDefaultDisplay().getHeight();
final ViewGroup.LayoutParams slayout = surfaceView.getLayoutParams();
slayout.width = screenWidth;
slayout.height = screenHeight;
surfaceView.requestLayout();
final ScrollView beautyContent = (ScrollView) inflate.findViewById(R.id.scrollView);
holder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = getCamera();
camera.setPreviewDisplay(holder);
camera.setDisplayOrientation(90);
camera.startPreview();
} catch (IOException e) {
Log.i("Lucy", e.getMessage());
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
addContentView(inflate, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
public Camera getCamera() {
try {
final Camera open = Camera.open();
return open;
} catch (Exception ex) {
Log.i("Lucy", ex.getMessage());
return null;
}
}
When i do this I get:
The Idea is to set the size of SurfaceView as full screen but reduce the size of scroll view. But what I want is the first Image. What am I doing wrong?
Observations:
When I scroll the view up completely. Go back to Home Screen and open the app again. i get the first image. But if I scroll again and go back to main to come back, i get the second Image.
Any other strategy?
PS:
1. Having other view on top of camera is also option, but I cannot use it for lengthy reasons
The problem is the height settings for your elements:
Your root element should be a relative layout, you can then set the fixed height of the camera view and set the textview with the text to height match parent and layout_below the scroll view.
try this layout.But don't understand why use scrollView.
I post 2 layout
First one where surface mean camera face is 160dp
<?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"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayoutBeautyContent"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:orientation="vertical" >
<SurfaceView
android:id="#+id/surfaceViewBeautyCamera"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<TextView
android:id="#+id/scanText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/linearLayoutBeautyContent"
android:background="#000000"
android:text="Scanning..." >
</TextView>
</RelativeLayout>
2nd one is, camera will capture fullscreen size picture but u can see only 160 window at time of capture
<?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"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayoutBeautyContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<SurfaceView
android:id="#+id/surfaceViewBeautyCamera"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<TextView
android:id="#+id/scanText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_marginTop="160dp"
android:background="#000000"
android:text="Scanning..." >
</TextView>
</RelativeLayout>
I dont know why you put surfaceView in the scrollView But I put up with your layout file and its works for me as you show in expected snapshot result .
Edits:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/linearLayoutBeautyContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<SurfaceView
android:id="#+id/surfaceViewBeautyCamera"
android:layout_width="match_parent"
android:layout_height="160dp" />
<TextView
android:id="#+id/scanText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Scanning..." >
</TextView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</LinearLayout>
I want to have a dialog with buttons
whenever i click on a specific button, I want the dialog to "flip" and show another layout. A click on another button will return to the original dialog's view.
I try to use ViewFlipper as follows:
xmls:
send_feedback_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/feedback" />
</LinearLayout>
...
social_actions_dialog.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/main_activity_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" >
<ViewFlipper
android:id="#+id/viewFlipper"
android:layout_width="300dp"
android:layout_height="407dp"/>
<RelativeLayout
android:id="#+id/main_activity_card_face"
android:layout_width="300dp"
android:layout_height="407dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#android:color/white"
android:clickable="true"
android:onClick="onCardClick"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
...
code:
final ViewFlipper viewFlipper = (ViewFlipper) mDialog
.findViewById(R.id.viewFlipper);
View feedbackview = View.inflate(mContext,
R.layout.send_feedback_dialog, viewFlipper);
// ((ViewGroup)
// feedbackview.getParent()).removeView(feedbackview);
// viewFlipper.addView(feedbackview);
// View socialActions = View.inflate(mContext,
// R.layout.social_actions_dialog, viewFlipper);
// ((ViewGroup) socialActions.getParent())
// .removeView(socialActions);
// viewFlipper.addView(socialActions);
private void flipDialog(ViewFlipper viewFlipper,
boolean isSocialActionsShown, AlphaAnimation alphaIn,
AlphaAnimation alphaOut) {
if (isSocialActionsShown) {
isSocialActionsShown = false;
viewFlipper.setInAnimation(alphaIn);
viewFlipper.setOutAnimation(alphaOut);
// Show the next Screen
viewFlipper.showNext();
} else {
isSocialActionsShown = true;
viewFlipper.setInAnimation(alphaIn);
viewFlipper.setOutAnimation(alphaOut);
viewFlipper.showPrevious();
}
}
I used to get java.lang.StackOverflowError and then i have commented some rows and now the flip is executed but nothing changes.
how can i make it properly work?
what is the right way to organize the dialog layout in different files?
i have tried to create the dialog in a different file but it requires many things and maybe it's better to use it as an inner class
I want to have a slider that is open when the app starts. It will be open with buttons and such and when the user closes it, there will be more buttons to access. Is this possible with a sliding drawer? What would I add to the onCreate() method?
Thanks
XML Layout - In a basic LinearLayout:
<SlidingDrawer
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:id="#id/handle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/handleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tray_expand" />
<Button
android:id="#+id/handleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:text="Up me" />
</LinearLayout>
<LinearLayout
android:id="#+id/content"
android:paddingTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#013E53"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_commentDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textSize="20dp" />
</LinearLayout>
</SlidingDrawer>
And your Activity will looks like this:
public class Home extends Activity implements OnDrawerScrollListener
{
private ImageView handleImage;
private Button handleButton;
private SlidingDrawer slide;
private TextView tv_commentDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
tv_commentDisplay = (TextView)this.findViewById(R.id.tv_commentDisplay);
handleImage = (ImageView)this.findViewById(R.id.handleImage);
handleButton = (Button)this.findViewById(R.id.handleButton);
slide = (SlidingDrawer)this.findViewById(R.id.slide);
slide.open(); // not sure
slide.setOnDrawerScrollListener(this);
handleButton = ((Button)this.findViewById(R.id.handleButton));
tv_commentDisplay.setText("Hello World");
}
#Override
public void onScrollEnded() {
}
#Override
public void onScrollStarted() {
if (slide.isOpened())
handleImage.setImageResource(R.drawable.ic_tray_collapse);
else {
handleImage.setImageResource(R.drawable.ic_tray_expand);
}
}
Use open() in your onCreate(), it will open the drawer immediately.
You can take a look at the full API here