Add Text in ImageView on Android - java

I'm currently having a viewlist composed of Imageview. I'm trying to add text on each imageview of the list.
Here is the view management: A call called the ImgView method with it's context and Associated view.
public ImgView(Context c, ImageView ImageV) {
mImageView = filteredImage;
mImageView.setTag(this);
mFiltersText = new TextView(c);
mFiltersText.setText("No effect");
mFiltersText.setVisibility(View.VISIBLE);
mContext = c;
mImage = new GPUImage(mContext);
mEffect = R.id.none;
mActivity = (Activity) mContext;
}
the mEffect is just an int used to know the type of effect I want to apply on the pic and stored in xml
<resources>
<item type="id" name="none" />
<item type="id" name="blackandwhite" />
<item type="id" name="bla" />
</resources>
My goal is to find a way to get a string from the xml and display this string on the image view.
for example, when selecting "None" id, I display "No effect" on the image
I defined mFilterText but it's not displaying anything.
The View is get as shown below:
public View getView(int position, View convertView, ViewGroup parent) {
....
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_filter, null);
filteredImage = (ImageView) convertView.findViewById(R.id.filteredImage);
filteredImage.setBackground(new BitmapDrawable(mFakeBitmap));
filteredText = new TextView(mContext);
it's possible that filteredText is not correctly used. I use it as new but perhaps I need to attached it ad the FilteredImage do.
Any idea

Just place TextView on top of ImageView.

Use a Framelayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</FrameLayout>
You can also use the drawable property of textview
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_clear_white_24dp"
android:drawableRight="#drawable/ic_clear_white_24dp"
android:drawableBottom="#drawable/ic_clear_white_24dp"
android:drawableTop="#drawable/ic_clear_white_24dp"
/>

Related

change text color in spinner

I want to change a text color in text spinner in dropView. I tried to override the method getDropDownView and change a text color but it doesn't work.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
extendedCursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER) {
#Override
public View getDropDownView(int position, View convertView,android.view.ViewGroup parent){
View v = convertView;
if (v == null) {
Context mContext = AddEditLoadActivity.this;
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Androids orginal spinner view item
v = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
}
// The text view of the spinner list view
TextView tv = (TextView) v.findViewById(android.R.id.text1);
boolean disabled = !isEnabled(position);
if(disabled){tv.setTextColor(Color.WHITE);}
else{tv.setTextColor(Color.WHITE);}
return v;
}
#Override
public long getItemId(int position) {
extendedCursor.moveToPosition(position);
return extendedCursor.getLong(extendedCursor.getColumnIndex(DatabaseContract.DictionaryTable.ITEM_ID));
}
};
android.R.layout.simple_spinner_item this line in your spinner adapter use default behavior, try using custom layout to change text color.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#color/your_color" />
make sure its id will be #android:id/text1 and don't be changed.
use own custom layout
Create a layout named spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#316FA2"
android:textSize="12sp"
android:gravity="left"
android:singleLine="true"
android:padding="6dip"
android:textColor="#color/white" />///you can add your color
/>
replace android.R.layout with R.layout.spinner_row.xml

Edit Custom Views programmatically

I created a layout like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rlMenu"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_centerVertical="true">
<RelativeLayout
android:background="#drawable/dark_rectangle_bord"
android:id="#+id/rl1dia"
android:elevation="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_width="#dimen/sizeCard"
android:layout_height="#dimen/sizeCard"
android:layout_marginBottom="#dimen/padding_bottom_cards"
android:layout_marginEnd="#dimen/padding_end_cards"
android:layout_marginRight="#dimen/padding_end_cards"
android:layout_marginLeft="#dimen/padding_start_cards"
android:layout_marginStart="#dimen/padding_start_cards"
android:layout_marginTop="#dimen/padding_top_cards">
<TextView
android:text="1ยบ Dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1dia"
android:textStyle="normal|bold"
android:textColor="#color/Branco"
android:layout_alignParentBottom="true"
android:padding="10dp"
android:textSize="#dimen/texto_pequeno"
android:gravity="center"
android:fontFamily="sans-serif"
android:layout_centerHorizontal="true"/>
<ImageView
app:srcCompat="#drawable/ic_calendario_1"
android:id="#+id/iv1dia"
android:layout_width="#dimen/sizeImage"
android:layout_height="#dimen/sizeImage"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
After that, I had included it on my MainActivity.
<include
android:id="#+id/my_custom_view"
layout="#layout/custom_view></include>
But in this Custom View there are a RelativeLayout, a ImageView and a TextView. I need to create some Custom Views dynamically, sample as below.
How can i create this Custom View Programatically?
Ex.:
Button bt = new Button(MainActivity.this);
And how can I change the TextView, RelativeLayout, background and the ImageView programatically? Like:
CustomView cv = new CustomView(MainActivity.this);
cv.setImage(R.drawable.chips);
cv.setRlBackground(Color.WHITE);
cv.setText("Hello, World!");
You can use LayoutInflater for that
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = (View) inflater.inflate(R.layout.custom_view, null);
View email = (View) mView.findViewById(R.id.email);
yourParentView.addView(mView);
But make sure when you add view to its parent call removeAllview() method of yourParentView
Maybe you want to create an actual custom View, using include will not create a custom View but will only inject in your layout that particular xml. So, create a class (in this case extends RelativeLayout but you can use whatever suits you here)
public class CustomView extends RelativeLayout {
private TextView textView;
private ImageView imageView;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context, R.layout.custom_view, this);
imageView = (ImageView) findViewById(R.id.image_view);
textView = (TextView) findViewById(R.id.text_view);
}
public void setImage(int resId){
imageView.setImageResource(resId);
}
public void setText(String text){
textView.setText(text);
}
}
an xml of the layout of your custom View (custom_view.xml), you might want to use merge instead of include since you already have a parent layout (RelativeLayout in this case)
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/image_view"
android:layout_width="48dp"
android:layout_height="48dp"
/>
</merge>
add your custom view to the Activity layout like this, please note that you need to use the full name of the custom View class you're using, in this case com.example.lelloman.dummy.CustomView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<com.example.lelloman.dummy.CustomView
android:id="#+id/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
and in your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
CustomView customView = (CustomView) findViewById(R.id.custom_view);
customView.setImage(R.drawable.some_icon);
customView.setText("Hello world");
}

Android Set Custom Preference Layout

I'm working on it Android project. I have a prefs.xml code, something like this
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="pref_name_color_picker"
android:title="Colour"
android:summary="Colour of the name"
android:defaultValue="#FFFFFF"
android:layout="#layout/custom_name_setting_layout" />
</PreferenceCategory>
</PreferenceScreen>
And I need to custom preference layout. And I created;
custom_name_setting_layout.xml
<?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="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView
android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView
android:id="#+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignLeft="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="2" />
<ImageView
android:id="#+id/ivNameTextColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="32dp"
android:minWidth="32dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
And write a SettingActivity.java
public class SettingActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
int color = 0xffffff00;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_name_setting_layout, null);
ImageView ivNameTextColor = (ImageView) row.findViewById(R.id.ivNameTextColor);
ivNameTextColor.setBackgroundColor(Color.RED);
}
}
My problem is; I write setBackgroundColor method but not working. Not working meanin is, this program is running without error (like NullReferenceException, there is no error). But background color still not changing.
I don't know why. How can i solve this problem?
Thanks
Obviously, if you are hard-coding the color then you can just do it in your XML:
android:background="#android:color/red"
If you want to do it in code then unfortunately it's trickier than it might seem. You can't just set the color of the preference view in onCreate() because the preference views are stored in a list and are created and recycled dynamically as you scroll the list.
You need to set the background color when the view is being created. To do that you'll need to implement a custom preference class and override getView():
public class CustomColorPreference extends Preference
{
int backgroundColor = Color.BLACK;
public CustomColorPreference(Context context) {
super(context);
}
public CustomColorPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setCustomBackgroundColor(int color)
{
backgroundColor = color;
}
#Override
public View getView(View convertView, ViewGroup parent)
{
View v = super.getView(convertView, parent);
// v.setBackgroundColor(backgroundColor); // set background color of whole view
ImageView ivNameTextColor = (ImageView)v.findViewById(R.id.ivNameTextColor);
ivNameTextColor.setBackgroundColor(backgroundColor);
return v;
}
}
Change your XML to use the CustomColorPreference class:
<com.example.yourapp.CustomColorPreference
android:key="pref_name_color_picker"
android:title="Colour"
android:summary="Colour of the name"
android:defaultValue="#FFFFFF"
android:layout="#layout/custom_name_setting_layout" />
Then in your onCreate you can get the CustomColorPreference and set the color on it using the public method setCustomBackgroundColor():
CustomColorPreference picker = (CustomColorPreference)findPreference("pref_name_color_picker");
picker.setCustomBackgroundColor(Color.RED);

Android listview array adapter selected

i'm trying to add a contextual action mode to a listview, but i'm having some problems with the selection, if i make aList1.setSelection(position) it doesn't select anything, and if i make List1.setItemChecked(position, true) it works but it only changes the font color a little and i want it to change the background or something more notable, is there any way to detect the selection and manually and change the background, or i'm missing something?
the list:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:drawSelectorOnTop="false">
</ListView>
</RelativeLayout>
the adapter:
public class ServicesRowAdapter extends ArrayAdapter<String[]> {
private final Activity context;
private final ArrayList<String[]> names;
static class ViewHolder {
public TextView Id;
public TextView Date;
public RelativeLayout statusbar,bglayout;
}
public ServicesRowAdapter(Activity context, ArrayList<String[]> names) {
super(context, R.layout.servicesrowlayout, names);
this.context = context;
this.names = names;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.servicesrowlayout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Id = (TextView) rowView.findViewById(R.id.idlabel);
viewHolder.Date = (TextView) rowView.findViewById(R.id.datelabel);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.Date.setText(names.get(position)[2]);
holder.Id.setText(names.get(position)[1]);
return rowView;
}
}
with the use of a layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/idlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="right"
android:text="#+id/idlabel"
android:textSize="20dp"
android:width="70dp" >
</TextView>
<TextView
android:id="#+id/datelabel"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/datelabel"
android:textSize="20dp"
android:layout_marginLeft="90dp" >
</TextView>
</RelativeLayout
This is becasue in touch mode there is no focused or selection. You're on the right track using the checked state. You just need to use a state drawable with different states.
Steps:
1) In your row_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/item_selector"
>
2) Create a new xml file (match the name to the one you used in your row layout) in your drawable folder like so (note that I have a color.xml file set up so I can use #color/color, if you don't you can simply put a hex color code in there):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="true"
android:drawable="#color/blue" />
</selector>
That's it. It's not reaql intuitive, but "activated" basically means "checked" when you have enabled the checked state.
With that setup (and enabling the list rows to be checkable) you will now have the background changed from whatever it is normally to blue when in the checked state. Add code to set that state in onTouch or onClick and you'll be all set. The background will stay that way until you touch another item.

Android: Use XML Layout for List Cell rather than Java Code Layout (Widgets)

I'm in the process of making a music app and I'm currently working on the library functionality. I'm having some problems, however, in working with a list view (In particular, the cells). I'm trying to move from a simple textview layout in each cell that's created within java to one that uses an XML file for layout (Hence keeping the Java file mostly semantic)
This is my original code for the cell layout:
public View getView(int position, View convertView, ViewGroup parent) {
String id = null;
TextView tv = new TextView(mContext.getApplicationContext());
if (convertView == null) {
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id += "\n" + musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM);
musiccursor.moveToPosition(position);
id += "\n" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
And my new version:
public View getView(int position, View convertView, ViewGroup parent) {
View cellLayout = findViewById(R.id.albums_list_cell);
ImageView album_art = (ImageView) findViewById(R.id.album_cover);
TextView album_title = (TextView) findViewById(R.id.album_title);
TextView artist_title = (TextView) findViewById(R.id.artist_title);
if (convertView == null) {
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM);
musiccursor.moveToPosition(position);
album_title.setText(musiccursor.getString(music_column_index));
//music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
//musiccursor.moveToPosition(position);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
artist_title.setText(musiccursor.getString(music_column_index));
} else{
cellLayout = (TextView) convertView;
}
return cellLayout;
}
The initialisation (done in the on create file):
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(this));
musiclist.setOnItemClickListener(musicgridlistener);
And the respective XML files:
(main)
<?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">
<ListView
android:id="#+id/PhoneMusicList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1.0"
android:text="#string/no_list_data"
/>
</LinearLayout>
(albums_list_cell)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/albums_list_cell"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/album_cover"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="50dip"
android:layout_height="50dip"
/>
<TextView
android:id="#+id/album_title"
android:layout_toRightOf="#+id/album_cover"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/artist_title"
android:layout_toRightOf="#+id/album_cover"
android:layout_below="#+id/album_title"
android:layout_width="wrap_content"
android:layout_height="15dip"
/>
</RelativeLayout>
In theory (based on the tiny bit of Android I've done so far) this should work..it doesn't though. Logcat gives me a null pointer exception at line 96 of the faulty code, which is the album_title.setText line. It could be a problem with my casting but Google tells me this is ok :D
Thanks for any help and let me know if you need more info!
EDIT: Just to point out, the code this section is based on is from, most of is the same as mine for now
http://androidsamples.blogspot.com/2009/06/displaying-list-of-video-files-stored.html
When you want to use a custom layout to represent each item, in your adapter you need to inflate your custom layout in your getView method, using a LayoutInflater.
So i would change the following line:
View cellLayout = findViewById(R.id.albums_list_cell);
to:
View cellLayout = LayoutInflater.from(mContext).inflate(R.layout.albums_list_cell,null);
You need to move your albums_list_cell.xml into the layout folder aswell.
You would then also edit how you retrieve the relevant widgets defined in your layout as follows:
ImageView album_art = (ImageView) cellLayout.findViewById(R.id.album_cover);
TextView album_title = (TextView) cellLayout.findViewById(R.id.album_title);
TextView artist_title = (TextView) cellLayout.findViewById(R.id.artist_title);
Hope this helps!
Solved the above problem...note the (TextView) cast on the following lines. This should be a relativelayout cast of course
} else{
cellLayout = (TextView) convertView;
}

Categories

Resources