android tab content does not show up - android-tabs

I'm currently learning to build app for Android, and I use Android Studio to do so.
I searched for similar posts but did not find what could help me.
My goal is to have an Activity with a tabHost and 4 Tabs (for the moment). I will put the code after giving some details.
To begin, I'm just focused on two tabs on which I would like to:
- put a button to start (on the first Tab) a dedicated activity to create an item (contact item),
- Display the Contact list on the second Tab.
My problem : The button is correctly visible and works fine (I can create a Contact). i also have a function checking wheter a contact already exists or not (correctly working).
What's not working is when I select the second Tab, my list won't show up and I can't see my contacts.
This my General Activity :
package avappmobile.fourwe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class General extends Activity {
List<Contact> Contacts = new ArrayList<Contact>();
List<Stuff> Stuffs = new ArrayList<Stuff>();
List<Money> Moneys = new ArrayList<Money>();
ListView contactListView;
DatabaseContactHandler dbContactHandler;
DatabaseMoneyHandler dbMoneyHandler;
DatabaseStuffHandler dbStuffHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_general);
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
contactListView = (ListView) findViewById(R.id.listViewContact);
dbContactHandler = new DatabaseContactHandler(getApplicationContext());
dbMoneyHandler = new DatabaseMoneyHandler(getApplicationContext());
dbStuffHandler = new DatabaseStuffHandler(getApplicationContext());
tabHost.setup();
TabHost.TabSpec tabSpec = tabHost.newTabSpec("home");
tabSpec.setContent(R.id.tabHome);
tabSpec.setIndicator("Home");
tabHost.addTab(tabSpec);
tabHost.newTabSpec("contact");
tabSpec.setContent(R.id.tabContact);
tabSpec.setIndicator("Contact");
tabHost.addTab(tabSpec);
tabHost.newTabSpec("money");
tabSpec.setContent(R.id.tabMoney);
tabSpec.setIndicator("Money");
tabHost.addTab(tabSpec);
tabHost.newTabSpec("stuff");
tabSpec.setContent(R.id.tabStuff);
tabSpec.setIndicator("Stuff");
tabHost.addTab(tabSpec);
if (dbContactHandler.getContactsCount() != 0)
Contacts.addAll(dbContactHandler.getAllContacts());
populateList();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.general, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void goToContactCreation(View view) {
Intent intent = new Intent(this, ContactCreation.class);
startActivity(intent);
}
//Contact
private void populateList() {
ArrayAdapter<Contact> adapter = new ContactListAdapter();
contactListView.setAdapter(adapter);
}
private class ContactListAdapter extends ArrayAdapter<Contact> {
public ContactListAdapter() {
super (General.this, R.layout.contact_listview, Contacts);
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getLayoutInflater().inflate(R.layout.contact_listview, parent, false);
Contact currentContact = Contacts.get(position);
TextView firstName = (TextView) view.findViewById(R.id.txtFirstName);
firstName.setText(currentContact.getFirstName());
TextView lastName = (TextView) view.findViewById(R.id.txtLastName);
lastName.setText(currentContact.getLastName());
return view;
}
}
}
My DbContactHandler :
package avappmobile.fourwe;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
/**
* Created by a.vescera on 13/11/2014.
*/
public class DatabaseContactHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "fourWe",
TABLE_CONTACTS = "contacts",
KEY_ID = "id",
KEY_FIRSTNAME = "firstName",
KEY_LASTNAME = "lastName",
KEY_PHONE = "phone",
KEY_EMAIL = "email",
KEY_MONEYLEND = "moneyLend",
KEY_MONEYBORROWED = "moneyBorrowed",
KEY_STUFFLEND = "stuffLend",
KEY_STUFFBORROWED = "stuffBorrowed";
public DatabaseContactHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_FIRSTNAME + " TEXT," +
KEY_LASTNAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_EMAIL + " TEXT," + KEY_MONEYLEND + " INTEGER," + KEY_MONEYBORROWED + " INTEGER," +
KEY_STUFFLEND + " INTEGER," + KEY_STUFFBORROWED + " INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
onCreate(db);
}
// Method to create a contact.
public void createContact(Contact contact) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, contact.getFirstName());
values.put(KEY_LASTNAME, contact.getLastName());
values.put(KEY_PHONE, contact.getPhone());
values.put(KEY_EMAIL, contact.getEmail());
values.put(KEY_MONEYLEND, 0);
values.put(KEY_MONEYBORROWED, 0);
values.put(KEY_STUFFLEND, 0);
values.put(KEY_STUFFBORROWED, 0);
db.insert(TABLE_CONTACTS, null, values);
db.close();
}
// Method to get the details of a specific contact by an id.
public Contact getContactId(int id) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_FIRSTNAME, KEY_LASTNAME, KEY_PHONE, KEY_EMAIL, KEY_MONEYLEND,
KEY_MONEYBORROWED, KEY_STUFFLEND, KEY_STUFFBORROWED }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null );
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
Integer.parseInt(cursor.getString(5)),Integer.parseInt(cursor.getString(6)),Integer.parseInt(cursor.getString(7)),Integer.parseInt(cursor.getString(8)));
db.close();
cursor.close();
return contact;
}
// Method to get the details of a specific contact by firstName and lastName.
public Boolean getContact(String firstName, String lastName) {
SQLiteDatabase db = getReadableDatabase();
Contact contact;
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_FIRSTNAME, KEY_LASTNAME, KEY_PHONE, KEY_EMAIL, KEY_MONEYLEND,
KEY_MONEYBORROWED, KEY_STUFFLEND, KEY_STUFFBORROWED }, KEY_FIRSTNAME + "=?" + " AND " + KEY_LASTNAME + "=?", new String[] { firstName, lastName }, null, null, null, null );
if (cursor != null && cursor.moveToFirst()) {
db.close();
cursor.close();
return true;
} else {
db.close();
cursor.close();
return false;
}
}
// Method to delete a specific contact.
public void deleteContact(Contact contact) {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + "=?", new String[] { String.valueOf(contact.getId()) });
db.close();
}
// Method to get the total number of existing contacts into the DB.
public int getContactsCount() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
int count = cursor.getCount();
db.close();
cursor.close();
return count;
}
// Method to update the details of a specific contact.
public int updateContact(Contact contact) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, contact.getFirstName());
values.put(KEY_LASTNAME, contact.getLastName());
values.put(KEY_PHONE, contact.getPhone());
values.put(KEY_EMAIL, contact.getEmail());
values.put(KEY_MONEYLEND, contact.getMoneyLend());
values.put(KEY_MONEYBORROWED, contact.getMoneylBorrowed());
values.put(KEY_STUFFLEND, contact.getStuffLend());
values.put(KEY_STUFFBORROWED, contact.getStuffBorrowed());
int rowsAffected = db.update(TABLE_CONTACTS, values, KEY_ID + "=?", new String[] { String.valueOf(contact.getId()) });
db.close();
return rowsAffected;
}
// Method to get the list of the details of all the contacts present into the DB.
public List<Contact> getAllContacts() {
List<Contact> contacts = new ArrayList<Contact>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
if (cursor.moveToFirst()) {
do {
contacts.add(new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
Integer.parseInt(cursor.getString(5)),Integer.parseInt(cursor.getString(6)),Integer.parseInt(cursor.getString(7)),Integer.parseInt(cursor.getString(8))));
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return contacts;
}
}
I don't put the detail of the Contact class, but if you think you need it, please feel free to ask me.
I put then the Design part of my main activity:
<LinearLayout 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"
tools:context="avappmobile.fourwe.General">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/tabHome"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/new_contact"
android:id="#+id/btnNewContact"
android:onClick="goToContactCreation"
android:enabled="true"
android:layout_gravity="right" />
</LinearLayout>
<LinearLayout
android:id="#+id/tabContact"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/contacts_list"
android:id="#+id/txtMyContacts"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listViewContact"
android:layout_marginTop="20dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/tabMoney"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/tabStuff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
And the layout created to display my contacts on the secont Tab.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/first_name_contact"
android:id="#+id/txtFirstName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtLastName"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/first_name_contact"
android:singleLine="false" />
</RelativeLayout>
Thanks.

After further investigation on internet, I found that with the latest version of android, it might be more interesting to show a file with tab (and each tab having a different file, which is my goal) using the ViewPager.
A quick and correctly defined tutorial is available here : http://architects.dzone.com/articles/android-tutorial-using.
I then close my subject since I'm now focused on this way of doing my work.
Regards.

Related

RecyclerView not showing the image full height

I have a problem with the images inside recyclerview
They shown like this
My code to load the image is :
#BindingAdapter("imageUrl")
fun ImageView.bindImage(imgUrl: String?) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build().toString()
val uiHandler = Handler(Looper.getMainLooper())
thread(start = true) {
val bitmap = downloadBitmap(imgUri)
uiHandler.post {
this.setImageBitmap(bitmap)
}
}
}
}
fun downloadBitmap(imageUrl: String): Bitmap? {
return try {
val conn = URL(imageUrl).openConnection()
conn.connect()
val inputStream = conn.getInputStream()
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream.close()
bitmap
} catch (e: Exception) {
Log.e(ContentValues.TAG, "Exception $e")
null
}
}
XML for item:
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/iv_song_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:imageUrl="#{songObject.songImage}"
android:src="#drawable/img_song_cover" />
And XML For Recycler:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_home_albums_list"
android:layout_width="match_parent"
android:layout_height="#dimen/dp200w"
android:layout_marginStart="#dimen/dp16w"
android:layout_marginLeft="#dimen/dp16w"
android:layout_marginTop="#dimen/dp8w"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_home_albums_title"
tools:listitem="#layout/item_album" />
My task is to do all the work without using any third-party,
So any help ?
I have solved the problem just add a statement to check if the image loaded before to not render it more than once when I scroll with recycler view, and remove the place holder.
if(this.drawable == null) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build().toString()
val uiHandler = Handler(Looper.getMainLooper())
thread(start = true) {
val bitmap = downloadBitmap(imgUri)
uiHandler.post {
this.setImageBitmap(bitmap)
}
}
}
}

Dynamic population of dropdown based on another dropdown in xamarin android

I am in need of getting state and corresponding cities from selected country, in Xamarin.Android (need not in Xamarin.Forms)
I populate the countries in Spinner control as shown below
Spinner spinnerMailingCountry;
int[] countryId = new int[] { 0, 58, 98, 105, 86 };
String[] countryName = { "Select", "England", "Germany", "India"};
ArrayAdapter<String> countryAdapter;
String countryIdByPosition;
int selectedPosition;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
...
spinnerMailingCountry = FindViewById<Spinner>(Resource.Id.spinnerMailingCountry);
SetupCountrySpinner();
}
void SetupCountrySpinner()
{
countryAdapter = new ArrayAdapter<string>(this, Resource.Layout.spinner_item, countryName);
countryAdapter.SetDropDownViewResource(Resource.Layout.spinner_item);
spinnerMailingCountry.Adapter = countryAdapter;
spinnerMailingCountry.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(CountrySpinner_ItemSelected);
}
private void CountrySpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
var spinner = (Spinner)sender;
selectedPosition = spinner.SelectedItemPosition;
countryIdByPosition = countryId[selectedPosition].ToString();
}
Now once a country is selected, I need to populate list of corresponding state in spinner. This flow is also for city too. How can I achieve this?
NOTE: State and city lists are getting from my database through API call.
Now once a country is selected, I need to populate list of corresponding state in spinner. This flow is also for city too. How can I achieve this?
When CountrySpinner_ItemSelected be invoked, you can set ArrayAdapter for State.And when State be selected, you can set ArrayAdapter for City.
axml:
<Spinner
android:id="#+id/spinnerCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<Spinner
android:id="#+id/spinnerState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<Spinner
android:id="#+id/spinnerCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
Activity.cs : Adding test data( *stateName / cityName *)
Spinner spinnerMailingCountry;
Spinner spinnerMailingState;
Spinner spinnerMailingCity;
int[] countryId = new int[] { 0, 58, 98, 105, 86 };
String[] countryName = { "Select", "England", "Germany", "India" };
String[] stateName = { "Select", "EnglandStateOne", "EnglandStateTwo", "EnglandStateThree" };
String[] cityName = { "Select", "EnglandCityOne", "EnglandCityTwo", "EnglandCityThree" };
ArrayAdapter<String> countryAdapter;
ArrayAdapter<String> stateAdapter;
ArrayAdapter<String> cityAdapter;
String countryIdByPosition;
int selectedPosition;
OnCreate Method:
spinnerMailingCountry = FindViewById<Spinner>(Resource.Id.spinnerCountry);
spinnerMailingState = FindViewById<Spinner>(Resource.Id.spinnerState);
spinnerMailingState.Enabled = false;
spinnerMailingCity = FindViewById<Spinner>(Resource.Id.spinnerCity);
spinnerMailingCity.Enabled = false;
countryAdapter = new ArrayAdapter<string>(this, Resource.Layout.support_simple_spinner_dropdown_item, countryName);
spinnerMailingCountry.Adapter = countryAdapter;
spinnerMailingCountry.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(CountrySpinner_ItemSelected);
spinnerMailingState.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(StateSpinner_ItemSelected);
spinnerMailingCity.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(CitySpinner_ItemSelected);
CountrySpinner_ItemSelected : Display state according to country.
private void CountrySpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
var spinner = (Spinner)sender;
selectedPosition = spinner.SelectedItemPosition;
countryIdByPosition = countryId[selectedPosition].ToString();
if(selectedPosition != 0) {
stateAdapter = new ArrayAdapter<string>(this, Resource.Layout.support_simple_spinner_dropdown_item, stateName);
//stateAdapter's data can get from your databse API with countryIdByPosition
spinnerMailingState.Enabled = true;
spinnerMailingState.Adapter = stateAdapter;
}
else
{
spinnerMailingState.Enabled = false;
}
}
StateSpinner_ItemSelected: Display city according to state.
private void StateSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
var spinner = (Spinner)sender;
selectedPosition = spinner.SelectedItemPosition;
cityAdapter = new ArrayAdapter<string>(this, Resource.Layout.support_simple_spinner_dropdown_item, cityName);
if (selectedPosition != 0)
{
spinnerMailingCity.Enabled = true;
spinnerMailingCity.Adapter = cityAdapter;
//cityAdapter' data can get from your databse API with selectedPosition
Console.WriteLine("-------------" + stateName[selectedPosition].ToString());
}
else
{
spinnerMailingCity.Enabled = false;
}
}
CitySpinner_ItemSelected: Show the city you want.
private void CitySpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
var spinner = (Spinner)sender;
selectedPosition = spinner.SelectedItemPosition;
Console.WriteLine("-------------" + cityName[selectedPosition].ToString());
}
When OnDestroy , unregister ItemSelected event:
protected override void OnDestroy()
{
base.OnDestroy();
spinnerMailingCountry.ItemSelected -= new EventHandler<AdapterView.ItemSelectedEventArgs>(CountrySpinner_ItemSelected);
spinnerMailingState.ItemSelected -= new EventHandler<AdapterView.ItemSelectedEventArgs>(StateSpinner_ItemSelected);
spinnerMailingCity.ItemSelected -= new EventHandler<AdapterView.ItemSelectedEventArgs>(CitySpinner_ItemSelected);
}

How to rotate images in GridView

A collection of images are showing in GridView using Xamarin Android with Mvvm Cross, but the problem is there are two button which rotates clock and anti clock direction with 90 degrees for each click all cells(or images) should be rotate within the GridView.
How can I achieve it?
Here is my viewModel collection which will be bind to GridView,
private ObservableCollection<Snapshot> _snapshotsItemSource;
public ObservableCollection<Snapshot> SnapshotsItemSource
{
get { return _snapshotsItemSource; }
set
{
_snapshotsItemSource = value;
RaisePropertyChanged(() => SnapshotsItemSource);
}
}
And my Model object is,
public class Snapshot : MvxViewModel
{
string _ID;
public string ID
{
get
{
return _ID;
}
set
{
_ID = value;
ImageUrl = string.Format("http://{0}:{1}/GetSnapshot?ID={2}", TIConstants.Device.IpAdd, TIConstants.PortNo, value);
}
}
string _ImageUrl;
public string ImageUrl
{
get
{
return _ImageUrl;
}
set
{
_ImageUrl = value;
RaisePropertyChanged("ImageUrl");
}
}
}
And My view is,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mvvm="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<MvvmCross.Binding.Droid.Views.MvxGridView
android:id="#+id/gvSnapshots"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:layout_alignParentTop="true"
mvvm:MvxItemTemplate="#layout/snapshotcellview"
mvvm:MvxBind="ItemsSource SnapshotsItemSource" />
<RelativeLayout
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="#dimen/section_height"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<ImageButton
android:id="#+id/btnRotateLeft"
android:layout_alignParentLeft="true"
android:src="#drawable/abc_text_select_handle_left_mtrl_dark"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/btnRotateRight"
android:layout_alignParentRight="true"
android:src="#drawable/abc_text_select_handle_left_mtrl_dark"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Try With Glide, is This helpful for you
Glide
.with( context )
.load( "image Url" )
.transform( new RotateTransformation( context, 90f ))
.into( imageView );
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.SnapshotsView, null);
var toolbar = ((MainActivity)this.Activity).FindViewById(Resource.Id.toolbar);
var txtTitle = ((MainActivity)this.Activity).FindViewById<TextView>(Resource.Id.TitleText);
txtTitle.Text = ViewModel.ViewTitle;
RegisterEvents();
ViewModel.SetItemSource();
var btnLeft = view.FindViewById<ImageButton>(Resource.Id.btnRotateLeft);
btnLeft.Click += BtnLeft_Click;
var btnRight = view.FindViewById<ImageButton>(Resource.Id.btnRotateRight);
btnRight.Click += BtnRight_Click;
grid = view.FindViewById<MvxGridView>(Resource.Id.gvSnapshots);
mAdapter = new SnapshotsDataViewAdapter(this.Activity, (IMvxAndroidBindingContext)BindingContext);
grid.Adapter = mAdapter;
grid.OnItemClickListener = this;
return view;
}
void BtnLeft_Click(object sender, EventArgs e)
{
currentRotation = currentRotation + 1;
mAdapter.NotifyDataSetChanged();
}
void BtnRight_Click(object sender, EventArgs e)
{
currentRotation = currentRotation - 1;
mAdapter.NotifyDataSetChanged();
}
static int currentRotation = 0;
static float RotationTranslation = 0;
private class SnapshotsDataViewAdapter : MvxAdapter
{
SnapshotsViewModel mViewModel;
public SnapshotsDataViewAdapter(FragmentActivity context, IMvxAndroidBindingContext bindingContext) : base(context, bindingContext)
{
mViewModel = bindingContext.DataContext as SnapshotsViewModel;
}
protected override View GetBindableView(View convertView, object dataContext, ViewGroup parent, int templateId)
{
View row = convertView;
try
{
var item = (BSSnapshot)dataContext;
if (row == null)
{
row = BindingContext.BindingInflate(templateId, parent, false);
}
var imgView = row.FindViewById<ImageView>(Resource.Id.imgRotate1);
Picasso.With(Android.App.Application.Context).Load(item.ImageUrl).Into(imgView);
imgView.Rotation = currentRotation*90;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally
{}
return row;
}
}
Here I used Picasso as Glide was not able to getting add to the project and it is working fine.

Issue with Xamarin Android AppcombatToolbar and MobileBarcodeScanner

If I try to use the AppcombatToolbar (https://developer.xamarin.com/guides/android/user_interface/toolbar/part-3-toolbar-compatibility/) in a customoverlay the items in my toolbar are not displayed. I see the space of the toolbar but without buttons and text
This is my zxing usage:
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
//Tell our scanner we want to use a custom overlay instead of the default
scanner.UseCustomOverlay = true;
//Inflate our custom overlay from a resource layout
View zxingOverlay = LayoutInflater.FromContext(this).Inflate(Resource.Layout.Master, null);
//Set our custom overlay
scanner.CustomOverlay = zxingOverlay;
//Start scanning!
var result = await scanner.Scan();
This my BaseActivity
[Activity(Label = "#string/app_name", MainLauncher = false, Icon = "#drawable/icon")]
public class MasterActivity : AppCompatActivity
{
LinearLayout content;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
base.SetContentView(Resource.Layout.Master);
// Create your application here
var toolbar = FindViewById<Toolbar>(Resource.Id.maintoolbar);
SetSupportActionBar(toolbar);
SupportActionBar.Title = GetString(Resource.String.app_name);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeButtonEnabled(true);
content = (LinearLayout)FindViewById(Resource.Id.content);
}
public override void SetContentView(int id)
{
LayoutInflater inflater = (LayoutInflater)BaseContext.GetSystemService(Context.LayoutInflaterService);
if (content != null)
{
inflater.Inflate(id, content);
}
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.topmenus, menu);
return base.OnCreateOptionsMenu(menu);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if(item.ItemId == Resource.Id.menu_logout)
{
Settings.UserName = null;
Settings.Password = null;
StartActivity(typeof(LoginActivity));
}
Toast.MakeText(this, "Action selected: " + item.TitleFormatted,
ToastLength.Short).Show();
return base.OnOptionsItemSelected(item);
}
}
And this my Base Layout:
<?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"
android:minWidth="25px"
android:minHeight="25px"
android:weightSum="1">
<include
android:id="#+id/maintoolbar"
layout="#layout/toolbar" />
<LinearLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/transparent"/>
</LinearLayout>
Page with working Toolbar
Scanpage with no working toolbar

Checkbox check jumping randomly around the list items?

I have a ExpandableListView declared as follow :
<ExpandableListView
android:id="#+id/ListBExpandable"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
and I have Child Item layout declared as follow :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px" >
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBoxBListItem" />
</LinearLayout>
When I am checking the child's checkboxes and expand or close some group element the checked checkbox start jumping in seems like random position.
This is the source of the adapter :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace LEX.Droid
{
public class BActivityGroupedAdapter : BaseExpandableListAdapter
{
public static List<BNodeProxy> bNodes = bew List<BNodeProxy>();
public static Dictionary<int, BGroupProxy> bGroups = new Dictionary<int, BGroupProxy>();
Activity context;
List<int> groupKeys;
List<int> filteredGroupKeys;
Dictionary<int,List<int>> filteredItems;
public BActivityGroupedAdapter( Activity context, Dictionary<int,List<int>> filteredItems )
{
this.context = context;
groupKeys = bGroups.Keys.ToList();
this.filteredGroupKeys = filteredGroupKeys;
}
public override Java.Lang.Object GetChild( int groupPosition, int childPosition )
{
int nodeId = filteredItems[ filteredGroupKeys[ groupPosition ] ][ childPosition ];
return bNodes.Where( o => o.id == nodeId ).First().label;
}
public override long GetChildId( int groupPosition, int childPosition )
{
return childPosition;
}
public override int GetChildrenCount( int groupPosition )
{
return filteredItems[ filteredGroupKeys[ groupPosition ] ].Count();
}
public override View GetChildView( int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent )
{
View view;
int nodeId = filteredItems[ filteredGroupKeys[ groupPosition ] ][ childPosition ];
var item = bNodes.Where( o => o.id == nodeId ).First();
if( convertView == null )
{
view = context.LayoutInflater.Inflate( Resource.Layout.ListChildItem, null );
}
else
{
view = convertView;
}
try
{
var checkBoxSelect = view.FindViewById<CheckBox>( Resource.Id.checkBoxListItem );
checkBoxSelect.Text = item.label;
checkBoxSelect.Selected = item.selected;
checkBoxSelect.Tag = item.id;
checkBoxSelect.Click -= theClickEvent;
checkBoxSelect.Click += theClickEvent;
}
catch( Exception ex )
{
}
view.Clickable = false;
view.Focusable = false;
view.FocusableInTouchMode = false;
view.LongClickable = false;
return view;
}
public override Java.Lang.Object GetGroup( int groupPosition )
{
return bGroups[ filteredGroupKeys[ groupPosition ] ].label;
}
public override long GetGroupId( int groupPosition )
{
return groupPosition;
}
public override View GetGroupView( int groupPosition, bool isExpanded, View convertView, ViewGroup parent )
{
View view;
var item = bGroups[ filteredGroupKeys[ groupPosition ] ].label;
if( convertView == null )
{
var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
view = inflater.Inflate( Resource.Layout.ListGroupItem, null );
}
else
{
view = convertView;
}
try
{
var textBox = view.FindViewById<TextView>( Resource.Id.groupItemListText );
textBox.SetText( item, TextView.BufferType.Normal );
}
catch( Exception ex )
{
}
return view;
}
public override bool IsChildSelectable( int groupPosition, int childPosition )
{
return true;
}
public override int GroupCount
{
get
{
return filteredGroupKeys.Count;
}
}
public override bool HasStableIds
{
get
{
return true;
}
}
private void theClickEvent( object sender, EventArgs e )
{
var selectionState = ( (CheckBox)sender ).Checked;
int tagId = (int)( (CheckBox)sender ).Tag;
bNodes.Where( o => o.id == tagId ).First().selected = selectionState;
}
}
}
How to prevent the checkbox from this strange behavior ?
Found the bug..
checkBoxSelect.Selected = item.selected;
actually should be:
checkBoxSelect.Checked = item.selected;
seems like my stack went overflow :)

Resources