I'm working on developing a simple todo list app with a custom ListView. I want to be able to change the colors (such as white text rather then black on the text view) and perhaps add a checkbox for each item in the ListView.
When using a default list view I need to use the layout.xml id '#android:id/list', however, to customize the view, I am using the id '#+id/task_list'. When I do this I receive the error:
"your content must have a listview whose id attribute is 'android.r.id.list'"
What should I do? And insight will be appreciated.
Below is the code that I am using.
MainActivity.java
package com.example.todo;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ListActivity {
ArrayList<TextView> listItems=new ArrayList<TextView>();
ArrayAdapter<TextView> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter=new ArrayAdapter<TextView>(this,
R.layout.task_row_item,
listItems);
setListAdapter(adapter);
}
public void addItems(View view) {
EditText newItem = (EditText) findViewById(R.id.textField);
TextView listItem = (TextView) findViewById(R.id.task_description);
listItems.add(listItem);
adapter.notifyDataSetChanged();
newItem.setText("");
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/textField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Enter a task..."
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/addItems"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="addItems"
android:text="Add" />
</LinearLayout>
<ListView
android:id="#+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
task_row_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="horizontal" >
<TextView
android:id="#+id/task_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Either stop using ListActivity or replace
<ListView
android:id="#+id/task_list"
with
<ListView
android:id="#android:id/list"
Get familiar with: http://developer.android.com/reference/android/app/ListActivity.html
Related
I want to know how to develop a custom dropdown icons Menu with GridView options from Menu.
This is my code for custom down icons
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c2ec97"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="viralandroid.com.androidxmluserinterfacetutorial.MainActivity">
<Button
android:id="#+id/dropdown_custom_icon_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#5bace6"
android:drawableRight="#android:drawable/arrow_down_float"
android:onClick="verticalDropDownIconMenu"
android:padding="16dp"
android:text="DropDown\t"
android:textColor="#eee" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_custom_icon_menu"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Custom Vertical Dropdown Menu IconsAndroid Custom Vertical. Break line after Icon in Menu Item
Android.
Dropdown Menu IconsAndroid Custom Vertical Dropdown. popup menu with icon in android example.
Android Custom Vertical Dropdown Menu Icons. Android Custom Vertical Dropdown Menu Icons" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Android Custom Vertical Dropdown Menu Icons" />
</LinearLayout>
<LinearLayout
android:id="#+id/vertical_dropdown_icon_menu_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_custom_icon_menu"
android:background="#333"
android:orientation="vertical"
android:padding="3dp"
android:visibility="invisible">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_send"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_refresh"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />
</LinearLayout>
</RelativeLayout>
This is what i have :
This is what i want :
You need to use the spinner and that will lead to use an adapter and a couple of layouts.
First in your activity_main.xml:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Spinner
android:id="#+id/sSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
then create a new layout under /res/layout folder and just name it anything like custom_spinner_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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/myImageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:padding="10dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="test"
android:textColor="#000" />
</LinearLayout>
then in you MainActivity.java you need to do your code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] carNames={"Chevy","Dodge","Mazda","Honda","BMW","Toyota"};
int icons[] = {R.drawable.Chevy, R.drawable.Dodge, R.drawable.Mazda, R.drawable.Honda, R.drawable.BMW, R.drawable.Toyota};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin = (Spinner) findViewById(R.id.sSpinner);
spin.setOnItemSelectedListener(this);
CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),icons,carNames);
spin.setAdapter(customAdapter);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
//your code goes here///
Toast.makeText(getApplicationContext(), carNames[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// something happens here
}
}
You will need to create a new java class file and name it CustomAdapter.java and thats gonna be extended fro BaseAdapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Context context;
int icons[];
String[] carNames;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] icons, String[] carNames) {
this.context = applicationContext;
this.icons = icons;
this.carNames = carNames;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return icons.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_layout, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(icons[i]);
names.setText(carNames[i]);
return view;
}
}
you can get the working code in :
https://github.com/edgebasis/spinnerExample
Im trying to create a listview containing all currently installed User Apps.
The problem im facing is that the ListView is Only showing one entry and i cant figure out what im doing wrong.
I use the same code elsewhere (With a JSON Result) and it works fine.
Here is my Activity XML
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/colorPrimary"
tools:context=".AppsList">
<ListView
android:id="#+id/applistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Close" />
</RelativeLayout>
My List item XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="6" >
<ImageView
android:id="#+id/app_icon"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="1"
android:src="#drawable/frissonhead" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/app_name"
android:layout_width="258dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Soon To Clear"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorAccent"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/app_package"
android:layout_width="258dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Soon To Clear"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorAccent"
android:textSize="12dp" />
</LinearLayout>
<CheckBox
android:id="#+id/app_check"
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_weight="1.02"
android:checked="false"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
And my Activity Java
package com.fixmypcscotland.childsafe;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AppsList extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
ListView list;
ArrayList<HashMap<String, String>> oslist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
setContentView(R.layout.activity_apps_list);
sharedPreferences = getSharedPreferences("CHILD_MODE", MODE_PRIVATE);
editor = sharedPreferences.edit();
list = (ListView)findViewById(R.id.applistview);
oslist = new ArrayList<HashMap<String, String>>();
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages =
pm.getInstalledApplications(PackageManager.GET_META_DATA);
HashMap<String, String> map = new HashMap<String, String>();
for (ApplicationInfo packageInfo : packages) {
String appkg = packageInfo.packageName;
String apn = pm.getApplicationLabel(packageInfo).toString();
map.put("APPPKG", appkg);
map.put("APPNAME", apn);
}
oslist.add(map);
ListAdapter adapter = new SimpleAdapter(AppsList.this, oslist, R.layout.activity_apps_list_item, new String[]{"APPPKG", "APPNAME"}, new int[]{R.id.app_package, R.id.app_name});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
editor.putString("bid", oslist.get(+position).get("APPPKG"));
editor.commit();
Toast.makeText(getApplicationContext(), oslist.get(+position).get("APPPKG"), Toast.LENGTH_LONG).show();
}
});
}
}
Any help is appreciated. Its driving me crazy.
Change your List Item with this one.
<?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:paddingBottom="7dp"
android:background="#color/colorPrimary" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="6" >
<ImageView
android:id="#+id/app_icon"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="1"
android:src="#drawable/frissonhead" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/app_name"
android:layout_width="258dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Soon To Clear"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorAccent"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/app_package"
android:layout_width="258dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Soon To Clear"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorAccent"
android:textSize="12dp" />
</LinearLayout>
<CheckBox
android:id="#+id/app_check"
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_weight="1.02"
android:checked="false"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
And Learn about Adapter click here you may get hint from here.
for (ApplicationInfo packageInfo : packages) {
String appkg = packageInfo.packageName;
String apn = pm.getApplicationLabel(packageInfo).toString();
map.put("APPPKG", appkg);
map.put("APPNAME", apn);
**oslist.add(map);**
}
You are passing the wrong data to your adapter, so you have to change this line:
ListAdapter adapter = new SimpleAdapter(AppsList.this, oslist, R.layout.activity_apps_list_item, oslist, new int[]{R.id.app_package, R.id.app_name});
Also, I must to warn you to see the documentation about the SimpleAdapter to pass the right parameters to it:
The first parameter is a Context where the View associated with this SimpleAdapter is running
The second parameter is a Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to".
The third is a list of column names that will be added to the Map associated with each item.
And the last one is The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
And then, fix your for loop:
for (ApplicationInfo packageInfo : packages) {
String appkg = packageInfo.packageName;
String apn = pm.getApplicationLabel(packageInfo).toString();
map.put("APPPKG", appkg);
map.put("APPNAME", apn);
oslist.add(map);
}
I have written a sign-In xml page and I am checking the eligibility of the user to login through Firebase database. Now after entering the username and password , I am clicking on the "Sign-In" button. It is taking few seconds to load the Home page after this. So I want to display a Progress Bar which revolves during that time and stops on opening the Home page.
My layout page for sing-In is:
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/loadingSignIn"
android:indeterminate="true"
android:visibility="invisible"
>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.hsports.bandpop.MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/title"
android:text="WEDDING PLANNER"
android:gravity="center"
android:textSize="50dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:paddingTop="100dp"
>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Layout_username"
android:hint="Username"
>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/username"
android:gravity="center"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Layout_password"
android:hint="Password"
>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/password"
android:gravity="center"
android:inputType="textPassword"
/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="50dp"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SIGN IN"
android:onClick="checkSignIn"
android:gravity="center"
android:id="#+id/SignInButton"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="REGISTER"
android:onClick="registerForm"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</ProgressBar>
And the code as a fragment on clicking the "SignIn" button is:
package com.example.hsports.weddingplanner.Fragments;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.hsports.weddingplanner.Activities.FrontPage;
import com.example.hsports.weddingplanner.Activities.WeddingPlanningUsers;
import com.example.hsports.weddingplanner.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
/**
* Created by I324671 on 11/28/2016.
*/
public class SignIn extends Fragment {
SharedPreferences sharedPreferences;
String MyPreferences="loginUserInfo";
ProgressBar afterSignIn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
final View view=inflater.inflate(R.layout.activity_login_page, container, false);
afterSignIn=(ProgressBar)view.findViewById(R.id.loadingSignIn);
Button checkSignIn=(Button)view.findViewById(R.id.SignInButton);
checkSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
afterSignIn.setVisibility(view.VISIBLE);
final String username = ((TextView) (view.findViewById(R.id.username))).getText().toString();
final String password = ((TextView) (view.findViewById(R.id.password))).getText().toString();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference("Users");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String usernameFromDB=child.getKey().toString();
String passwordFromDB=child.getValue().toString();
if(username.equals(usernameFromDB)&& password.equals(passwordFromDB))
{
sharedPreferences=getContext().getSharedPreferences(MyPreferences, Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("Username",username);
editor.putString("Password", password);
editor.commit();
afterSignIn.setVisibility(view.GONE);
Intent transferToHome=new Intent(getContext(), FrontPage.class);
startActivity(transferToHome);
break;
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("The read failed", databaseError.getDetails());
}
});
}
});
return view;
}
}
The error which I ma getting on executing this is :
"Binary XML file android.widget.ProgressBar cannot be cast to android.view.ViewGroup".
How can I resolve this.
The Problem is that you
Have to close top tag inside XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/loadingSignIn"
android:indeterminate="true"
android:visibility="invisible"
/>
And:
Use
</LinearLayout>
</ScrollView>
</LinearLayout>
You can have only one root of your layout file. Here you have two. namely ProgessBar and ScrollView. Try encapsulating them in FrameLayout and everything will be alright.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ProgressBar
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/loadingSignIn"
android:indeterminate="true"
android:visibility="invisible"
>
<ScrollView
...
</ScrollView>
</FrameLayout>
edit -->
for making progress bar center wise, change
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:id="#+id/loadingSignIn"
android:indeterminate="true"
android:visibility="invisible"
/>
and handle invisibility / visibility. :)
I have the following activity. It is an simplification of a draggable map. The problem is that mytext2 is not clickable, even if it is visible. Can anybody tell me how to make it clickable? And for some reason the inner-RelativeLayout gets not bigger than screen size, even when i set this high dp.
<RelativeLayout 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:background="#0000ff"
android:clipChildren="false"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="100000dp"
android:layout_height="100000dp"
android:background="#00ff00"
android:translationX="-100dp" >
<TextView
android:id="#+id/mytext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="400dp"
android:translationY="100dp" />
<TextView
android:id="#+id/mytext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="200dp"
android:translationY="100dp" />
</RelativeLayout>
</RelativeLayout>
class
package com.example.relativatest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.mytext1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("HELLO!1");
}
});
findViewById(R.id.mytext2).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("HELLO!2");
}
});
}
}
Use this code.
<RelativeLayout 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:background="#0000ff"
android:clipChildren="false"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="100000dp"
android:layout_height="100000dp"
android:background="#00ff00"
android:translationX="-100dp" >
<TextView
android:id="#+id/mytext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:focusable="true"
android:padding="20dp"
android:translationX="400dp"
android:clickable="true"
android:translationY="100dp" />
<TextView
android:id="#+id/mytext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="200dp"
android:clickable="true"
android:padding="20dp"
android:focusable="true"
android:translationY="100dp" />
</RelativeLayout>
</RelativeLayout>
I have the following code that is a dynamic listview where you type in the textbox and click the add button and its added to the listview below. Listview is a custom listview with 2 textviews. Somehow the code adds the first item and then does not add the rest. The arraylist gets the item, i call adapter.notifyDataSetChanged(); and yet still the listview does not get updated. What am i doing wrong?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ManageComplaintsActivity extends DashboardActivity {
EditText txtAddComplaint;
ListView lvComplaintsList;
Button btnAddComplaint;
private SimpleAdapter adapter;
private int count = 1;
private ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_complaints);
setTitleFromActivityLabel (R.id.title_text);
txtAddComplaint= (EditText) findViewById(R.id.txtAddComplaint);
lvComplaintsList= (ListView) findViewById(R.id.lvComplaintsList);
btnAddComplaint = (Button)findViewById(R.id.btnAddComplaint);
String[] from = { "complaint", "complaintid" };
int[] to = { R.id.lblC, R.id.lblCID };
adapter = new SimpleAdapter (this, list, R.layout.activity_manage_complaints_row, from, to);
lvComplaintsList.setAdapter(adapter);
btnAddComplaint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("complaint", txtAddComplaint.getText().toString());
item.put("complaintid", Integer.toString(count));
list.add(item);
adapter.notifyDataSetChanged();
count++;
txtAddComplaint.setText("");
Toast.makeText(getApplicationContext(),Integer.toString(list.size()), Toast.LENGTH_SHORT).show();
}
});
lvComplaintsList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView)view.findViewById(R.id.lblC)).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
This is the 2 xml files
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dip"
android:shrinkColumns="1"
android:stretchColumns="1" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/txtAddComplaint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" />
<Button
android:id="#+id/btnAddComplaint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtComplaintLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List of Complaints : "
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="20dp"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lvComplaintsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_span="2" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
This is the listviewrow xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/lblC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/lblCID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
As you can see in documentation to SimppleAdapter it is
An easy adapter to map static data to views defined in an XML file.
I believe that you can find any tricky way to add data dynamically. But I'm not sure that you have to do it like this. Try to use ArrayAdapter instead. By default it binds T.toString() values to single TextView, but you can override getView(int, View, ViewGroup) to return the type of view you want.
I changed the main xml file as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/txtAddComplaint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
<Button
android:id="#+id/btnAddComplaint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to Listview" >
</Button>
</LinearLayout>
<ListView
android:id="#+id/lvComplaintsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I had used a table layout and the listview was in one of the rows.
Hope this helps someone .