Android Studio emulator opens but black screen - android

I'm new to programming. I've only learned the basics. Right now, I'm trying Android development out with Android Studio. My program simply has a title, and 2 buttons that are supposed to open a URL. There doesn't seem to be anything wrong with my code since nothing is underlined in red, so I don't understand why the layout model isn't showing up in the emulator.
What are some things that affects the emulator from running properly? I tried it on a windows computer and the emulator came after a minute or so. But nothing for the Mac. Anyway to speed up the process?
My code has no errors. Only problem I'm facing is the emulator not running in a timely manner. The app is supposed to open a URL for each of the 2 buttons in the browser.
Running on a Macbook Pro Mid 2010 13" with 8 GB RAM
**************************************Activity Code**********************************************
package com.first.bharg.firstapp;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOnClick();
}
public void buttonOnClick(){
Button b = (Button) findViewById(R.id.button);
Button b1 = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i1 =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com"));
startActivity(i1);
}
});
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i2 =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i2);
}
});
}
#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);
}
}
************************************************Layout******************************************
<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"
android:id="#+id/activity_main"
android:clickable="true"
android:background="#ffffa157"
android:focusable="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yahoo"
android:id="#+id/button"
android:layout_marginTop="107dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="51dp"
android:layout_marginStart="51dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google"
android:id="#+id/button2"
android:layout_alignBottom="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="42dp"
android:layout_marginEnd="42dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="First App"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

As far as windows concerns, I am afraid there's nothing you can do but wait for the emulator to load everything up. It always takes a lot of time, unless you use your own device (i.e connect your mobile phone to your laptop via USB).
For Mac it's a bit tricky. It's obviously not the only way, but I do suggest you to try another emulator if the one you actually have it's not working properly on it. It's gonna be faster than trying to solve whatever that's going on.
Apparently your code is ok. However if you are new to android I suggest you first get familiar with android activities life cycle before using intents. Here you may find some information related.
I've personally used eclipse's android SDK and it works fine on both OS.

Related

android project not resolving and reseting androids studio doesn't work

I have a problem where files that should be showing up in my android project are not appearing. For example, in the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="bottom|left"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/pencil"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="left"
android:src="#drawable-mdpi/pencil"
/>
<ImageButton
android:id="#+id/eraser"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable-mdpi/eraser"
/>
</LinearLayout>
I get errors for the pencil and eraser src lines, despite having the two bitmap images in that exact folder (and being able to see it in my directory). Similar problems exist for this code:
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class DrawingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawing);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(res.menu.drawing, 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 == res.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Where it cannot find drawing.xml (at line with res.menu.drawing), even though again it is visible in the directory.
Is there something else I'm supposed to do first? Maybe some kind of import beyond putting the items in the folder and rebuilding? I've read in some other cases that these kinds of problems were resolved by exiting and re-opening Android Studio, but I've now done that twice with no effect (as well as multiple cleans and rebuilds of the project). As such, I can only assume that I'm missing something more essential.
Any help would be greatly appreciated.

Android Layout does not display controls

I am writing my first android app after reading a tutorial. I am excited and thought its easy. However, after adding a simple edit text and a hello world message, i run and was expecting to see something yet nothing showed up. Neither the textbox nor the default hello world. Pls where do go wrong ? Below is my layout (i.e 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="16dp"
android:paddingRight="16dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:textColor="#FF00FF00"/>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#FF0000FF"
android:id="#+id/et_Text" />
</RelativeLayout>
My java code is the default/code generated one. I was expecting to see atleast the message for a start yet didn't show any control. Just a screen with a phone dialers on the rigth hand.
package com.example.mytest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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);
}
}
Since you are using RelativeLayout, you need to tell Android how each control is relative to each other. Change the layout as follow:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:id="#+id/textView1"
android:textColor="#FF00FF00"/>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/textView1"
android:textColor="#FF0000FF"
android:id="#+id/et_Text" />
This will tell the system to place the EditText below the TextView.

Imageview gets blurry

This app is supposed to display images in which the students can count up what the money is worth in total, and then input the value in a editText text box, which is then compared against a stored value. Unfortunately, when I try to switch activities past a certain point (there are 11 active activities, with three of them displaying images fine), the images start blurring and the coins are hard to distinguish from each other. I do not know whether this is a Java or XML error, however I have pasted the code below. The following is XML code.
<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=".Ldsm" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit" />
</LinearLayout>
<TextView
android:id="#+id/userQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout"
android:layout_alignLeft="#+id/userAnswer"
android:layout_marginBottom="30dp"
android:text="#string/how_many_coins_total"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/userAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/userQuestion"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginTop="118dp" >
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/userQuestion"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/linearLayout"
android:src="#drawable/ic_coin4" />
That's just one of the 7 screens that look blurry when they display the images. The following is the Java code for the same activity.
package com.example.ldsm3;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.example.ldsm3.Problem5;
public class Problem4 extends Activity
{
private final int COIN3_SCREEN_ANSWER = 95;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_problem4);
Button submitButton = (Button)findViewById(R.id.submitButton);
submitButton.setOnClickListener(submitButtonListener);
}
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;
}
private OnClickListener submitButtonListener = new OnClickListener()
{
public void onClick(View arg0)
{
EditText editText = (EditText)findViewById(R.id.userAnswer);
int userAnswerValue = Integer.parseInt(editText.getText().toString());
// Build the Alert Dialog
android.app.AlertDialog.Builder alert = new AlertDialog.Builder(Problem4.this);
alert.setTitle("Answer");
alert.setCancelable(false);
if(userAnswerValue == COIN3_SCREEN_ANSWER)
{
alert.setMessage("Congratulations!!!");
}
else
{
alert.setMessage("Sorry, that's not right.");
}
alert.setPositiveButton("OK",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
Intent nextCoinScreenIntent = new Intent(Problem4.this, Problem5.class);
startActivity(nextCoinScreenIntent);
}
});
alert.show();
}
};
}
And this a screenshot when it is running on a Nexus 10:
Please let me know if any more information is needed.
It seems like it was fetching the icons that were not designed for the particular screen size. You can fix this by going to the workspace and deleting every icon that is any other folder besides the "drawable" one. (With the exception of the ic_launcher icon, as that is the one that allows you to display your app in the Android app menu and will not get blurry.)
Better than your solution would be to make sure the proper resolution file is in every folder. The proper resolution is your desired resolution with the following multipliers, as shown on the Android Design documents:

Enable or Disable buttons programmatically based on the active (i.e. visible) fragments

I am relatively new to Android programming and really need assistance with this problem I am trying to solve. I have an application that revolves around three base activities. In the one activity - the largest by far - I have ten buttons in the UI that implement 16 different fragments (each with their own UIs) in a logical (i.e. step-by-step) fashion depending on what the user wants to accomplish.
So, for each of these 16 different fragments I need to activate and deactivate (enable and disable) various buttons in the UI depending on what the user is allowed to do when a specific fragment is active (i.e. at the front of the view stack or in view or visible to the user). In actual fact, I need to change (i.e. set) the states (i.e. enabled state) of all 10 buttons everytime a new fragment is loaded into the fragment placeholder/container to give the user a clear idea of where they are in this logical process of steps.
Please note: all 10 buttons are visible (i.e. should be visible) at all times (always) and must only be enabled or disabled depending on the fragment that is currently loaded or currently in view (i.e. displayed/visible to the user). OK, so let's look at some code...
Here is the Activity "DmpAct.java" (complete code to date) that I was referring to above...
package com.carzy.carzyapp;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
public class DmpAct extends Activity implements OnClickListener {
Fragment fragment;
Fragment newFragment;
FragmentManager fragMan;
Button birtListBtn, evenListBtn, appoListBtn, todoListBtn, specListBtn, dmpExitBtn;
#SuppressLint({ "NewApi", "CommitTransaction" })
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the Title Bar of the Application --> Must come before setting the Layout...
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Hide the Status Bar of Android OS --> Can also be done later...
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Now you can draw the second Layout --> HomeScreen of the Application...
setContentView(R.layout.dmp_act);
// Instantiate the FragmentManager and FragmentTranstion...
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
// Now you need to define or set the initial/start fragment to be loaded when the view is laid out...
DMPWelcFrag startFragment = new DMPWelcFrag();
fragTrans.add(R.id.dmpFragContainer, startFragment);
fragTrans.commit();
// Instantiate (or get references to) all buttons laid out in this Activity
Button birtListBtn = (Button) findViewById(R.id.dmp_bir_btn);
birtListBtn.setOnClickListener(this);
Button evenListBtn = (Button) findViewById(R.id.dmp_eve_btn);
evenListBtn.setOnClickListener(this);
Button appoListBtn = (Button) findViewById(R.id.dmp_app_btn);
appoListBtn.setOnClickListener(this);
Button todoListBtn = (Button) findViewById(R.id.dmp_tod_btn);
todoListBtn.setOnClickListener(this);
Button specListBtn = (Button) findViewById(R.id.dmp_spe_btn);
specListBtn.setOnClickListener(this);
Button dmpExitBtn = (Button) findViewById(R.id.dmp_exi_btn);
dmpExitBtn.setOnClickListener(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.dmp, menu);
return true;
}
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// Fragment newFragment;
// Set the DMP Fragment state here and pass it on to the FragmentTransaction module that follows..
if (v.getId() == R.id.dmp_bir_btn) {
newFragment = new BirtListFrag();
}
else if (v.getId() == R.id.dmp_eve_btn) {
newFragment = new EvenListFrag();
}
else if (v.getId() == R.id.dmp_app_btn) {
newFragment = new AppoListFrag();
}
else if (v.getId() == R.id.dmp_tod_btn) {
newFragment = new TodoListFrag();
}
else if (v.getId() == R.id.dmp_spe_btn) {
newFragment = new SpecListFrag();
}
else {
newFragment = new DMPWelcFrag();
}
if (v.getId() == R.id.dmp_exi_btn) {
Intent go2Main = new Intent(DmpAct.this, MainAct.class);
startActivity(go2Main);
}
else;
FragmentTransaction transact = getFragmentManager().beginTransaction();
transact.replace(R.id.dmpFragContainer, newFragment, "activeFrag");
transact.addToBackStack("activeFrag");
primeRelativeBtns();
transact.commit();
};
#SuppressLint("NewApi")
public void primeRelativeBtns() {
if (newFragment.equals("dmpBirtListFragTag")) {
birtListBtn.setEnabled(false);
evenListBtn.setEnabled(true);
appoListBtn.setEnabled(true);
todoListBtn.setEnabled(true);
specListBtn.setEnabled(true);
}
else if (newFragment.getTag() == "dmpEvenListFragTag") {
birtListBtn.setEnabled(true);
evenListBtn.setEnabled(false);
appoListBtn.setEnabled(true);
todoListBtn.setEnabled(true);
specListBtn.setEnabled(true);
} else;
}
}
Please note...at the time of submitting this question I had only completed 6 of the 10 buttons used by this activity (as is obvious in the code above)...
Anyway...on we go...
Here is the UI that is implemented for this activity by the "dmp_act.xml" file...
<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:orientation="horizontal"
android:baselineAligned="false"
tools:context=".DmpAct">
<LinearLayout
android:id="#+id/leftButtonColumn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2705"
android:gravity="center"
android:paddingTop="5pt"
android:paddingBottom="10pt"
android:paddingLeft="8pt"
android:paddingRight="8pt"
android:orientation="vertical">
<ImageView
android:id="#+id/dmp_cat_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0180"
android:contentDescription="#string/dmp_cat_sign"/>
<Button
android:id="#+id/dmp_bir_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"
android:layout_marginBottom="13pt"/>
<Button
android:id="#+id/dmp_eve_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"
android:layout_marginBottom="13pt"/>
<Button
android:id="#+id/dmp_app_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"
android:layout_marginBottom="13pt"/>
<Button
android:id="#+id/dmp_tod_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"
android:layout_marginBottom="13pt"/>
<Button
android:id="#+id/dmp_spe_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dmpFragContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5350"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="8pt"
android:paddingBottom="8pt">
<!-- <ImageView
android:id="#+id/dmp_wel_sta_wal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="1"
android:contentDescription="#string/dmp_welc_wall"/> -->
</LinearLayout>
<LinearLayout
android:id="#+id/rightButtonColumn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1795"
android:gravity="center"
android:paddingTop="20pt"
android:paddingBottom="20pt"
android:paddingLeft="8pt"
android:paddingRight="8pt"
android:orientation="vertical">
<Button
android:id="#+id/dmp_edi_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"
android:layout_marginBottom="15pt"/>
<Button
android:id="#+id/dmp_sav_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"
android:layout_marginBottom="15pt"/>
<Button
android:id="#+id/dmp_add_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"
android:layout_marginBottom="15pt"/>
<Button
android:id="#+id/dmp_del_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"
android:layout_marginBottom="15pt"/>
<Button
android:id="#+id/dmp_exi_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"/>
</LinearLayout>
Before posting here I tried all the available solution already discussed here in SO...but to no avail. So to summarize the problem...I basically need a really good way (best-practise) of setting or changing all the buttons' enabled states everytime a new fragment is loaded (and displayed) to the user in the "dmpFragContainer".
I want to apologize beforehand if it seems like I am talking down to anyone, but I want to make sure everyone that reads this post will clearly understand the problem at hand. Please feel free to shred my code if you think I can implement better "best-practise" code structure - as I said before...I am new to Android coding and need all the help I can get.
Appreciate the help...
Cheers,
SilSur.
Thweeet! I solved the problem. Hello all... I managed to solve this problem I had with enabling or disabling all ten buttons just the way I want them to be disabled everytime a new fragment is loaded and visible to the user. The solution is that you have to implement this "enable/disable" code in the class files for each of the fragments. Pretty straightforward actually... So without further ado here is the solution in a more understandable fashion [code]...
This is the code from only one "BirtListFrag.java" of the 16 fragments I have running through my application...
package com.carzy.carzyapp;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
#SuppressLint("NewApi")
public class BirtListFrag extends Fragment {
public static Button BirCatBtn;
public static Button EveCatBtn;
public static Button AppCatBtn;
public static Button TodCatBtn;
public static Button SpeCatBtn;
public static Button EdiFunBtn;
public static Button SavFunBtn;
public static Button AddFunBtn;
public static Button DelFunBtn;
public static Button ExiFunBtn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dmp_birt_list_frag, container, false);
}
#Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
/*** Get references to all the buttons you want to manipulate everytime this Fragment is loaded & visible in the viewport ***/
BirCatBtn = (Button) getActivity().findViewById(R.id.dmp_bir_btn);
EveCatBtn = (Button) getActivity().findViewById(R.id.dmp_eve_btn);
AppCatBtn = (Button) getActivity().findViewById(R.id.dmp_app_btn);
TodCatBtn = (Button) getActivity().findViewById(R.id.dmp_tod_btn);
SpeCatBtn = (Button) getActivity().findViewById(R.id.dmp_spe_btn);
EdiFunBtn = (Button) getActivity().findViewById(R.id.dmp_edi_btn);
SavFunBtn = (Button) getActivity().findViewById(R.id.dmp_sav_btn);
AddFunBtn = (Button) getActivity().findViewById(R.id.dmp_add_btn);
DelFunBtn = (Button) getActivity().findViewById(R.id.dmp_del_btn);
ExiFunBtn = (Button) getActivity().findViewById(R.id.dmp_exi_btn);
/*** Now you can manipulate whatever attributes (***See Below***) of the buttons u created references to ABOVE ***/
BirCatBtn.setEnabled(false);
EveCatBtn.setEnabled(true);
AppCatBtn.setEnabled(true);
TodCatBtn.setEnabled(true);
SpeCatBtn.setEnabled(true);
EdiFunBtn.setEnabled(false);
SavFunBtn.setEnabled(false);
AddFunBtn.setEnabled(true);
DelFunBtn.setEnabled(false);
ExiFunBtn.setEnabled(true);
}
}
As you can see...it is pretty logical. Anyway. I owe this breakthrough to "Greensy" who answered a question posted by "ColorFrog" on a similar problem that I had ==> here is the jump if you want to check out what I am talking about..."Disabling buttons in a Fragment".
Anywho...at the time of posting this reply I had been a member of S.O. for only 5 days and thus I couldn't "upvote" Greensy's answer as I didn't have that privilege at the time. So, since his answer really helped me solve my problem, I decided the least I could do was post this reply and thank him/her upfront. So thanks mate...I definitely owe u something (hmm...a beer maybe?!). Who knows. Anyway, I hope a will get to return the favor one day.
Cheers,
Shore-T.

Unfortunately (project name) has stopped working on button click

An area which will present a predefined question
A button which will cause the answer to the question to be shown while leaving the question on the screen
An area which will present the answer
A button which will cause a transition to a screen that is formatted the same as this one, with the next question shown on it
A button which will cause the app to end (a transition to (3))
Only trying to get the transition from screen 1 to screen 2 to work with a different question/answer pair in its place with a button click. If there is anyway to do this other than switching the screens and activities which is the error, please let me know.
package com.example.androidassignment2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class AndroidAssignment2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_assignment2_1);
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_assignment2_1, menu);
return true;
}
}
layout file
<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:orientation="vertical" >
<TextView android:id="#+id/Questions"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:text="#string/Q2" />
<Button android:id="#+id/QButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_question" />
<Button android:id="#+id/AButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
<TextView android:id="#+id/Answers"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:hint="#string/A2" />
<Button android:id="#+id/QuitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_quit" />
</LinearLayout>
activity 1 file (incase needed)
package com.example.androidassignment2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivityForResult(myIntent, 0);
}
});
}
#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;
}
}
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivityForResult(myIntent, 0);
You are starting a new activity, the most likely cause is that this is has not been declared in the Manifest.
In your AndroidManifest.xml`, you will need to add something like this:
<activity
android:name="your.package.name.AndroidAssignment2_1" />
This will reside inside of the <application> tag.
You are using startActivityForResult() but you don't override the onActivityForResult() method in your first Activity. You could change that to just use startActivity() and remove setResult() from your second Activity.
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivity(myIntent);
}
});
If this isn't your problem then please post logcat (always when you have a crash) and make sure the Activity is decalred.
As far as your other inquiry
If there is anyway to do this other than switching the screens and activities which is the error, please let me know.
If you are simply changing the text then you can stay in the same Activity and change the text of your TextViews. You could store questions and answers in something like Arrays or in a DB if you wish and keep a counter to where you are and change the text with each click.
Another option, which may go along with the first if you want, is to use a ViewPager

Categories

Resources