Android: Creating custom support v7 preference not working as Expected - android

I have created a sample demo application for the custom Android preference using support library preference-v7.
Code: https://github.com/saurabhkpatel/Android-Custom-Preference-Sample
I am facing two problems here:
In this demo application, I have total three different types of preferences. One is ListPreference, Second is Custom Preference which I have created and the last one is SwitchPreferenceCompat. If I put Custom Preference in between these two preferences categories then it's not working expected. Please check this attached screenshot. You can see that third SwitchPreferenceCompat is missing.
Even I can see the seek bar twice which comes from custom layout file, but I have only one seek-bar over there.
Everything is working fine if I put SampleCustomPreference at last.
Any ideas, Why this behavior is happening?
Thanks for your time.
UnExpected
Expected

The layout file layout_pref.xml has an issue. The height of the parent layout should be wrap content not match parent. Corrected xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/widget_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#android:id/title"
style="#android:style/TextAppearance.DeviceDefault.SearchResult.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Title" />
<TextView
android:id="#android:id/summary"
style="#android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Summary" />
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Related

Items Don't Stay Where Placed With Drag Drop Feature in Android Studio

So Android Studio has the drag/drop feature in the design tab. I am able to place the elements where I want them, and the layout looks great on the screen.
However, when I go to run the app emulator, it looks completely different. Everything gets shoved up to the top-left hand corner of the screen, nowhere near where I placed it in design mode.
Is there a way to have your elements show the same way in the emulator, as the way you place them in design mode? Right now I'm having to go back and edit all the code each time.
I have my code below, as well as a image link below to further clarify my point.Comparison of Design mode vs App Emulator
<?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.luke.currencyconverter.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter The Amount in Dollars"
tools:layout_editor_absoluteX="-159dp"
tools:layout_editor_absoluteY="126dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="403dp"
android:layout_height="0dp"
android:text="Enter The Amount in Dollars:"
android:textColor="#android:color/black"
android:textSize="22sp"
tools:layout_editor_absoluteX="33dp"
tools:layout_editor_absoluteY="365dp" />
<ImageView
android:id="#+id/currency"
android:layout_width="361dp"
android:layout_height="450dp"
app:srcCompat="#drawable/currency"
tools:layout_editor_absoluteX="12dp"
tools:layout_editor_absoluteY="-117dp" />
<Button
android:id="#+id/convert"
android:layout_width="361dp"
android:layout_height="wrap_content"
android:onClick="convert"
android:text="Convert"
tools:layout_editor_absoluteX="12dp"
tools:layout_editor_absoluteY="462dp" />
<EditText
android:id="#+id/dollarField"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="408dp" />
</android.support.constraint.ConstraintLayout>
Drag and drop for android will only place the object within the screen. To keep it there you should modify the xml so that the objects will be constrained to each other.
You can use either relative layout or constraint layout for an easier time in designing your layout.
I figured it out. I was using "constraint layout", and I needed to use "relative layout".
I just had this error as well and as annoying as it sounds, my solution was to delete my xml file and recreate it.

Generate UI from a string [duplicate]

I had lots of research about this issue but I didn't get the answer that I want.
So I do have an application getting string from the server. The series of string is in XML Format.
Here is an example of what I shall get from the server (as you can see it's a layout):
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/number_ref" />
<EditText
android:id="#+id/etTxtNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/subject_ref" />
<EditText
android:id="#+id/etTxtSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/body_ref" />
<EditText
android:id="#+id/etTxtBody"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btnTxtSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/save_ref"/>
<Button
android:id="#+id/btnTxtSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/send_ref"/>
<Button
android:id="#+id/btnTxtClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close_ref"/>
</LinearLayout>
</LinearLayout>
This will be changed depending on what xml file will be uploaded from the server. My problem is how can I implement these series of string into layout and to be loaded as layout of an activity. I was thinking of saving it in an xml file to sdcard of the device and load it as layout but I guess it's not possible to recompile the code after running it. Any suggestions? Thank you.
As explained in the Android documentation, the layout xml files are not used at runtime - they are compiled to binary code at compile time. So, you can't directly load the xml as a Layout.
You'd have to write code that reads the xml, interprets it, and uses Android api methods to create a corresponding View.
Common use cases
If you need to A/B test different UI designs without the need to re-upload your APK.
If your app's UI changes dynamically based on different users or scenarios.
If you need to deploy UI fixes quickly and in real-time.
json2view can help you.
I think now it can be done using the dynamic layout's in android.
https://www.javacodegeeks.com/2012/09/android-dynamic-and-xml-layout.html#:~:text=Android%20activity%20contains%20various%20user,activity%20in%20two%20different%20ways.&text=Basically%2C%20Layout%20file%20is%20xml,file%20present%20in%20res%2Flayout.
https://medium.com/mindorks/creating-dynamic-layouts-in-android-d4008b72f2d

Android xml error: "No resource found that matches the given name" with RelativeLayout (#id/LinearLayout_acc, #id/ProgressBar_statusScreen)

Okay, so this is starting to really annoy me. This error pops up in a very special, not very logical way.
Let me start out by saying that I have already looked at the other questions regarding this error, Google'd it too. As far as I can tell, most similar problems occur because people refer to a String resource or something else not within the same layout file, they misplace the '+' in '#id+' or something similar.
The problem I am having occurs in a layout .xml file with a RelativeLayout. This contains a TableLayout, two LinearLayouts containing some text and finally a ProgressBar. What I want is the progress bar to be aligned with the relative layout using android:layout_alignParentBottom="true" and then align the two linear layouts above the progress bar (the bottom linear layout aligning above the progress bar, the other aligning above the bottom linear layout).
It should be simple enough and looks as if it works, i.e. the graphic view shows the desired result. However, and here comes the problem, Eclipse gives me an error on the two linear layouts,
"Error: No resource found that matches the given name (at 'layout_above' with value '#id/LinearLayout_acc')."
and the same error for the other linear layout referring to the progress bar. I have more than triple checked that there are no typos (the id's also exist in packagename.R.java), and I have tried cleaning the project a dozen times.
I don't get the error when saving (and auto building), not until I decide to run the project. Another weird thing is that when I refer to the bottom linear layout from the progress bar instead of the top linear layout, I get no error!
My layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_activity" >
<TableLayout
... />
<LinearLayout
android:id="#+id/LinearLayout_dist"
android:layout_above="#id/LinearLayout_acc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" >
<TextView
... />
<TextView
... />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout_acc"
android:layout_above="#id/ProgressBar_statusScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
<TextView
... />
<TextView
... />
</LinearLayout>
<ProgressBar
android:id="#+id/ProgressBar_statusScreen"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="16dp" />
</RelativeLayout>
Please help, I have no idea what causes this error!
Edit with answers
Shrikant came with the solution of changing the order of appearance in the layout file so that elements reference only other elements already defined when the reference is read.
Also, as others have posted, changing #id/ to #+id/, even in a reference, does remove the error messages. As Marco W. wrote in this thread, the thing is you have to use #+id/ the first time each id is mentioned and then use #id/ afterwards, even though the first time may not be a definition.
I made most of the design and set the referred id's in Eclipse's graphical editor, so code that resulted in an error message was automatically inserted. Maybe this is a bug in Eclipse.
change
#id/LinearLayout_acc
to
#+id/LinearLayout_acc
Please check the below code
<?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:background="#drawable/ic_launcher" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/LinearLayout_dist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/LinearLayout_acc"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIRST" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SECOND" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout_acc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/ProgressBar_statusScreen"
android:layout_centerHorizontal="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIRD" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FOURTH" />
</LinearLayout>
<ProgressBar
android:id="#+id/ProgressBar_statusScreen"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="16dp" />
</RelativeLayout>
Also check the following link.
It says that android:layout_below="#id/myTextView" won't recognise an element with id "myTextView" if it is written after the element you're using it in.
Change every id #id to #+id, no matter when it's defining or referencing an id. With this, you won't get
Android xml error: “No resource found that matches the given name” with RelativeLayout (#id/LinearLayout_acc, #id/ProgressBar_statusScreen).
<LinearLayout
android:id="#+id/LinearLayout_dist"
android:layout_above="#+id/LinearLayout_acc" <--- here might be a problem you forgot + sign
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" >

How can I achieve this layout?

I am trying to achieve this in an android app layout.
So far I have managed to get the buttons the way I wish, but I am having trouble working out how I am going to implement the divider and the layout on the right side of it.
Here is what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<!-- this is main activity's layout. a main menu of sorts. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center" android:background="#drawable/background">
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:id="#+id/layout_maintest2_relativelayoutleft">
<Button android:text="#string/string_main_NewCalculation" android:drawableLeft="#drawable/calculator5" android:onClick="onClickButton_NewCalculation" android:id="#+id/button_main_NewCalculation" android:layout_width="300px" android:layout_height="90px" android:drawablePadding="0px"></Button>
<Button android:text="#string/string_main_Help" android:drawableLeft="#drawable/question" android:onClick="onClickButton_Help" android:id="#+id/button_main_Help" android:layout_width="150px" android:layout_height="90px" android:drawablePadding="0px" android:layout_below="#id/button_main_NewCalculation"></Button>
<Button android:text="#string/string_main_Share" android:drawableLeft="#drawable/share" android:onClick="onClickButton_Share" android:id="#+id/button_main_Share" android:layout_width="150px" android:layout_height="90px" android:drawablePadding="0px" android:layout_below="#id/button_main_NewCalculation" android:layout_toRightOf="#id/button_main_Help"></Button>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/layout_maintest2_relativelayoutright" android:layout_toRightOf="#id/layout_maintest2_relativelayoutleft" android:layout_alignTop="#id/layout_maintest2_relativelayoutleft">
<TextView android:layout_width="wrap_content" android:textSize="24px" android:textStyle="bold" android:layout_height="match_parent" android:id="#+id/textView_calculator_CalculatorTitle" android:text="#string/string_calculator_CalculatorTitle"></TextView>
</RelativeLayout>
</LinearLayout>
Its hard to tell from the image you uploaded but are you trying to keep the two relative layouts side by side?
You can try to position the layouts by declaring them to be drawn to one side of the parent or the other. I cant remember off the top of my head at the moment and I know thats a crappy answer but I think the answer is somewhere in here
http://developer.android.com/guide/topics/ui/layout-objects.html
Take a look at the android:layout_alignParentRight="true" declaration.
Im pretty sure I used something similar to position multiple views and buttons in the same parent before. Try it out and when I get back to my work computer on monday Ill look through my projects and see which one I know works the best.

Use layout_alignBaseline with Spinner

I've noticed that layout_alignBaseline of TextVew control doesn't work with Spinner control.
I'm trying to place a text to the left of spinner, but it goes to the top of the parent control.
<Spinner
android:id="#+id/locations"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/userCode"
android:layout_alignLeft="#id/userCode"
android:layout_marginTop="15dip"
/>
<TextView
android:id="#+id/locationsLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Region"
android:layout_alignBaseline="#id/locations"
/>
Is it a bug or I do something wrong? The same technique works fine with EditText controls.
I am also facing the same problem but found the following workaround
<Spinner
android:id="#+id/locations"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/userCode"
android:layout_alignLeft="#id/userCode"
android:layout_marginTop="15dip"/>
<TextView
android:id="#+id/locationsLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Region"
android:layout_alignTop="#+id/locations"
android:layout_alignBottom="#+id/locations"
android:gravity="center_vertical"/>
Newer versions of Android support base line alignment for Spinners. The Spinner will create the first View of the list and use its baseline alignment.
If that doesn't work, make sure your spinner adapter creates a View that supports base line alignment. (getBaseline() != -1). For LinearLayouts, you can use the baselineAlignedChildIndex attribute to specify a child within the linear layout to use.
android:layout_toLeftOf="#id/locations" ?

Categories

Resources