When I was working on a new view yesterday I had 7*4 Edit texts , so I had to add lots of code to cover them when the user tries to change the color , for example Red
So I was thinking to do something like this:
if(item.getItemId() == R.id.menu_table_colors_Red){
EditText Holder = (EditText)mView.findViewById(View_ID);
Holder.setBackgroundResource(R.color.RED);
return true;
}
The View_ID is from onCreateContextMenu() which tells me which view was clicked on and mView is my fragment view , it works fine and all until I needed to save the values of the colors so the user can return and find the colors that he used:
I found two ways after searching either with a way in 11 api but I need a way to work on 8 api and higher.
the second was to implement a custom Edittext class , I copied the class from the solution to test it and here it is:
public class NewEditText extends EditText {
private int color;
public NewEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public NewEditText(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public NewEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
#Override
public void setBackgroundColor(int color) {
// TODO Auto-generated method stub
this.color=color;
super.setBackgroundColor(color);
}
public int getBackgroundColor() {
return color;
}
}
but I'm not sure how to use this ? when I do this it crashes because of " class cast exception "
NewEditText holder = (NewEditText)mView.findViewById(View_ID);
how am I supposed to use this ? sorry but I've never used a custom class and I couldn't find a solution.
Edit 1 the xml , sorry it's kinda big so I decided to post one of the days since there are 7 days and they're the same:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:id="#+id/table_view_mondaylayout"
android:layout_margin="0.3dp"
>
<TextView
android:layout_width="32dp"
android:layout_height="match_parent"
android:text="Mon"
android:layout_weight="0"
android:gravity="center"
android:background="#drawable/custom_table"
android:layout_margin="0.3dp"
/>
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="125 \n down"
android:textSize="14sp"
android:layout_weight="1"
android:background="#drawable/custom_table"
android:id="#+id/table_monday1"
android:layout_margin="0.3dp"
/>
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="125 \n down"
android:textSize="14sp"
android:layout_weight="1"
android:background="#drawable/custom_table"
android:id="#+id/table_monday2"
android:layout_margin="0.3dp"
/>
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="125 \n down"
android:textSize="14sp"
android:layout_weight="1"
android:background="#drawable/custom_table"
android:id="#+id/table_monday3"
android:layout_margin="0.3dp"
/>
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="125 \n down"
android:textSize="14sp"
android:layout_weight="1"
android:background="#drawable/custom_table"
android:id="#+id/table_monday4"
android:layout_margin="0.3dp"
/>
</LinearLayout>
Related
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
I'm using this CheckBoxPreference Class to support RTL text direction for pre Android 4.2 versions and to use a custom font style for my preference
public class MyCheckBoxPreference extends CheckBoxPreference {
TextView txtTitle, txtSum;
View Custmv;
public MyCheckBoxPreference(Context context) {
super(context);
}
public MyCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onBindView(#NonNull View view) {
super.onBindView(view);
Context context = getContext();
txtTitle = (TextView) view.findViewById(android.R.id.title);
txtTitle.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"));
txtSum = (TextView) view.findViewById(android.R.id.summary);
txtSum.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"));
}
#Override
protected View onCreateView(ViewGroup parent) {
Context ccc = getContext();
LayoutInflater inflater = (LayoutInflater) ccc.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Custmv = inflater.inflate(R.layout.arabic_checkboxpreference_layout, parent, false);
return Custmv;
}
and I'm using it in my prefs.xml like so:
<com.app.myclasses.MyCheckBoxPreference
android:defaultValue="false"
android:key="cb_big"
android:summaryOff="#string/off"
android:summaryOn="#string/on"
android:title="#string/Show_Big_Text" />
And here is the arabic_checkboxpreference_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize"
android:id="#+id/sos">
<CheckBox
android:id="#+android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginLeft="16dip"
android:minWidth="56dp"
android:gravity="center"
android:layout_gravity="center" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1"
android:gravity="end">
<TextView
android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:textColor="?android:attr/textColorPrimary"
android:text="Title"
android:layout_alignParentRight="true" />
<TextView
android:id="#android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="3"
android:text="subTitle"
android:layout_alignRight="#android:id/title"
android:textColor="?android:attr/textColorSecondary" />
</RelativeLayout>
Every thing works great, but the problem is when using more than one instance of my custom class, I mean when using multiple CheckBoxPreferences in prefs.xml and when clicking on a CheckBoxPreference it becomes focused for a while (~ 1-2 seconds) before getting checked or unchecked.
Any idea please?
Edit:
According to kha answer I removed the initialization from the onBind method to the constructor and every thing is smooth now:
public MyCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.mTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
this.Custmv = View.inflate(context, R.layout.arabic_checkboxpreference_layout, null);
TextView txtTitle = (TextView) Custmv.findViewById(android.R.id.title);
txtTitle.setTypeface(mTypeface);
TextView txtSum = (TextView) Custmv.findViewById(android.R.id.summary);
txtSum.setTypeface(mTypeface);
}
It may be related to the fact you load the font with Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf")every time you're binding the view.
Try moving it out from your onBindView method and create it once in your view instead and use the reference when you need to set the font and see if that makes any difference
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>
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.
In my foo_layout.xml file I have a subclassed RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.android.myapp.FooView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pegboard_table"
android:orientation="vertical"
android:scaleType="fitXY"
>
<ImageView
android:id="#+id/triangular"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/pegboard_board"
android:scaleType="fitXY"
android:gravity="center"
android:visibility="invisible"
/>
<Chronometer
android:id="#+id/timer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/timer_display"
android:textSize="40sp"
android:textColor="#000"
android:layout_alignParentTop="true"
android:gravity="center"
android:visibility="invisible"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/board_table"
android:visibility="invisible"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#drawable/tab_bar">
<!-- android:layout_alignLeft="#id/options_tab"-->
<ImageView
android:id="#+id/game_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/game_select" android:paddingLeft="15sp"/>
<ImageView
android:id="#+id/undo_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/game_select"
android:layout_weight="1"
android:src="#drawable/undo"
/>
<ImageView
android:id="#+id/settings_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/undo_select"
android:layout_weight="1"
android:src="#drawable/settings"
/>
<ImageView
android:id="#+id/info_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/settings_select"
android:layout_weight="1"
android:src="#drawable/info"
/>
</LinearLayout>
</com.android.myapp.FooView>
</FrameLayout>
Then I have a java file FooView.java with the class extending
RelativeLayout.
package com.android.myapp;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
public class FooView extends RelativeLayout {
public FooView(Context context) {
super(context);
fillTable();
// TODO Auto-generated constructor stub
}
public FooView(Context context, AttributeSet attrs) {
super(context, attrs);
fillTable();
// TODO Auto-generated constructor stub
}
public FooView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
fillTable();
// TODO Auto-generated constructor stub
}
public void fillTable() {
int board = this.getChildCount();
View boardView = findViewById(R.id.board_table);
if (board > 0)
;
}
}
board is always 0.
boardView is always null.
This doesn't seem like the right behavior. I've tried it with other views inside the FooView hierarchy and findViewById() always returns null.
For the life of me I can't figure out what I'm doing wrong.
First, your widget is attempting to reference widgets outside of itself. Calling findViewById() will get you children of your widget, not widgets elsewhere in the layout.
Second, you are trying to do this from your constructor. That is not safe. Please wait until onFinishInflate().
Third, you may not want to hardwire in R.id values for those other widgets, but allow those to be configurable, either through view attributes or setters. To access those widgets, given the configured IDs, you would call findViewById() not on yourself, but on your activity, obtained from getContext().
The image view and chronometer view are not part of the custom view until you add them using view.setlayout in the constructor.