I'm new in android.I'm using listview which contains images and textview.I want to add arrow icon in each row.Im trying but i cannot accomplish this.How could i do this? The code is below.Any help is highly appreciated.
CategoryActivity.java
public class CategoryActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.category1);
ImageView in1 = (ImageView) findViewById(R.id.glrrr1);
in1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),ImageSwitcherA.class);
startActivityForResult(myIntent, 0);
}
});
ImageView in2 = (ImageView) findViewById(R.id.grid1);
in2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent1 = new Intent(view.getContext(),RingGridView.class);
startActivityForResult(myIntent1, 0);
}
});
ListView view = (ListView) findViewById(R.id.list);
//instance of custom adapter
view.setAdapter(new CustomImageListAapter(this));
view.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Intent i = new Intent(AndroidGridLayoutActivity.this,FullImageActivity.class);
//Bundle bundle = new Bundle();
// bundle.putInt("operation", position);
//i.putExtras(bundle);
//startActivity(i);
if(position ==0)
{
Intent ii = new Intent(CategoryActivity.this,Ring1Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
ii.putExtra("operation", position);
startActivity(ii);
}
if(position ==1)
{
Intent in = new Intent(CategoryActivity.this,Ring2Full.class);
//Bundle bundle = new Bundle();
// bundle.putInt("operation", position);
in.putExtra("operation", position);
startActivity(in);
}
if(position ==2)
{
Intent inn = new Intent(CategoryActivity.this,Ring3Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
inn.putExtra("operation", position);
startActivity(inn);
}
if(position ==3)
{
Intent innm = new Intent(CategoryActivity.this,Ring4Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
innm.putExtra("operation", position);
startActivity(innm);
}
if(position ==4)
{
Intent intt = new Intent(CategoryActivity.this,Ring5Full.class);
//Bundle bundle = new Bundle();
// bundle.putInt("operation", position);
intt.putExtra("operation", position);
startActivity(intt);
}
if(position ==5)
{
Intent intt5 = new Intent(CategoryActivity.this,Ring6Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
intt5.putExtra("operation", position);
startActivity(intt5);
}
if(position == 6)
{
Intent buset= new Intent(CategoryActivity.this,Ring7Full.class);
// Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
buset.putExtra("operation", position);
startActivity(buset);
}
if(position == 7)
{
Intent buses= new Intent(CategoryActivity.this,Ring8Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
buses.putExtra("operation", position);
startActivity(buses);
}
if(position == 8)
{
Intent busez= new Intent(CategoryActivity.this,Ring9Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
busez.putExtra("operation", position);
startActivity(busez);
}
if(position == 9)
{
Intent buseh= new Intent(CategoryActivity.this,Ring10Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
buseh.putExtra("operation", position);
startActivity(buseh);
}
if(position == 10)
{
Intent busek1= new Intent(CategoryActivity.this,Ring11Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
busek1.putExtra("operation", position);
startActivity(busek1);
}
if(position == 11)
{
Intent busek2= new Intent(CategoryActivity.this,Ring12Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
busek2.putExtra("operation", position);
startActivity(busek2);
}
if(position == 12)
{
Intent busek3= new Intent(CategoryActivity.this,Ring13Full.class);
//Bundle bundle = new Bundle();
// bundle.putInt("operation", position);
busek3.putExtra("operation", position);
startActivity(busek3);
}
if(position == 13)
{
Intent busek4= new Intent(CategoryActivity.this,Ring14Full.class);
//Bundle bundle = new Bundle();
// bundle.putInt("operation", position);
busek4.putExtra("operation", position);
startActivity(busek4);
}
if(position == 14)
{
Intent busek5= new Intent(CategoryActivity.this,Ring16Full.class);
// Bundle bundle = new Bundle();
// bundle.putInt("operation", position);
busek5.putExtra("operation", position);
startActivity(busek5);
}
if(position == 15)
{
Intent busek6= new Intent(CategoryActivity.this,Ring16Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
busek6.putExtra("operation", position);
startActivity(busek6);
}
if(position == 16)
{
Intent busek7= new Intent(CategoryActivity.this,Ring17Full.class);
// Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
busek7.putExtra("operation", position);
startActivity(busek7);
}
if(position == 17)
{
Intent busek8= new Intent(CategoryActivity.this,Ring18Full.class);
//Bundle bundle = new Bundle();
//bundle.putInt("operation", position);
busek8.putExtra("operation", position);
startActivity(busek8);
}
}
});}}
CustomImageListAapter.java
public class CustomImageListAapter extends BaseAdapter {
private int[] images = {
R.drawable.rrr1,
R.drawable.rrr2,
R.drawable.rrr3,
R.drawable.rrr4,
R.drawable.rrr5,
R.drawable.rrr6,
R.drawable.rrr7,
R.drawable.rrr8,
R.drawable.rrr18,
R.drawable.rrr10,
R.drawable.rrr11,
R.drawable.rrr12,
R.drawable.rrr13,
R.drawable.rrr14,
R.drawable.rrr15,
R.drawable.rrr16,
R.drawable.rrr17,
R.drawable.rrr18,
};
private String[] imageDesc = { "Diamond Ring", "Silver Ring",
"Gold Ring","Antique Ring","Pearl Ring","Beats Ring","Diamond Ring","Stone Ring","Antique Ring","Diamond Ring", "Silver Ring",
"Gold Ring","Antique Ring","Pearl Ring","Beats Ring","Diamond Ring","Stone Ring","Antique Ring"};
Context ctx = null;
public CustomImageListAapter(Context context) {
this.ctx = context;
}
public int getCount() {
return images.length;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView imgView = new ImageView(this.ctx);
imgView.setScaleType(ScaleType.FIT_CENTER);
imgView.setPadding(8, 8, 8, 8);
imgView.setImageResource(images[arg0]);
imgView.setAdjustViewBounds(Boolean.TRUE);
imgView.setContentDescription(imageDesc[arg0]);
imgView.setMaxHeight(200);
imgView.setMaxWidth(200);
TextView tv = new TextView(this.ctx);
tv.setText(imageDesc[arg0]);
tv.setMaxHeight(100);
tv.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tv.setGravity(Gravity.CENTER);
LinearLayout layoutView = new LinearLayout(this.ctx);
layoutView.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(150, 150);
layoutView.addView(imgView, params1);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutView.addView(tv, params2);
return layoutView;
}
}
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:id="#+id/background"
android:gravity="center_vertical"
android:orientation="vertical" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
android:background="#drawable/bkg">
</ListView>
</LinearLayout>
LinearLayout layoutView = new LinearLayout(this.ctx);
layoutView.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(150, 150);
layoutView.addView(imgView, params1);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
150 ,LayoutParams.WRAP_CONTENT);
layoutView.addView(tv, params2);
ImageView i1= new ImageView(this.ctx);
i1.setImageResource(R.drawable.ic_launcher);
layoutView.addView(i1);
return layoutView;
try that one get view add one imageview to that layout(layoutView)
First you will have to define a custom layout for the row of the ListView. The following code has an ImageView on the left side and then the title and sub-title with an arrow icon on the right side.
ListRow.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="wrap_content"
android:orientation="horizontal"
android:padding="5sp" >
<!-- Left side Thumbnail image -->
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5sp"
android:layout_alignParentLeft="true"
android:background="#drawable/image_bg"
android:layout_marginRight="5sp">
<ImageView
android:id="#+id/list_image"
android:layout_width="50sp"
android:layout_height="50sp"
/>
</LinearLayout>
<!-- Title-->
<TextView
android:id="#+id/title_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textStyle="bold"/>
<!-- Subtitle -->
<TextView
android:id="#+id/subtitle_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title_name"
android:layout_marginTop="2sp"
android:layout_toRightOf="#+id/thumbnail"
/>
<!-- Rightend Arrow -->
<ImageView android:contentDescription="#string/right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Now you will have to create a Custom Adapter for displaying your custom ListView which will inflate the ListRow.xml and display the data in your list. You can also visit the following link for creating your layout:-
android-custom-listview-with-image-and-text
Hope this helps. Thanks.
Related
I have listview which contains textview and a checkbox.
I am showing the ListView in a DialogFragment. The problem is that the checkboxes do not show up for devices(emulator) running Android version 4.1 to 4.4.4. But they show up normally for Android 5.0+. I have been trying to fix this for hours. But no success.
Can someone please tell me what am I doing wrong?
[]
Listview:
<RelativeLayout
android:id="#+id/rl_cats_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_game_info">
<ListView
android:id="#+id/lv_select_cats"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:fadeScrollbars="false" />
<TextView
android:id="#+id/tv_overlay_pro"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:background="#A6000000"
android:gravity="center"
android:text="Available with \n'Quiz Time Pro'"
android:textColor="#color/android_green"
android:textSize="24sp" />
</RelativeLayout>
Listview row:
<?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">
<CheckBox
android:id="#+id/cb_row_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:id="#+id/tv_row_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cb_row_item"
android:layout_alignBottom="#+id/cb_row_item"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_toEndOf="#+id/cb_row_item"
android:layout_toRightOf="#+id/cb_row_item"
android:text="Category name goes here"
android:textSize="15sp" />
</RelativeLayout>
DialogFragment Class inflating the listview:
public class SelectGameModeDialogFragment extends DialogFragment {
boolean[] checkedCategories = new boolean[Constants.CATEGORIES.length + 1];
boolean[] checkedDifficulty = new boolean[Constants.DIFFICULTY_LEVELS.length + 1];
List<String> categories = new ArrayList<>();
List<String> difficultyLevels = new ArrayList<>();
TextView gameInfoTV;
Spinner spinner;
public static SelectGameModeDialogFragment newInstance(String title) {
SelectGameModeDialogFragment frag = new SelectGameModeDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
frag.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
return frag;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
categories.addAll(Arrays.asList(Constants.CATEGORIES));
difficultyLevels.addAll(Arrays.asList(Constants.DIFFICULTY_LEVELS));
return inflater.inflate(R.layout.select_game_mode, container);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final ListView lvCategories = (ListView) view.findViewById(R.id.lv_select_cats);
gameInfoTV = (TextView) view.findViewById(R.id.tv_game_info);
gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[0]);
final TextView proTV = (TextView) view.findViewById(R.id.tv_overlay_pro);
final Button btnPlay = (Button) view.findViewById(R.id.btn_restart_game);
spinner = (Spinner) view.findViewById(R.id.spinner_gamer_categories);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.game_types_spinner, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == Constants.MODE_MILLIONAIRE
|| pos == Constants.MODE_UNLIMITED
|| pos == Constants.MODE_20QUESTIONS
|| pos == Constants.MODE_60SECONDS) {
ViewGroup.LayoutParams params = lvCategories.getLayoutParams();
params.height = 0;
lvCategories.setLayoutParams(params);
proTV.setVisibility(View.GONE);
} else if (pos == Constants.MODE_CATEGORIES) {
MultipleCategoryListAdapter adapter = new MultipleCategoryListAdapter(getActivity(), categories);
lvCategories.setAdapter(adapter);
ViewGroup.LayoutParams params = lvCategories.getLayoutParams();
params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
lvCategories.setLayoutParams(params);
} else if (pos == Constants.MODE_DIFFICULTY) {
SingleCategoryListAdapter adapter = new SingleCategoryListAdapter(getActivity(), difficultyLevels);
lvCategories.setAdapter(adapter);
ViewGroup.LayoutParams params = lvCategories.getLayoutParams();
params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
lvCategories.setLayoutParams(params);
}
lvCategories.setVisibility(View.INVISIBLE);
if (pos == Constants.MODE_MILLIONAIRE) {
gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[0]);
} else if (pos == Constants.MODE_20QUESTIONS) {
gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[1]);
} else if (pos == Constants.MODE_CATEGORIES) {
gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[2]);
lvCategories.setVisibility(View.VISIBLE);
} else if (pos == Constants.MODE_60SECONDS) {
gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[3]);
} else if (pos == Constants.MODE_UNLIMITED) {
gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[4]);
} else if (pos == Constants.MODE_DIFFICULTY) {
gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[5]);
lvCategories.setVisibility(View.VISIBLE);
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int spinnerPosition = spinner.getSelectedItemPosition();
ArrayList<String> cat = new ArrayList<>();
if (spinnerPosition == Constants.MODE_MILLIONAIRE) {
goToPlayingFragmentWithCategories(cat, Constants.MODE_MILLIONAIRE);
} else if (spinnerPosition == Constants.MODE_20QUESTIONS) {
goToPlayingFragmentWithCategories(cat, Constants.MODE_20QUESTIONS);
} else if (spinnerPosition == Constants.MODE_CATEGORIES) {
for (int i = 0; i < checkedCategories.length; i++) {
if (checkedCategories[i]) {
cat.add(categories.get(i));
}
}
goToPlayingFragmentWithCategories(cat, Constants.MODE_CATEGORIES);
Arrays.fill(checkedCategories, false);
System.out.println();
} else if (spinnerPosition == Constants.MODE_60SECONDS) {
goToPlayingFragmentWithCategories(cat, Constants.MODE_60SECONDS);
} else if (spinnerPosition == Constants.MODE_UNLIMITED) {
goToPlayingFragmentWithCategories(cat, Constants.MODE_UNLIMITED);
} else if (spinnerPosition == Constants.MODE_DIFFICULTY) {
String diff = "";
for (int i = 0; i < 3; i++) {
if (checkedDifficulty[i]) {
diff = difficultyLevels.get(i);
}
}
goToPlayingFragmentWithCategories(cat, Constants.MODE_DIFFICULTY, diff);
}
dismiss();
}
});
Button btnBack = (Button) view.findViewById(R.id.btn_game_mode_back);
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
}
});
}
why not just use the text of the CheckBox?
That way your ListView item could just be
<?xml version="1.0" encoding="utf-8"?>
<CheckBox
android:id="#+id/cb_row_item"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Category name goes here"
android:textSize="15sp"/>
I have found the problem. The checkbox color for Android 4.x is black, so it was blending with the black background. Fixed it by using AppCompatCheckBox instead of CheckBox.
hello guys I'm looking how to make my ListView clickable , i searched in the net but i haven't found the right answer , and this is my code please help me
`public class acceuil extends AppCompatActivity {
ListView listView;
int [] movie_poster_resource = {R.drawable.profil};
String[] patient_names;
String[] temps_rendez;
MovieAdapter adapter;
View view;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acceuil);
listView= (ListView)findViewById(R.id.listView);
temps_rendez = getResources().getStringArray(R.array.temps);
patient_names = getResources().getStringArray(R.array.patient_title);
int i=0;
adapter = new MovieAdapter(getApplicationContext(),R.layout.patient_name);
listView.setAdapter(adapter);
for (String titles: patient_names)
{
MovieDataProvider dataProvider = new MovieDataProvider(movie_poster_resource[i],titles,temps_rendez[i]);
adapter.add(dataProvider);
}
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
if (id == 0)
startActivity(new Intent(this, patient_from_listview.class));
}
public void open_messagerie (View view){
startActivity(new Intent(this, acceuil.class));
}
public void openn_otification (View view){
startActivity(new Intent(this, acceuil.class));
}
public void opena_parametre (View view){
startActivity(new Intent(this, acceuil.class));
}
public void open_calcule (View view){
startActivity(new Intent(this, acceuil.class));
}
}`
You'd call listView.setOnItemClickListener(OnItemClickListener). That sets the class to be called when an item is clicked. It looks like you've already implemented the onItemClicked function, this will hook it up.
This is how you can achieve this
adapter = new MovieAdapter(getApplicationContext(),R.layout.patient_name);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
// HERE YOU CAN MAKE CASES FOR EACH CLICK
}
Here is my code, how to do clickable listview on Android Studio...
MAIN ACTIVICTY CODE
ListView listView;
int mImage[] = {R.drawable.switzerland, R.drawable.canada, R.drawable.japan, R.drawable.usa};
String mTitle[] = {"Switzerland Title", "Canada Title", "Japan Title", "USA Title"};
String mDescription[] = {"Switzerland is a mountainous Central European country, home to numerous lakes, villages and the high peaks of the Alps.", "Canada is a country in the northern part of North America. Its ten provinces and three territories extend from the Atlantic to the Pacific.", "Japan is an island country in East Asia, located in the northwest Pacific Ocean.", "The U.S. is a country of 50 states covering a vast swath of North America, with Alaska in the northwest."};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// for listview settings
listView = findViewById(R.id.listview);
MyAdapter adapter = new MyAdapter(this, mImage, mTitle, mDescription);
listView.setAdapter(adapter);
//listview click listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if (position==0){
Intent intent = new Intent(getApplicationContext(), Switzerland.class);
Bundle bundle = new Bundle();
bundle.putInt("image", mImage[0]);
intent.putExtras(bundle);
intent.putExtra("title", mTitle[0]);
intent.putExtra("description", mDescription[0]);
intent.putExtra("position", ""+0);
startActivity(intent);
}else if (position==1){
Intent intent = new Intent(getApplicationContext(), Canada.class);
Bundle bundle = new Bundle();
bundle.putInt("image", mImage[1]);
intent.putExtras(bundle);
intent.putExtra("title", mTitle[1]);
intent.putExtra("description", mDescription[1]);
intent.putExtra("position", ""+1);
startActivity(intent);
}else if (position==2){
Intent intent = new Intent(getApplicationContext(), Japan.class);
Bundle bundle = new Bundle();
bundle.putInt("image", mImage[2]);
intent.putExtras(bundle);
intent.putExtra("title", mTitle[2]);
intent.putExtra("description", mDescription[2]);
intent.putExtra("position", ""+2);
startActivity(intent);
}else if (position==3){
Intent intent = new Intent(getApplicationContext(), USA.class);
Bundle bundle = new Bundle();
bundle.putInt("image", mImage[3]);
intent.putExtras(bundle);
intent.putExtra("title", mTitle[3]);
intent.putExtra("description", mDescription[3]);
intent.putExtra("position", ""+3);
startActivity(intent);
}
}
});
}
// for listview adapter
class MyAdapter extends ArrayAdapter<String> {
Context context;
int sImage[];
String sTitle[];
String sDescription[];
MyAdapter (Context c, int image[], String title[], String description[]){
super(c, R.layout.main_page_row, R.id.main_page_title, title);
this.context = c;
this.sImage = image;
this.sTitle = title;
this.sDescription = description;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view= layoutInflater.inflate(R.layout.main_page_row, parent, false);
ImageView imageView = view.findViewById(R.id.main_page_image);
TextView titleText = view.findViewById(R.id.main_page_title);
TextView descriptionText = view.findViewById(R.id.main_page_description);
imageView.setImageResource(sImage[position]);
titleText.setText(sTitle[position]);
descriptionText.setText(sDescription[position]);
return view;
}
}
Here is MAIN ACTIVITY XML Code
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1500dp"
android:orientation="vertical">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Here is MAIN ACTIVITY ROW XML Code
<androidx.cardview.widget.CardView
android:orientation="vertical"
app:cardElevation="5dp"
app:cardCornerRadius="12dp"
android:layout_margin="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageID"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/erroricon"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/titleID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="#string/bb_name"
android:textSize="18sp"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/descriptionID"
android:text="#string/mp_des"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="10dp"
android:textColor="#000000"
android:textSize="16sp"
android:ellipsize="end"
android:maxLines="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
Here is Details Page Java Code
ImageView image;
TextView sTitle, sDescription;
int position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_page);
image = findViewById(R.id.details_page_image);
sTitle = findViewById(R.id.details_page_title);
sDescription = findViewById(R.id.details_page_description);
if (position == 0){
Intent intent = getIntent();
Bundle bundle = this.getIntent().getExtras();
int picture = bundle.getInt("image");
String postTitle = intent.getStringExtra("title");
String postDescrip = intent.getStringExtra("description");
image.setImageResource(picture);
sTitle.setText(postTitle);
sDescription.setText(postDescrip);
}
if (position == 1){
Intent intent = getIntent();
Bundle bundle = this.getIntent().getExtras();
int picture = bundle.getInt("image");
String postTitle = intent.getStringExtra("title");
String postDescrip = intent.getStringExtra("description");
image.setImageResource(picture);
sTitle.setText(postTitle);
sDescription.setText(postDescrip);
}
if (position == 2){
Intent intent = getIntent();
Bundle bundle = this.getIntent().getExtras();
int picture = bundle.getInt("image");
String postTitle = intent.getStringExtra("title");
String postDescrip = intent.getStringExtra("description");
image.setImageResource(picture);
sTitle.setText(postTitle);
sDescription.setText(postDescrip);
}
if (position == 3){
Intent intent = getIntent();
Bundle bundle = this.getIntent().getExtras();
int picture = bundle.getInt("image");
String postTitle = intent.getStringExtra("title");
String postDescrip = intent.getStringExtra("description");
image.setImageResource(picture);
sTitle.setText(postTitle);
sDescription.setText(postDescrip);
}
}
I need to put one listview with cursorloader above and down one litsview with static data. This is my layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:orientation="vertical">
<TextView
android:id="#+id/header_mi_facultad"
android:text="#string/header_list_mi_facultad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textStyle="bold"
android:textSize="22sp"
android:textColor="#FFFFFF"
android:padding="10dp"
android:background="#336699"
/>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView
android:id="#+id/header_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:text="#string/header_list_servicios"
android:textStyle="bold"
android:textSize="22sp"
android:textColor="#FFFFFF"
android:padding="10dp"
android:background="#336699"
/>
<ListView
android:id="#+id/list_servicios"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/servicios_array"
>
</ListView>
</LinearLayout>
the snippet of the fragment where i set the adapters
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(LOG_TAG, "---Se creo la vista del MainActivityFragment---");
View rootView = inflater.inflate(R.layout.list_main, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.list_view);
listServiciosView = (ListView) rootView.findViewById(R.id.list_servicios);
serviciosAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, servicios);
listServiciosView.setAdapter(serviciosAdapter);
menuAdapter = new MenuAdapter(getActivity(), null, 0);
listView.setAdapter(menuAdapter);
ListUtils.setDynamicHeight(listView);
ListUtils.setDynamicHeight(listServiciosView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
int index = cursor.getColumnIndex(AppContract.MenuEntry.COLUMN_TYPE);
int type = cursor.getInt(index);
Intent intent;
Log.d(LOG_TAG, "id : " + id + " position : " + position);
Log.d(LOG_TAG, "tipo : " + type);
switch (type) {
case NOTICIA_LISTA:
index = cursor.getColumnIndex(AppContract.MenuEntry._ID);
Long idMenu = cursor.getLong(index);
Log.d(LOG_TAG, "url : " + idMenu);
intent = new Intent(view.getContext(), ListActivity.class);
intent.putExtra("idSolicitado", idMenu);
intent.putExtra("idOrigen", 0);
startActivity(intent);
break;
case NOTICIA_URL:
index = cursor.getColumnIndex(AppContract.MenuEntry.COLUMN_URL);
String url = cursor.getString(index);
Log.d(LOG_TAG, "url : " + url);
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
break;
case SERVICIO_CUOTA:
intent = new Intent(view.getContext(), PayActivity.class);
startActivity(intent);
break;
default:
Log.d(LOG_TAG, "Nada!!!");
break;
}
}
});
listServiciosView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent;
Log.d(LOG_TAG, "id : " + id + " position : " + position);
switch (position) {
case 0:
intent = new Intent(view.getContext(), PayActivity.class);
startActivity(intent);
break;
default:
Log.d(LOG_TAG, "Nada!!!");
break;
}
}
});
return rootView;
}
the utility class that i tried
public static class ListUtils {
public static void setDynamicHeight(ListView mListView) {
ListAdapter mListAdapter = mListView.getAdapter();
if (mListAdapter == null) {
// when adapter is null
return;
}
int height = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
for (int i = 0; i < mListAdapter.getCount(); i++) {
View listItem = mListAdapter.getView(i, null, mListView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = mListView.getLayoutParams();
params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
mListView.setLayoutParams(params);
mListView.requestLayout();
}
}
Here are the screenshots url
sc1
sc2
it works with portrait orientation, but with landscape the firts listview only show the firts element. I want to show all the elements in it. How i can do that? I tried this here and it works with static data but in my case have the problem explained. Is there a problem with the cursor loader? or i miss something in the layout?
are you saving all your variable sate using SaveInstanceState method.
I am trying to make this gridview clickable. Everything works fine but the last cell. When I click on the last cell i.e. 8th cell, the program crashes. Any suggestion as to why it crashes will helpful.
public class MainView extends Activity {
ImageView back, home, site;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainview);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent myIntent = null;
if(position == 0){
myIntent = new Intent(v.getContext(), TheShow.class);
}
if(position == 1){
myIntent = new Intent(v.getContext(), Exhibitor.class);
}
if(position ==2){
myIntent = new Intent(v.getContext(), Visitor.class);
}
if(position ==3){
myIntent = new Intent(v.getContext(), Conference.class);
}
if(position ==4){
myIntent = new Intent(v.getContext(), ContactUs.class);
}
if(position == 5){
myIntent = new Intent(v.getContext(), VisitorReg.class);
}
if(position == 6){
myIntent = new Intent(v.getContext(), ExhibitorReg.class);
}
if(position == 7){
Toast.makeText(getApplicationContext(), "Pending", 4000).show();
}
startActivity(myIntent);
}
});
}
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("The Show", R.drawable.globecopy1));
items.add(new Item("Exhibitor", R.drawable.workcopy1));
items.add(new Item("Visitor", R.drawable.visitor1copy1));
items.add(new Item("Conference", R.drawable.conferencecopy1));
items.add(new Item("Contact Us", R.drawable.contactus1copy1));
items.add(new Item("Visitor Registration", R.drawable.registercopy1));
items.add(new Item("Exhibitor Registration", R.drawable.exregistercopy1));
items.add(new Item("Delegate Registration", R.drawable.anothercopy1));
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i)
{
return items.get(i);
}
#Override
public long getItemId(int i)
{
return items.get(i).drawableId;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item
{
final String name;
final int drawableId;
Item(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backg" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/header" />
<GridView
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="500dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:layout_above="#+id/TableLayout1"
android:layout_gravity="bottom"
android:layout_margin="10dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="4dp"
android:background="#00FF0000"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
<TableLayout
android:id="#+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:stretchColumns="*" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#505050" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="#drawable/bottom1" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/bottom2" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:src="#drawable/bottom3" />
</TableRow>
</TableLayout>
</RelativeLayout>
Your myIntent is null in the last condition. That's why crash is happening. So check if the myIntent is null before starting it. Try the following:
if(myIntent!=null){
startActivity(myIntent);
}
Here in grid there is 8 items and As you said you are selecting last postion i.e. no 7 position . and you have declare Intent as a null and while checking condition you didnt set the value of your Intent.
if(position == 7)
{
Toast.makeText(getApplicationContext(), "Pending", 4000).show();
}
Replace above code with this
if(position == 7)
{
myIntent = new Intent(v.getContext(), YOUR_CLASS_NAME.class);
}
Click on search item inside edittext redirect on wrong activity, it doesn't open the activity associated with it. It opening other activities that are associated with other listitems but not that i am clicking one.
Here is my complete code:
public class Tabtwo extends Activity implements OnItemClickListener {
ListView listView;
TextView txt;
ArrayAdapter<String> adapter;
// Search EditText
EditText edtSearch;
// Array of strings storing country names
String[] countries = new String[] { "Admin Cost", "Affinity Diagram",
"Analyse", "Apprasal Costs", "Assessment of Stakeholders",
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[] { R.drawable.admin, R.drawable.affinity,
R.drawable.analysis, R.drawable.appraisal, R.drawable.assessment,
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabtwo);
// Each row in the list stores country name, currency and flag
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 4; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("txt", countries[i]);
hm.put("flag", Integer.toString(flags[i]));
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag", "txt" };
// Ids of views in listview_layout
int[] to = { R.id.flag, R.id.txt };
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = (ListView) findViewById(R.id.listview);
edtSearch = (EditText) findViewById(R.id.Search_box);
txt = (TextView) findViewById(R.id.txt);
listView.setOnItemClickListener(this);
// Setting the adapter to the listView
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(this);
edtSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
if (position == 0) {
Intent int0 = new Intent(getApplicationContext(), Admincost.class);
startActivity(int0);
}
if (position == 1) {
Intent int1 = new Intent(getApplicationContext(), Affinity.class);
startActivity(int1);
}
if (position == 2) {
Intent int2 = new Intent(getApplicationContext(), Analyse.class);
startActivity(int2);
}
if (position == 3) {
Intent int3 = new Intent(getApplicationContext(),
ApprasalCosts.class);
startActivity(int3);
}
if (position == 4) {
Intent int1 = new Intent(getApplicationContext(), Assessment.class);
startActivity(int1);
} }
}
}
Here is my tabtwo xml file.
<?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" >
<EditText
android:id="#+id/Search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Search a Item from ListView"
android:inputType="textVisiblePassword" />
/>
<TextView
android:id="#+id/List_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="12dip"
android:textSize="17sp"
android:textStyle="bold" />
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-50dp" />
</LinearLayout>
Here is listview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" />
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="21dp" />
</LinearLayout>
Replace your onItemClick method with below and try.. Hope it works..
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
TextView tv = (TextView) arg1.findViewById(R.id.txt);
String str = tv.getText().toString().trim();
if (str.equals(countries[0])) {
Intent int0 = new Intent(Tabtwo.this, Admincost.class);
startActivity(int0);
}else if(str.equals(countries[1])) {
Intent int1 = new Intent(Tabtwo.this, Affinity.class);
startActivity(int1);
}else if(str.equals(countries[2])) {
Intent int2 = new Intent(Tabtwo.this, Analyse.class);
startActivity(int2);
}else if(str.equals(countries[3])) {
Intent int3 = new Intent(Tabtwo.this, ApprasalCosts.class);
startActivity(int3);
}else if(str.equals(countries[4])) {
Intent int1 = new Intent(Tabtwo.this, Assessment.class);
startActivity(int1);
}
}
Add this code to your TextView as well as to ImageView
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
and for ItemClick you can use the answer of Tamilan
I meet the same question with you. Try the following one:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
//(String) getListAdapter().getItem(position);
String city_id=map.get( (String) adapter.getItem(position) );
Toast.makeText(getApplicationContext(),city_id, Toast.LENGTH_SHORT).show();
Intent i=new Intent(MainActivity.this,DetailActivity.class);
i.putExtra("city_id",city_id);
startActivity(i);
}
});