RelativeLayout, match parent contains unwanted padding right? - android

I have a custom header bar (something like actionbar).
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/bg_header">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ibSlider"
android:src="#drawable/ic_drawer"
android:background="#color/transparent"
android:layout_marginLeft="#dimen/side_margin"
android:contentDescription="#string/general_content_description"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/header_branding"
android:id="#+id/tvAppName"
android:layout_toRightOf="#+id/ibSlider"
android:gravity="center_vertical"
android:layout_marginLeft="#dimen/side_margin"
style="#style/branding_orange"/>
<ProgressBar
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/pbLoading"
android:layout_alignParentRight="true"
android:indeterminate="true"
android:layout_marginRight="#dimen/side_margin"
android:layout_centerVertical="true"/>
</RelativeLayout>
When I'm adding this custom view in my activity, there is padding right that I have no idea comes from where! I have added green background to the view in order to find I'm talking about where.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<view
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/headerBar"
android:background="#color/green"
class="com.kamalan.widget.HeaderBar"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/headerBar"/>
</RelativeLayout>
And this is the code of my HeaderBar.
public class HeaderBar extends RelativeLayout {
private static final String TAG = "HeaderBar";
private Context mContext;
private ProgressBar progressBar;
private ImageButton ibMenuSlider;
public HeaderBar(Context context) {
super(context);
this.mContext = context;
init();
}
public HeaderBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init();
}
public HeaderBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
init();
}
private void init() {
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout) mInflater.inflate(R.layout.widget_headerbar, null);
addView(view);
progressBar = (ProgressBar) view.findViewById(R.id.pbLoading);
ibMenuSlider = (ImageButton) view.findViewById(R.id.ibSlider);
}
...
}
Image 1 shows preview of class header (first xml code) and second image displays when it has been added to my activity. I have no idea that green padding comes from where! any idea would be appreciated. thanks.

Problem was the image that I was using as background of Header. Have no Idea why it works on first image but doesn't work on second one. Maybe activity is messing or Theme has bug, as Bob said in comment.
Anyways, I fixed the problem by not using image :)
For other's reference, I changed xml of header as below and I could fix the problem in this way.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/header_bar_height"
android:background="#color/gray">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ibSlider"
android:src="#drawable/ic_drawer"
android:background="#color/transparent"
android:layout_marginLeft="#dimen/side_margin"
android:contentDescription="#string/general_content_description"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/header_branding"
android:id="#+id/tvAppName"
android:layout_toRightOf="#+id/ibSlider"
android:gravity="center_vertical"
android:layout_marginLeft="#dimen/side_margin"
style="#style/branding_orange"/>
<ProgressBar
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/pbLoading"
android:layout_alignParentRight="true"
android:indeterminate="true"
android:layout_marginRight="#dimen/side_margin"
android:layout_centerVertical="true"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/header_divider"
android:layout_alignParentBottom="true"/>
</RelativeLayout>

I also face same problem but i get success when i try customize Actionbar.I am unable to resolve your problem due to absence of drawable and string resource so please provide zip of this one so that i can see directly .....

Related

Set the text in horizontal time line

I am trying to implement horizontal timeline. SO I have written the code to design the horizontal line but I am able to figure out how I will write text on the above and below of the line.
One more thing I don't want to use any other library.
I have try to solve it through Custom view as people here have been suggested but got struck.
timeline_segment.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:weightSum="1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="3dp"
android:textAlignment="textEnd"
android:text="Top"
android:id="#+id/top_data"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="#color/alphabet_a"
android:layout_width="wrap_content"
android:layout_height="2dp" />
<ImageView
android:background="#drawable/circle1"
android:layout_width="15dp"
android:layout_height="15dp" />
<TextView
android:layout_weight="0.5"
android:layout_gravity="center"
android:background="#color/alphabet_a"
android:layout_width="wrap_content"
android:layout_height="2dp" />
</LinearLayout>
<TextView
android:padding="3dp"
android:gravity="center"
android:text="bottom"
android:id="#+id/bottom_data"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</merge>
timeline_segment.java
public class timeline_segement extends LinearLayout {
View rootView;
TextView upperText;
TextView startLine;
TextView endLine;
ImageView circleView;
TextView bottomText;
public timeline_segement(Context context) {
super(context);
init(context);
}
public timeline_segement(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public timeline_segement(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
rootView=inflate(context, R.layout.timeline_segment, this );
upperText=(TextView) rootView.findViewById(R.id.top_data);
bottomText=(TextView) rootView.findViewById(R.id.bottom_data);
upperText.setText("Top");
bottomText.setText("Bottom");
}
public void setUpperText(String string)
{
upperText.setText(string);
}
public void setBottomText(String string)
{
bottomText.setText(string);
}
}
Decided to answer because the comment box is kinda limiting. This is my comment:
You'll need a custom view to achieve this. You can either composite ready-made views or go full custom
If you choose to composite views, then you start by breaking down that image level by level. At the highest level, its a horizontal layout with 'TimelineCell's (or whatever you choose to call it).
A TimelineCell will basically be a vertical LinearLayout with a right aligned TextView, a View that draws the line and another center aligned TextView.
You can then create these programmatically and add them to a parent horizontal LinearLayout.
If you however choose to go full custom, Youll have to handle measuring, layouting and drawing of all the components including the text above and below the line.
Take a look at this link for a good introduction to custom views on android

Android - How to layout and use a custom View?

I am writing an app on Android Studio and I am trying to create a custom view with a specific form I need, something like:
Custom View Example
(I used different colors to show how the layout is supposed to go)
Here's the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="150dp"
android:weightSum="2"
android:background="#android:color/holo_red_dark">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/hourLbl"
android:layout_gravity="center"
android:background="#android:color/holo_blue_bright"/>
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/minuteLbl"
android:layout_gravity="center"
android:background="#android:color/holo_orange_dark"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/actionName"
android:layout_weight="1"
android:gravity="center"
android:background="#android:color/darker_gray"/>
</LinearLayout>
The problem comes when I try to add this view to another layout in the app.. It comes as a blank square. I have been trying to look for an answer on how to properly do this, but nothing helps.
This is what I have in the xml of the activity where I want the view (ActionView):
<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" tools:context=".HomeActivity">
<...ActionView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="#+id/firstAction" />
</RelativeLayout>
Here's the ActionView's constructors I have (not sure what to do with them to be honest, just copied them from a tutorial):
public ActionView(Context context) {
super(context);
}
public ActionView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.ActionView,
0, 0);
try {
mHourLbl = a.getString(R.styleable.ActionView_hourLbl);
mMinuteLbl = a.getString(R.styleable.ActionView_minuteLbl);
mActionName = a.getString(R.styleable.ActionView_actionName);
} finally {
a.recycle();
}
}
public ActionView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
I think you're missing the most important part. You're not tying your layout to your ActionView object...
Add:
LayoutInflater.from(context).inflate(R.layout.<xml_of_action_view>, this, true);
inside your ActionView constructor.

AndroidStudio not recognizing RelativeLayout subclass as a RelativeLayout

Hopefully this question will resolve to me having incorrect expectations, but I'm using Android Studio 0.2.10 and am having problems with the rendering system recognizing the XML for a custom view group extending RelativeLayout as an actual RelativeLayout. The effects of this are that children in the custom view aren't getting options that should be available to them... layout_width, layout_height or alignComponent for instance (which seems to indicate that it's not even recognizing the custom layout as a layout at all).
I can of course just add these attributes to the XML manually or just set the root tag to RelativeLayout temporarily and the design tool seems to work fine, but I'm curious if, because AndroidStudio is failing to resolve the custom extension properly, I'm doing something wrong by design. I'm new to Android development, so I didn't want to lob this away as an IDE bug prematurely.
public class CommentCellView extends RelativeLayout {
private static String TAG = "CommentCellView";
final private CommentCellView _cellView;
private Comment _comment;
public CommentCellView(Context context) {
super(context);
_cellView = this;
}
public CommentCellView(Context context, AttributeSet attrs) {
super(context, attrs);
_cellView = this;
}
public CommentCellView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
_cellView = this;
}
public void setComment(Comment comment) {
_comment = comment;
TextView nameTextView = (TextView)this.findViewById(R.id.commentcell_nameTextView);
TextView commentTextView = (TextView)this.findViewById(R.id.commentcell_commentTextView);
TextView dateTextView = (TextView)this.findViewById(R.id.commentcell_dateTextView);
nameTextView.setText(comment.name);
commentTextView.setText(comment.comment);
dateTextView.setText(comment.date);
}
}
<com.testapp.CommentCellView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ffffff">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/commentcell_backgroundImageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="#drawable/comment_bg"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/commentcell_avatarImageView"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="#drawable/avatar"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/commentcell_nameTextView"
android:layout_alignTop="#+id/commentcell_avatarImageView"
android:layout_toRightOf="#+id/commentcell_avatarImageView"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/commentcell_commentTextView"
android:layout_below="#+id/commentcell_nameTextView"
android:layout_alignLeft="#+id/commentcell_nameTextView"
android:textColor="#999999"
android:layout_marginTop="2dp"
android:layout_alignParentRight="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/commentcell_dateTextView"
android:layout_alignBottom="#+id/commentcell_avatarImageView"
android:layout_toRightOf="#+id/commentcell_avatarImageView"
android:layout_marginLeft="10dp"/>
</com.testapp.CommentCellView>

xml LinearLayout different behaviours

I am trying to create a custom button layout. I created an xml file "custom_button.xml" which I then inflate in the "ButtonLanguageSelection" class. In this class I added the onClickListener. I added this class to the "main_activity.xml". It behaves like it should except it won't receive any touch events. Then I copied the code from "custom_button.xml" and added it directly without inflating it, I just added the android:onClick parameter and in this way it worked. I can't find out what would be the problem, the layouts are the same.
Has some of you have similar problems? What could be the problem? I attached the code if it helps someone to find the problem.
Thank you for any help!
custom_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button_language_selection_states"
android:weightSum="1"
android:clickable="true"
>
<TextView
android:id="#+id/textview_select_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:paddingTop="30px"
android:layout_weight="0.4"
android:textSize="21sp"
android:textColor="#333333"
/>
<View
android:layout_width="match_parent"
android:background="#drawable/gradient"
android:layout_height="1dp"
android:layout_margin="10px">
</View>
<TextView
android:id="#+id/textview_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:paddingTop="40px"
android:layout_weight="0.6"
android:textSize="28sp"
android:textColor="#ffffff"
/>
</LinearLayout>
ButtonLanguageSelection
public class ButtonLanguageSelection extends LinearLayout
{
private TextView introductoryTextView;
private TextView language;
private final String TAG = "ButtonLanguageSelection";
public ButtonLanguageSelection(Context context) {
super(context);
}
public ButtonLanguageSelection(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.button_language_selection, this);
introductoryTextView = (TextView)findViewById(R.id.textview_select_language);
language = (TextView)findViewById(R.id.textview_language);
this.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onclick");
}
});
}
public ButtonLanguageSelection(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setIntroductoryTextView(String s)
{
introductoryTextView.setText(s);
}
public void setLanguage(String s)
{
language.setText(s);
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/screen_welcome_bg">
<defaultpackage.ButtonLanguageSelection
android:id="#+id/ButtonLanguageSelection_Slovene"
android:layout_width="341px"
android:layout_height="260px"
android:layout_marginRight="2px"/>
<defaultpackage.ButtonLanguageSelection
android:id="#+id/ButtonLanguageSelection_Italian"
android:layout_width="341px"
android:layout_height="260px"
android:layout_marginRight="2px"/>
<!--<defaultpackage.ButtonLanguageSelection-->
<!--android:id="#+id/ButtonLanguageSelection_English"-->
<!--android:layout_width="341px"-->
<!--android:layout_height="260px" />-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="341px"
android:layout_height="260px"
android:background="#drawable/button_language_selection_states"
android:weightSum="1"
android:clickable="true"
android:onClick="sendMessage"
>
<TextView
android:id="#+id/textview_select_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:paddingTop="30px"
android:layout_weight="0.4"
android:textSize="21sp"
android:textColor="#333333"
/>
<View
android:layout_width="match_parent"
android:background="#drawable/gradient"
android:layout_height="1dp"
android:layout_margin="10px">
</View>
<TextView
android:id="#+id/textview_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:paddingTop="40px"
android:layout_weight="0.6"
android:textSize="28sp"
android:textColor="#ffffff"
/>
</LinearLayout>
I think the problem is writing all the init code in only one constructor. Consider making an init function and call it from all the constructors. Also consider setting the onClick events listeners in your activity rather than than the layout constructor itself.

How to access the main activity's view from a custom widget?

I have a custom "view" widget (CustomController) on my application's main screen.
In the widget's constructor, I can access an image in the widget's xml file with the following code:
(Note, img_cursor is an ImageView in "custom_controller.xml")
public final class CustomController extends LinearLayout{
public CustomController(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view_controller = layoutInflater.inflate(R.layout.custom_controller,this);
img_cursor = (ImageView) view_dpad.findViewById(R.id.img_cursor);
...
However, I want to access a TextView on the main activity's screen.
The custom controller widget is displayed on the main screen through main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linlayRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.test.projectname.CustomController
android:id="#+id/dpad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="15px"
android:layout_marginLeft="15px" />
<Button
android:text="Connect"
android:id="#+id/btn_connection"
android:layout_height="wrap_content"
android:layout_width="100dp">
</Button>
<Button
android:text="Settings"
android:id="#+id/btn_settings"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</Button>
<Button
android:text="Debug x,y"
android:layout_width="wrap_content"
android:id="#+id/btn_debug_x_y"
android:layout_height="wrap_content">
</Button>
<TextView
android:id="#+id/txt_view_x"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X = 0">
</TextView>
<TextView
android:id="#+id/txt_view_y"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y = 0">
</TextView>
</LinearLayout>
How do I access the TextViews in main.xml from the CustomController class?
I've tried stuff like:
public final class CustomController extends LinearLayout{
public CustomController(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main_controller = layoutInflater.inflate(R.layout.main,this);
textview = (TextView) view_dpad.findViewById(R.id.txt_view_x);
...
I can't seem to access the main screen's view. It doesn't like:
View main_controller = layoutInflater.inflate(R.layout.main,this);
Thanks for any help!

Categories

Resources