Same style for every row in Android's TableLayout - android

Is there any way in Android XML to set the same style (or style paramerers, like padding) for all rows in a TableLayout at once?
What I would like to avoid doing is:
<TableLayout>
<TableRow style="#style/MyStyle">...</TableRow>
<TableRow style="#style/MyStyle">...</TableRow>
<TableRow style="#style/MyStyle">...</TableRow>
<TableRow style="#style/MyStyle">...</TableRow>
...
</TableLayout>

You can define your own styles by creating XML files on the res/values directory. So, let's suppose you want to have red and bold text, then you create a file with this content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyRedTheme" parent="android:Theme.Light">
<item name="android:textAppearance">#style/MyRedTextAppearance</item>
</style>
<style name="MyRedTextAppearance" parent="#android:style/TextAppearance">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
You can name it like you want, for instance res/values/red.xml. Then, the only thing you have to do is using that view in the widgets that you want, for instance:
<?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
style="#style/MyRedTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is red, isn't it?"
/>
</LinearLayout>
For more referense click here

Related

TableLayout not shown properly

I'm having trouble with the layout of my Android app.
The graphical preview editor shows me an image like (and this is how I want it to look like):
I am using a table layout that looks like this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="5dp" >
<TextView
style="#style/leftColumn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Branche" />
<TextView
android:id="#+id/textViewBranche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/rightColumn"
android:text="Branche"
/>
</TableRow>
</TableLayout>
styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- ... -->
<style name="leftColumn">
<item name="android:background">#2B60DE</item>
<item name="android:textColor">#ffffff</item>
</style>
<style name="rightColumn">
<item name="android:background">#C0C0C0</item>
<item name="android:textColor">#ffffff</item>
</style>
</resources>
If I run my app it looks like this:
As you can see only one text view is displayed. I would like the table layout to have 2 columns (just like in the preview). The first one (blue) indicating what I am displaying and the second (grey) the information provided by my adapter. However I only see the provided information of my adapter (in this case "sonstige (others)". So basically I only see the right column.
I have no idea why I can't see the content of the first text view (respectively the first row).
I played around with some properties like layout_width and layout_height but it doesn't change.
I'm running this app on my S3.
Any help would greatly be appreciated.
Cheers

CustomView Styling

I have a problem with styling customview.
I'd like to use different styles in different layouts. But my styles aren't applied.
CustomView layout:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
</merge>
Definition in another layout:
<com.buka.customviews.Bukaview
android:id="#+id/bukaId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/BukaStyle1">
styles.xml
<style name="BukaStyle1">
<item name="android:textSize">10sp</item>
</style>
<style name="BukaStyle2">
<item name="android:textSize">20sp</item>
</style>
Thanks
You should create a custom attribute :
<attr name="BukaStyleTextSize" format="dimensions" />
Then in the TextView tag of your custom view layout, put :
android:textSize="?attr:BukaStyleTextSize"
And finally, in your styles in styles.xml put :
<item name="BukaStyleTextSize>10sp</item>

How to set the textsize, textcolor and other parameters for the imagebutton in android?

How to set the text size, text color and other parameters in one xml and than assign them to the imagebutton in android?
I can set the parameters manually for each button, but if there are more than 3 buttons it becomes a nightmare. What is the best practices for doing this?
You can use styles for that
http://developer.android.com/guide/topics/ui/themes.html
Create a style depending on your requirement's add that to your button like this style="#style/your_style". look at this tutorial Styles and Themes
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomText" parent="#style/Text">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<EditText
style="#style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="#string/hello" />

Defining IDs within style, is it safe or a disaster?

Following question has kept me puzzled for a while and I thought maybe asking about this does no harm. I have the following layout.xml and style.xml files;
res/layout/layout.xml
<?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" >
<LinearLayout
style="#style/headerContainer" />
<LinearLayout
style="#style/footerContainer" />
<ScrollView
style="#style/contentContainer" />
</RelativeLayout>
res/values/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="container">
<item name="android:layout_width">fill_parent</item>
</style>
<style name="headerContainer" parent="container">
<item name="android:layout_height">40dp</item>
<item name="android:layout_alignParentTop">true</item>
<item name="android:background">#80FF0000</item>
<item name="android:id">#+id/header</item>
</style>
<style name="footerContainer" parent="container">
<item name="android:layout_height">50dp</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:background">#8000FF00</item>
<item name="android:id">#+id/footer</item>
</style>
<style name="contentContainer" parent="container">
<item name="android:layout_height">60dp</item>
<item name="android:layout_below">#id/header</item>
<item name="android:layout_above">#id/footer</item>
<item name="android:background">#800000FF</item>
</style>
</resources>
Now, the question is, is there a danger of overlapping IDs as I'm introducing them in style.xml? Funny thing is that this approach works, on the emulator I'm using at least, but the created IDs are not being added to the R class. And I'm a bit confused how they are defined once my layout is inflated.
Don't use #+id/... in styles.
#+id/... can only be used in layouts.
Otherwise you can get Error executing apt: return code 139 during build.
Use #id/... and generate ids with help resource file if needed:
res/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="header" />
<item type="id" name="footer" />
</resources>
http://developer.android.com/guide/topics/resources/more-resources.html#Id
Its not safe my friend. You should use different id for different files. Here the emulator will not act problem. It will understand because for every xml file there is unique code defined R.java file automatically. So emulator will understand from there very easily. But if you need to improve or edit the code for any up gradation surely you will get confused which id is belonging to which xml file's layout or wedged. So provide unique id to every widget of layout. It will be helpful if you provide the id including some tag of respective file name.
Example: If file name is filldetails.xml then you can use id=#+fd_name
It will be helpful to know the flow of application.
I am doing it this way and have had good luck:
Layout
res/layout/main.xml
<?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"
>
<include layout="#layout/action_bar"/>
</RelativeLayout>
Common:
res/layout/action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_container"
android:layout_width="fill_parent"
android:layout_height="#dimen/action_bar_height"
android:layout_alignParentTop="true"
android:paddingLeft="5dip"
android:paddingRight="5dip"
>
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="20dip"
android:textStyle="bold"
/>
</RelativeLayout>

Calling android dialog without it fading the background

I have this nice dialog view I set my UserInputDialog class to:
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/nameMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="What is your name Captain?"
>
</TextView>
<EditText
android:id="#+id/nameEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
>
</EditText>
<LinearLayout android:id="#+id/LinearLayout02" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:id="#+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK">
</Button>
<Button android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
I want my dialog to show up but for the background to not fade out. Is this possible? Cause the view that calls this dialog has a neato background I would like to be shown as a backdrop to the dialog.
I found this online:
<style name="doNotDim" parent="#android:style/Theme.Dialog">
<item name="android:backgroundDimAmount">0</item>
</style >
but not sure how to apply that to my dialog? I have a class called public class UserInputDialog extends Dialog implements OnClickListener. It sets its content view to the layout described above.
I think I am doing this all right, just not sure how to add that style so I can NOT fade the background.
Secondary question: Can you get new looks on your dialog (by say having an image or icon display with your text there) by using Themes?
You can also remove the dim effect by code with the following:
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Create a res/values/styles.xml file and add this to it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.DoNotDim" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
And apply the theme to your activity.
<activity android:name=".SampleActivity" android:theme="#style/Theme.DoNotDim">
Create a custom style and place it in your values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourCustomStyle" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
To set a style to an individual dialog you can use
Dialog dialog = new Dialog(context, R.style.YourCustomStyle);
For displaying dialog like TrueCaller do this:
In styles.xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myBackgroundStyle" parent="android:Theme.Dialog">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
In AndroidManifest.xml do this:
<activity android:name=".SampleActivity" android:theme="#style/myBackgroundStyle">

Categories

Resources