android error drives me crazy - android

I'm new to android developing, using eclipse Luna,
When I create my first blank activity project as my book says there should be a layout file (for designing visually the app) in the path"res/layout",
for instance a file named "MyFirstapp.xml" but in my case the layout folder is empty!
I mean there is no where yo see the designing window.
please help me with this problem.

You need to create a new file in the res/layout folder.
Name it e.g. sample_layout.xml
Create a xml based layout or use the graphical interface for it.
Sample code from Android Developers Website:
(http://developer.android.com/guide/topics/ui/declaring-layout.html)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
Afterwards you can use the layout by calling:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_layout);
}
If you want to get into Android developing please make sure you study Android Developer website:
http://developer.android.com/
There are tons of examples and tutorials!

Related

Layout in Emulator is not matching that from Android Studio

So I'm completely new to Android Development and I'm working through a book on Android Game Development. First game we build is called Tappy Defender.
Anyway, they provide me with background image and told me to put a button and a TextView. I put the background in the drawable folder and assigned it to the background for the Activity, I also aligned both the TextView and the button.
However, when I build and run the program in the emulator, both the button and the TextView are located in the top left corner (in landscape) and are overlapping each other. The background also doesn't show at all.
I'm so confused as to what it is that I'm doing wrong as I followed the steps correctly and have gone over it several times.
Any help appreciated.
I've included the .xml file too:
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:background="#drawable/background"
tools:context="com.example.tappydefender.MainActivity">
<Button
android:id="#+id/buttonPlay"
android:layout_width="122dp"
android:layout_height="39dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Play"
tools:layout_editor_absoluteY="293dp"
tools:layout_editor_absoluteX="280dp" />
<TextView
android:id="#+id/textHighScore"
android:layout_width="158dp"
android:layout_height="28dp"
android:text="TextView"
tools:layout_editor_absoluteX="264dp"
tools:layout_editor_absoluteY="258dp"
tools:text="High Score: 99999" />
</android.support.constraint.ConstraintLayout>
You are using a Constraint layout, but you are not constraining anything. Furthermore, you are using "tools" attributes, which only apply to the emulator, and are ignored when the app is built.
You need to set constraints to your views, like
app:layout_constrainTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/otherView"
Please read on Constraint layout (https://developer.android.com/training/constraint-layout/index.html) and apply constraints to your views.

Simple but serious issue with Tablet programming for android

So I'm taking the course by Google "Developing Android Apps" and I am stuck since I ran into a serious problem I can't explain:
See,their idea is to have a different layout file for tablets under the folder res\layout-sw600dp so that if the minimum height/width of the device is 600dips then there will be a different layout inflated.
The only problem is that for some reason that assertion doesn't work, not for their code nor for the simple code i added here,at least not for an AVD tablet (I've used Nexus 10 with api 21).
For example the following code shows "Phone!" on the toast rather than "Tablet!":
public class MainActivity extends AppCompatActivity {
static final String TABLET="Tablet!";
static final String PHONE="Phone!";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String foundDeviceStr;
if(findViewById(R.id.weather_detail_container)==null){
foundDeviceStr=PHONE;
} else{
foundDeviceStr=TABLET;
}
Toast toast=Toast.makeText(this,foundDeviceStr,Toast.LENGTH_LONG);
toast.show();
}
}
When the xml file under the folder layout-sw600dp does have a frame with the id weather_detail_container.
I will add both xml files that I created at the bottom of the email, but you can also try out branch 5.10 from the following Google project- it doesn't load a two panes as it should:
https://github.com/udacity/Sunshine-Version-2/tree/5.10_selected_item, again at least not for any AVD devices (I have a physical android phone but not a physical tablet).
For the question to be complete here are the two xml files:
under the folder layout this is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Whatever"/>
</LinearLayout>
and under the folder layout-sw600dp this is activity_main.xml:
<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:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
tools:context="com.example.android.sunshine.app.MainActivity">
<!--
This layout is a two-pane layout for the Items master/detail flow.
-->
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="Whatever" />
<FrameLayout
android:id="#+id/weather_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4" />
</LinearLayout>
You need to ask/get the SCREENLAYOUT_SIZE_MASK that is the key...
I have tried ONLY on samsung devices but it was pretty accurate....
Example:
Context myAppContext = ...//load the context here...;
boolean amATablet = (myAppContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
if (amATablet) {
//Hello Tablet
}else{
//hello Phone
}
OK guys,so I just got a viable answer to my question:
Simply uncheck "enable device frame" in emulator settings!
If you need further assistance you can use this link:
How to remove the device's frame on Android Studio's emulators
Having said all that, I am not sure WHY that works. After all,the definition of this setting is: "Enable a frame around the Android emulator window that mimics the look of a real android device", so that SHOULD change only the looks according to my understanding. So if anyone knows why, please do post a further answer or comment :)

Rendering error when using EditText widget inside LinearLayout

I am working on CrimeIntent app in Android Programming:
The Big Nerd Ranch Guide. I got a rendering problem when i put an EditText widget in a LinearLayout file for a fragment (fragment_crime.xml).Here is the code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/crime_title_hint"/>
</LinearLayout>
I do not want to change the theme.
There is no error in your layout xml... possible it is the plugins and library loading issue in android studio... try to restart it or check the missing libraries and targets.. or check the #string/crime_title_hint if it exists in Strings.xml resources.

Slide Toggle for Android

Anyone know of any open source implementation of a slide toggle for android. The default android toggle(ToggleButton) is not pretty. I am looking for anything similar to iOS. I should be able to implement one from scratch. But if anything similar is already available, then i can build on it.
Thanks in advance to the wonderful stackoverflow community.
Edit1:
What I meant by iOS Slide Toggle is UISwitch
Edit2: Just want to summarize the answer. Commonsware provided the clue. I ended up back porting the Switch code from 4.0 to2.2.2. Thanks to the open-sourced code, back porting was not very difficult. The code is hosted on git hub. http://github.com/pellucide/Android-Switch-Demo-pre-4.0/tree/master/
A screenshot from that project
you can use the sliding drawer widget in android to have a sliding toggle switch. you just have to "slice" the ios toggle images into3 parts, one for the handle, one for the sliding drawer background and one for the content part. then put an image on top of it like a frame to give you the "round edges"
here's what i've come up with:
XML 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" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp" >
<SlidingDrawer
android:id="#+id/slidingDrawer1"
android:layout_width="154dp"
android:layout_height="54dp"
android:background="#drawable/ios_retina_toggle_on_full"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/handle"
android:layout_width="54dp"
android:layout_height="54dp"
android:background="#00000000"
android:src="#drawable/ios_retina_toggle_button" />
<ImageView
android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ios_retina_toggle_off" />
</SlidingDrawer>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ios_retina_toggle_frame" />
</FrameLayout>
</LinearLayout>
ios_retina_toggle_on_full
ios_retina_toggle_button
ios_retina_toggle_off
ios_retina_toggle_frame
and finally a screenshot of how it looked on the emulator on a 3.7 WVGA screen running gingerbread:
iOS does not seem to have a "slide toggle", at least under that name, based on a Google search. And, you did not provide an image (or a link to an image) of what you want.
Android 4.0 added a Switch that you might be able to backport to earlier versions. You will see samples of it in the API Demos app on your emulator:
You can try using ToggleButton specifying your own drawables for its states.
We can try using seekbar with custom drawables and thumb, setting the max value to 1 and min to 0. we can add animations for sliding effect

Android Layout designer doesn't open XML file created in IntelliJ

My main working tool is IntelliJ. So I use it to create XML files and layouts for Android activities. However, if I open such XML file in Eclipse, it does not recognize it as layout file and does not load its GUI designer (I open XML file via Eclipse Layout Editor). If I create an XML file in Eclipse, the GUI designer loads properly.
The very content of Eclipse layout XML filet and IntelliJ layout XML file is 100% identical.
What am I doing wrong?
//////////////////////////////////////////////////////////////
EDIT: I have created new project in IntelliJ. Added some test elements into Main.xml. Exported project to Eclipse format (File->Export to Eclipse...). Closed IntelliJ, opened Eclipse, Imported the newly created project, opened Main.xml with Layout Editor and NOTHING again. This is structure of Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="just testing text"/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test inside of layout"/>
</LinearLayout>
</LinearLayout>
And this is how Eclipse Layout Editor sees this XML file.
Try this
Right click on that xml file
Select Open With
In that select Android Layout Editor
Once you copy the file to the res/layout folder, right click on the project in the project explorer and hit refresh to let it know Eclipse the file is there. You might need to clean the project and rebuild also.
An xml file does not depend on where you created, it's just text.
Make sure the name given to your xml is just all small letters and underscore, no Caps. I had the same problem and changing the Case worked well
just give it time to generate the view. the bigger your layout file, the longer it takes. what you're seeing is a lack of a progress bar that shows it's doing something. just be patient for a while and it should come up

Categories

Resources