hai friends...my java file indicate this error:
R.styleable cannot be resolved....
my xml file:
<?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">
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/videoGrdVw"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="5dip"
android:horizontalSpacing="5dip"
android:columnWidth="80dip"
android:stretchMode="columnWidth"
android:gravity="center"/>
<ImageSwitcher
android:id="#+id/switcher"
android:layout_height="match_parent"
android:layout_width="match_parent">
</ImageSwitcher>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
</LinearLayout>
source code:
private class VideoGalleryAdapter extends BaseAdapter
{
private int itemBackground;
public VideoGalleryAdapter(Context c)
{
_context = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
create an xml file under values folder, with the name attributes.xml and copy below contents to it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
</resources>
This should work.
According to this forum thread you need to change:
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
to
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
For me, this did the trick:
import android.R;
Related
Working on a very simple app to change color of background upon clicking a button, I am stuck at a bug where the color of background is unable to be changed using MainActivity.java file.
package com.example.audio;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
View view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = this.getWindow().getDecorView();
view.setBackground(R.color.colorAccent);
}
}
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
activity_main.xml
<?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=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
When setting the background in MainActivity.java file, the following error pops up for the line view.setBackground(R.color.colorAccent);
error: incompatible types: int cannot be converted to Drawable
What should I do to resolve this bug?
It should probably be view.setBackgroundResource(R.color.colorAccent);
Works with AppCompatActivity:
view.setBackground(ContextCompat.getDrawable(context, R.color.colorAccent));
and
view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent));
With AndroidX, you have to use yourContext.getResources().getColor(R.color.yourColor) instead of (R.color.yourColor)
I would like to programatically style my dynamically created checkboxes as radio buttons as I prefer the look.
To do this I have created a checkboxStyle.xml and put it in values section of the res folder.
checkboxStyle.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="checkboxStyle">
<item name="android:checkboxStyle">#android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton</item>
</style>
</resources>
I am then calling this in my fragment as I create my checkboxes but R.id.checkboxStyle is coming up as not found, not really sure where to put this id though or if I need to refer to it in another way
CheckBox checkBox;
ArrayList<String> list = new ArrayList<>();
list.add("test");
list.add("this");
list.add("thing");
final ArrayList<CheckBox> checkBoxArray = new ArrayList<>();
for(int i = 0; i < list.size(); i++){
checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);
checkBox.setId(i);
checkBox.setText(list.get(i));
checkBox.setTag(list.get(i));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Log.v("CheckBox - checked", buttonView.getTag().toString());
}else{
Log.v("CheckBox - unchecked", buttonView.getTag().toString());
}
}
});
checkBoxArray.add(checkBox);
checkboxContainer.addView(checkBox);
}
testing_layout.xml
<?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"
android:background="#color/colorWhite">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/checks"
android:orientation="vertical">
</LinearLayout>
<Button
android:id="#+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);
it's not an id, it's an style and you need to access it like this
R.style.checkboxStyle;
After creating a list view with XML drawables on the side of each list item, for some reason the image views are not appearing the way that was expected. For each item, only a small horizontal line appears at the top of each list item rather than the drawable also filling up the height of each list view item. What needs to be done in order to fix the height of the image?
drawable/ic_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="20dp" />
<solid android:color="#color/red"/>
</shape>
drawable/ic_yellow.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="20dp" />
<solid android:color="#color/yellow"/>
</shape>
drawable/ic_green.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="20dp" />
<solid android:color="#color/green"/>
</shape>
fragment_helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list_helloworld"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
listitem_helloworld.xml
<?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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/item_img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="5dp"
android:paddingEnd="5dp"/>
<TextView
android:id="#+id/item_colourtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:layout_toEndOf="#id/item_img"/>
</RelativeLayout>
FragmentHelloWorld.java
public class FragmentHelloWorld extends android.support.v4.app.Fragment {
public FragmentHelloWorld() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_helloworld, container, false);
// Array of strings for ListView
String[] listviewTitle = new String[]{
"Item 1",
"Item 2",
"Item 3"
};
// Array of images for ListView
int[] listviewImage = new int[]{
R.drawable.ic_red,
R.drawable.ic_yellow,
R.drawable.ic_green
};
List<HashMap<String, String>> aList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
HashMap<String, String> hm = new HashMap<>();
hm.put("listview_title", listviewTitle[i]);
hm.put("listview_image", Integer.toString(listviewImage[i]));
aList.add(hm);
}
String[] from = {"listview_image", "listview_title"};
int[] to = {R.id.item_img, R.id.item_colourtitle};
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listitem_helloworld, from, to);
ListView list_helloworld = (ListView) v.findViewById(R.id.list_helloworld);
list_helloworld.setAdapter(simpleAdapter);
}
You may want to take a look at this post here
Or you may try specifying a rectangular shape to the drawables and use LinearLayout , that would be simple.
Using Android Studio, I get this error in my activity layout : Error:(9) No resource identifier found for attribute 'headerView' in package 'com.merahjambutech.zuki.deteksi'
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:compat="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.merahjambutech.zuki.deteksi.view.PullToZoomListViewEx
android:id="#+id/paralax_social_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
app:headerView="#layout/header_parallax_social" />
</LinearLayout>
I'm very sure the layout header_parallax_social.xml is available in my project files (res/layout), here's the code of header_parallax_social:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="160dp" >
<ImageView
android:id="#+id/header_parallax_social_new_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/cd_main_image"
android:scaleType="centerCrop"
android:src="#drawable/parallax_social_small" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
I have tried to change xmlns:app and anything like that, but still not found solution...
You have to set custom attribute i.e. headerView for your Listview in attrs.xml in values folder :
attrs.xml
<resources>
<declare-styleable name="PullToZoomListViewEx"> declare your custom listview class name here
<attr name="headerView" format="reference"/>
</declare-styleable>
</resources>
By doing this i hope app:headerView="#layout/header_parallax_social" will not show any error but to show header view in a listview you have to do some changes in your custom Listview class and it should looks like
public class PullToZoomListViewEx extends ListView{
private int headerId;
public PullToZoomListViewEx(Context context) {
super(context);
}
public PullToZoomListViewEx(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PullToZoomListViewEx(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PullToZoomListViewEx, defStyle, defStyle);
try {
headerId = a.getResourceId(R.styleable.PullToZoomListViewEx_headerView, View.NO_ID);
if (headerId != View.NO_ID) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header = inflater.inflate(headerId, null);
addHeaderView(header);
}
} finally {
a.recycle();
}
}
}
or
If you want to avoid above efforts, You can programmatically set a header view to a Listview like this :
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header,myListView, false);
//replace R.layout.header with R.layout.header_parallax_social and myListView with your listview object
myListView.addHeaderView(header, null, false);
Hope this helps.
I'm new in Android. I'm trying to set up a simple TabHost view.
This is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout 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" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#+id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#160203" />
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
</FrameLayout>
And here is my java code:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
public class Tabhost extends FragmentActivity {
private FragmentTabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabhost);
tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Articulos"),
ArticulosFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tutoriales"),
TutorialesFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Cursos"),
CursosFragment.class, null);
}
}
But the app crash with this error message (I really don't know that it means):
What do you think could be wrong?
Use instead:
<android.support.v4.app.FragmentTabHost
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
(...)
</android.support.v4.app.FragmentTabHost>