How to inflate the icon image in my layout? - android

I am trying to add a small home icon image to top of my list view.Below you can see the code of my adapter. It works perfectly for setting dynamic text but it fails when i want to add an image.
I do not get any runtime error, just the images aren't loaded.
The images referenced are 35x35 png file.
How to implement this? Im new to android. please guide me.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewList = Nexttopic.this.getLayoutInflater().inflate(R.layout.activity_nexttopic, null);
dialogMarketList = new Dialog(Nexttopic.this);
dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogMarketList.setContentView(viewList);
dialogMarketList.show();
lvForDialog = (ListView) viewList.findViewById(R.id.List_view);
lvForDialog.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
ArrayAdapter<String> adapter = (new ArrayAdapter<String>(Nexttopic.this, R.layout.row_topic, R.id.child_row,tnamelist));
ImageView home = (ImageView) findViewById(R.id.hme);
lvForDialog.setAdapter(adapter);
row_topic xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<ImageView android:id="#+id/hme"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_above="#+id/child_row"
android:src="#drawable/homex"/>
<TextView
android:id="#+id/child_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="#drawable/bg"/>
</RelativeLayout>
activity_nexttopic
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Nexttopic" >
<ListView
android1:id="#+id/List_view"
android1:layout_width="match_parent"
android1:layout_height="250dp"
android1:layout_centerHorizontal="true"
android1:layout_centerVertical="true"
android:background="#drawable/bg">
</ListView>
</RelativeLayout>

Related

how to add my layout design in loop

I'm trying to add multiple layouts in one root layout using loop but I dont know how I can do that. My layout is code is
<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"
android:id="#+id/framlayout"
tools:context="layout.SettingFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linearLayout"
android:padding="10dp"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp"
android:id="#+id/linearLayout1"
android:background="#drawable/texview_design"
android:backgroundTint="#color/popup_tv_color"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Baatein Ye Kabhi Na"
android:gravity="center"
android:textColor="#android:color/holo_red_dark"
android:textSize="20dp"
android:textStyle="italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hindi Romantic"
android:paddingBottom="10dp"
android:layout_gravity="center_horizontal"/>
<RatingBar
android:layout_width="144dp"
android:layout_height="wrap_content"
android:id="#+id/ratingBar1"
android:layout_gravity="right" />
</LinearLayout>
</LinearLayout>
</ScrollView>
But i want
Custom List View is best option in your case
follow this link:
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html
or
do you want include layout conformly you can by adding following code
RelativeLayout rl = (RelativeLayout) findById(R.id.rl); //id of layout which layout you want add chields
LayoutInflater layoutInflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) ); //content_layout is your layout with tw0 textview and rating bar
Hoping it will work for you
Better to use listview which is easy to implement and build for this types of task.
But you can add like below
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 3; i++) {
View view = inflater.inflate(R.layout.yourLayoutId, null);
// Assume you want to add your view in linear layout
linearLayout.addView(view);
}

Android: Custom FlowLayout

I want to make this kind of menu of tags in android. Fill layout dynamically and align to center.
How can I do that?
Edited using library recommended by #Dory
<FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:f="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
f:debugDraw="false"
f:weightDefault="0"
f:layoutDirection="ltr"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:paddingRight="12dip"
android:paddingBottom="12dip"
android:background="#android:color/black"
android:id="#+id/l_flow"/>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"/>
Flowlayout in one xml file Button is in another xml file. I'm inflating Button then fLayout.addView(button);
what I get is this Padding top and bottom between views is higher than expected
finally I did it with help of this library. I have used only class FlowLayout and attr.xml My main.xml looks like this:
<com.example.FlowTest.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#android:color/white"/>
and my item layout looks like this:
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"/>
And in my Activity's onCreate method:
LayoutInflater mInflater = LayoutInflater.from(this);
mFlowLayout = (FlowLayout) findViewById(R.id.flow);
String [] names = {"goods", "shops", "cars", "washing machine","blablabla","clothes","books"};
for(int i = 0; i<names.length;i++){
Button b = (Button)mInflater.inflate(R.layout.item_flow,mFlowLayout, false);
b.setText(names[i]);
mFlowLayout.addView(b);
}
Take a look
here, you can use this library
Edit:
There is attributes provided in the where you can set horizontal and verticalspacing of the Flow Layout. See below sample xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:f="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FlowLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
f:horizontalSpacing="5dp"
f:verticalSpacing="5dp" />
</RelativeLayout>
Happy coding :)

How do I get a TextView to show up on top of a ListView?

I'm trying to display a TextView on top of a ListView from within a ListActivity, but the text is not showing up.
LayoutInflater inflater = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View layout = inflater.inflate( R.layout.main, null );
TextView myTextView = (TextView)layout.findViewById( R.id.myTextView );
myTextView.setVisibility( View.VISIBLE );
myTextView.setText( R.string.emptyList );
setContentView( layout );
Here is the main layout:
<?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 android:id="#+id/myTextView"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
I want an "Empty list" notice to go at the top of the screen in case the list view is empty.
Why dont you use setEmptyView for your ListView and do your notification that way.
<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:layout_width="fill_parent"
android:layout_height="300dip"
android:id="#+id/list_view" />
<TextView
android:id="#+id/empty_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List view is empty"
android:visibility="gone" />
</LinearLayout>
And call in your code.
ListView listView = (ListView) findViewById( R.id.list_view );
listView.setEmptyView( findViewById( R.id.empty_list_view ) );
listView.setAdapter(yourAdapter);
Check these for more info.
Correct use of setEmtpyView in AdapterView
http://www.technotalkative.com/android-listview-setemptyview/
You could use a FrameLayout instead of a LinearLayout, that way both the TextView and the ListView will share the same layout space (although not strictly necessary).
Then you can set the ListView empty view to myTextView, and it will take care of showing or hiding the empty view automatically for you.
I'm not sure what you mean regarding the TextView. Are you trying to place something above the list when the activity loads? In that case adding it above the list in the LinearLayout will do just fine.
To get the "empty list" notice you are referring to, make sure activity you are using extends ListActivity, not just Activity, and that the respective views are titled "list" and "empty."
<?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/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/empty_user_list"/>
</LinearLayout>

how define a image in listview android?

I have a layout with this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/fundo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/consoleListaSimples"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/listaSimples"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
and for which item from the list a have this 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" android:background="#drawable/bk_principal_small">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imagemStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/green"/>
<TextView
android:id="#+id/texto1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/padrao" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
and to create the itens I create a SimpleAdapter that I insert the data using the method ListAdapterCreater
this.adapter = new ListAdapterCreater(list.getContext(),itensList,
R.layout.list_simple_item,new String[] {"text","image"},
new int[]{R.id.texto1,R.id.imagemStatus});
to set the text works pretty, but doesn`t work for the image.
what have to be the value of image? or what should I change to works?
Refer the below links which have sample code snippets.
LINK1 it has one textview and one image view in each list item.
LINK2 it has two textview in each list item.
LINK3 it has two textview and one image view in each list item.
I believe you'll have to create new Adapter that extends BaseAdapter.
Here's a few tutorials for you:
http://blog.sptechnolab.com/2011/02/01/android/android-custom-listview-items-and-adapters/
http://w2davids.wordpress.com/android-listview-with-iconsimages-and-sharks-with-lasers/
If you look closely on those tutorials, the trick lies in how getView() work.

Android: addHeader to ListView

I'm trying to add a header to my listView but I see only black screen when activity launches
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincoverflow);
final ListView itemslist = (ListView)findViewById(R.id.itemslist);
LayoutInflater inflater = getLayoutInflater();
ViewGroup mTop = (ViewGroup)inflater.inflate(R.layout.main_header, itemslist, false);
itemslist.addHeaderView(mTop, null, false);
//some actions with header and list items
//example: coverflow_item_title is located in header(R.layout.main_header)
title = (TextView)findViewById(R.id.coverflow_item_title); //checked - is not null
CoverFlow coverFlow = (CoverFlow) findViewById(R.id.coverflow);
ImageAdapter coverImageAdapter = new ImageAdapter(this);
coverFlow.setAdapter(new ImageAdapter(this));
coverImageAdapter.createImages();
coverFlow.setAdapter(coverImageAdapter);
coverFlow.setOnItemSelectedListener(new CoverAdapterView.OnItemSelectedListener(){
...
adapter = new LazyAdapter(MainCoverflowActivity.this, tools, tools_images);
itemslist.setAdapter(adapter);
}
itemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
...
}
}
maincoverflow.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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/itemslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
main_header.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:orientation="vertical" >
<TextView
android:id="#+id/coverflow_item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<com.thumbsupstudio.mobitour.CoverFlow
android:id="#+id/coverflow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/shadowbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/shadow_box" />
</LinearLayout>
You may notice that I'm using Coverflow widget (from here http://www.inter-fuser.com/2010/01/android-coverflow-widget.html) - it works fine.
In general, I'm changing listView content when Coverflow item is changed. It works fine, but I need Coverflow to scroll with listView, so I decided to attach it to header. After that, there's no listView and Coverflow on the screen, everything is black.
try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<TextView
android:id="#+id/coverflow_item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<com.thumbsupstudio.mobitour.CoverFlow
android:id="#+id/coverflow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/shadowbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/shadow_box" />
</RelativeLayout>
<ListView
android:id="#+id/itemslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
I found a solution. I don't fully understand WHY but:
NOT CORRECT
init listView
inflate header and add it to the list
set list adapter in Coverflow onItemSelected handler (it launch automatically with item position=0)
CORRECT
init listView
inflate header and add it to the list
set any adapter to listView (!!!)
set listView adapter in Coverflow onItemSelected handler (it launch automatically with item position=0)
after activity launches listView has adapter content which was set in Coverflow onItemSelected handler. What for is first adapter assignment - I don't understand.

Categories

Resources