I have a ViewFlipper populated with multiple ImageView. but now, i wanted to populate the view flipper with images from mysql database. But i don't know how to implement it.
here's my ViewFlipper.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login">
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="fill_parent"
android:flipInterval="5000"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right"
android:layout_height="fill_parent"
android:layout_centerInParent="true">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic1"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic2"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic3"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic4"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic5"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/pic6"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/pic7"/>
</ViewFlipper>
</RelativeLayout>
and here is my MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button btn_login = (Button)findViewById(R.id.login);
Button btn_signup = (Button)findViewById(R.id.create);
final EditText txt_username = (EditText)findViewById(R.id.username);
final EditText txt_password = (EditText)findViewById(R.id.password);
getSupportActionBar().hide();
ViewFlipper flipper = (ViewFlipper)findViewById(R.id.view_flipper);
flipper.startFlipping();
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);
Bitmap circularBitmap = ImageConverter.getRoundedCornerBitmap(bitmap, 80);
ImageView circularImageView = (ImageView)findViewById(R.id.logo);
circularImageView.setImageBitmap(circularBitmap);
View.OnClickListener login_click = new View.OnClickListener(){
//login form validation
public void onClick(View view) {
String user = txt_username.getText().toString();
String pass = txt_password.getText().toString();
if(user.length()==0)
{
txt_username.requestFocus();
txt_username.setError("Username cant be empty");
}
else if(pass.length()==0){
txt_password.requestFocus();
txt_password.setError("Password cant be empty");
}
else
{
Toast.makeText(Login.this,"Login Successful", Toast.LENGTH_LONG).show();
startActivity(new Intent(Login.this,Homepage.class));
}
}
};
btn_login.setOnClickListener(login_click);
View.OnClickListener signup_click = new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Login.this,Registration.class));
}
};
btn_signup.setOnClickListener(signup_click);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here my JSON.
Hope you can help me :)
Related
I tried Action Bar like this
I want like when i click on Actionbar's Item it will print a Toast but it is not working tried a lot.
I Created 2 custom layout,one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/noti"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginTop="3dp"
android:layout_marginLeft="15dp"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</RelativeLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/task"
android:id="#+id/imageView5" />
<TextView
android:id="#+id/cart_badge1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="15dp"
android:layout_gravity="right|end|top"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"
android:layout_alignTop="#+id/imageView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
navigation.menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:apps="http://schemas.android.com/tools">
<item
android:id="#+id/action_refresh"
android:icon="#drawable/noti"
app:showAsAction="always"
android:actionLayout="#layout/custom_action_notification_layout"
android:title="Refresh"/>
<item
android:id="#+id/action_task"
android:orderInCategory="100"
android:actionLayout="#layout/custom_action_task_layout"
android:title="Task"
android:icon="#drawable/task"
app:showAsAction="always" /></menu>
Now in my navigationdrawer.class i put thiscode onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
navigation.class
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_refresh);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
// RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
final MenuItem itemNotification1 = menu.findItem(R.id.action_task);
MenuItemCompat.setActionView(itemNotification1, R.layout.custom_action_task_layout);
View actionViewNotification1 = MenuItemCompat.getActionView(itemNotification1);
// RelativeLayout notifCount1 = (RelativeLayout) MenuItemCompat.getActionView(itemNotification1);
actionViewNotification1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification1);
}
});
Now finally in onOptionsItemSelected() the code is like
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
Tried in many ways but not working at all please help out from this..
1. Create two custom layout, one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_notification"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_task"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
2. Create a menu XML containing Notification and Task item. Use attribute app:actionLayout to set custom layout to each item.
custom_menu.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_notification"
android:icon="#drawable/icon_notification"
android:title="Notification"
app:actionLayout="#layout/custom_action_notification_layout"
app:showAsAction="always"/>
<item
android:id="#+id/action_task"
android:icon="#drawable/icon_task"
android:title="Task"
app:actionLayout="#layout/custom_action_task_layout"
app:showAsAction="always"/>
</menu>
3. In your Activity onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_notification);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
// Task
final MenuItem itemTask = menu.findItem(R.id.action_task);
View actionViewTask = MenuItemCompat.getActionView(itemTask);
actionViewTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemTask);
}
});
return true;
}
4. Finally, In onOptionsItemSelected() do the rest:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notification: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
return super.onOptionsItemSelected(item);
}
FYI, I have checked Notification and Task menu item click event by showing Toast messages.
OUTPUT:
UPDATE:
In your updated code use:
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
Instead of
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
Hope this will help~
My problem is, i want to implement a activity with a list and image and in the bottom of image there is four text .When I am going to open my app it has to show one selected text with its related image and list as default.when clicking on rest of the text it has to show their related list and images in the same activity respectively.Since i am new to android please help me out.
Here is xml code
<RelativeLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.5"
>
<ImageView
android:id="#+id/mainimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pestf"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pest Control"
android:id="#+id/pestf"
android:onClick="click1"
android:clickable="true"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/packf"
android:layout_marginLeft="5dp"
android:id="#+id/packf"
android:onClick="click1"
android:clickable="true"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cleaning"
android:layout_marginLeft="5dp"
android:id="#+id/cleanf"
android:onClick="click1"
android:clickable="true"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Landscaping"
android:id="#+id/landf"
android:onClick="click1"
android:clickable="true"
android:layout_marginLeft="5dp"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
</LinearLayout>
</RelativeLayout>
<ListView
android:id="#+id/lst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/ll_pest"
android:layout_weight="2.2"
android:background="#FFFFFF"
>
</ListView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/lst"
android:layout_weight="3.5"
android:gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="3dp"
android:layout_alignParentBottom="true"
android:background="#drawable/border_layout">
<TextView
android:id="#+id/txt_pest_Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="#FFFFFF"
android:text="#string/btn7"
android:drawablePadding="5dp"
android:drawableTop="#drawable/icon7"
android:textStyle="bold"
android:textColor="#000000"
/>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#82020202" />
<TextView
android:id="#+id/txt_pest_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="#ffffff"
android:text="#string/btn8"
android:drawablePadding="5dp"
android:drawableTop="#drawable/icon8"
android:textStyle="bold"
android:textColor="#000000"
/>
</LinearLayout>
Here is Activity code
public class demo2 extends AppCompatActivity {
TextView tv1,tv2,tv3,tv4,txtEmail,txtPhone;
ListView lv;
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo2);
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
lv = (ListView) findViewById(R.id.lst);
img = (ImageView) findViewById(R.id.mainimage);
txtEmail=(TextView)findViewById(R.id.txt_pest_Email);
txtPhone=(TextView)findViewById(R.id.txt_pest_phone);
tv1 = (TextView) findViewById(R.id.text1);
tv2 = (TextView) findViewById(R.id.text2);
tv3 = (TextView) findViewById(R.id.text3);
tv4 = (TextView) findViewById(R.id.text4);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, adobe_products));
tv1.setTextColor(getResources().getColor(R.color.blue));
txtPhone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mobileNo = "08042589999";
String uri = "tel:" + mobileNo.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
});
txtEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(demo2.this, Booknow.class);
// sending data to new activity
i.putExtra("flag",true);
i.putExtra("cat",1);
startActivity(i);
}
});
// Inflate the layout for this fragment
}
public void click2(View v){
img.setBackgroundResource(R.drawable.packers);
String[] countries = getResources().getStringArray(R.array.country);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, countries));
tv2.setTextColor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
tv3.setTextColor(getResources().getColor(R.color.red));
tv4.setTextColor(getResources().getColor(R.color.red));
}
public void click1(View v){
img.setBackgroundResource(R.drawable.pestf);
String[] adobe = getResources().getStringArray(R.array.adobe_products);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, adobe));
tv1.setTextColor(getResources().getColor(R.color.blue));
tv2.setTextColor(getResources().getColor(R.color.red));
tv3.setTextColor(getResources().getColor(R.color.red));
tv4.setTextColor(getResources().getColor(R.color.red));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
Intent i = new Intent(demo2.this, BedbugControl.class);
// sending data to new activity
i.putExtra("position", position);
startActivity(i);
}
});
}
public void click3(View v){
img.setBackgroundResource(R.drawable.cleaningf);
String[] sweets = getResources().getStringArray(R.array.sweets);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, sweets));
tv3.setTextColor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
tv2.setTextColor(getResources().getColor(R.color.red));
tv4.setTextColor(getResources().getColor(R.color.red));
}
public void click4(View v) {
img.setBackgroundResource(R.drawable.landf);
String[] companies = getResources().getStringArray(R.array.company);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, companies));
tv4.setTextColor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
tv2.setTextColor(getResources().getColor(R.color.red));
tv3.setTextColor(getResources().getColor(R.color.red));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_demo2, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
i have some problem here when i want to use my own customize Toolbar instead the default system Toolbar, now i want to use that toolbar on my MainActivity.
i have make my own xml resource file for the toolbar, and i have included it on my MainActivity layout file, but when i run the app i got some error said
android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.toolbar
I don't know what happen, i have try to change the setting on my xml file but still the error appear.
Please master, help me.
Thanks Before.
NB. This my code for app_bar.xml (toolbar resource file)
<android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor">
and this one for my MainActivity.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:background="#fafafa"
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">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f325272f"
android:textStyle="bold" />
<Button
android:id="#+id/btnNewTrans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp"
android:background="#drawable/custom_button"
android:text="#string/NewTransaction" />
<Button
android:id="#+id/btnViewCashflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:background="#drawable/custom_button"
android:text="#string/ViewCashflow" />
<Button
android:id="#+id/btnAddCateg"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_marginBottom="73dp"
android:layout_toLeftOf="#+id/btnRate"
android:layout_toStartOf="#+id/btnRate"
android:background="#drawable/custom_button"
android:text="#string/AddCategory" />
<Button
android:id="#+id/btnRate"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnAddCateg"
android:layout_alignLeft="#+id/spnCategSelect"
android:layout_alignStart="#+id/spnCategSelect"
android:text="#string/RateUs" />
<Button
android:id="#+id/btnSetting"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnRate"
android:layout_toEndOf="#+id/btnRate"
android:layout_toRightOf="#+id/btnRate"
android:text="#string/Setting" />
<Button
android:id="#+id/btnAbout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnSetting"
android:layout_toEndOf="#+id/btnSetting"
android:layout_toRightOf="#+id/btnSetting"
android:text="#string/About" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/Trademark"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f325272f"
android:textStyle="bold" />
<Spinner
android:id="#+id/spnCategSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnAddCateg"
android:layout_alignEnd="#+id/textView"
android:layout_alignLeft="#+id/btnViewCashflow"
android:layout_alignRight="#+id/textView"
android:layout_alignStart="#+id/btnViewCashflow"
android:spinnerMode="dropdown" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNewTrans"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="#string/SelectCategory"
android:textAppearance="?android:attr/textAppearanceSmall" />
and this one for my MainActivity.java
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
private static Button BtnINewTrans;
private static Button BtnIViewCash;
private static Button BtnIAddCateg;
Spinner my_Spinner;
DatabaseHelper dbHelper = new DatabaseHelper(this);
public static String catSelected = null;
private Toolbar toolbar;
//ArrayAdapter<String> adapterCategory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
my_Spinner.setOnItemSelectedListener(this);
select_spinner_Category();
onButtonClickButtonListener();
}
/*ArrayList<String> my_array = new ArrayList<String>();
my_array = getTableValues();*/
/*ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.spinner_row, my_array);
My_spinner.setAdapter(my_Adapter);*/
public void select_spinner_Category () {
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
DatabaseHelper dbH = new DatabaseHelper(getApplicationContext());
List<String> listCategory = dbH.getAllCategory();
ArrayAdapter<String> adapterCategory = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listCategory);
adapterCategory
.setDropDownViewResource(android.R.layout.simple_spinner_item);
my_Spinner.setAdapter(adapterCategory);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id){
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "You selected "+label,
Toast.LENGTH_LONG).show();
catSelected = label;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
public String getCatSelected(){
return catSelected;
}
/*ArrayList<String> arrayCategory;
arrayCategory = dbHelper.getAllCategory();
selectCategory = (Spinner) findViewById(R.id.spnCategSelect);
ArrayAdapter adapterCategory = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayCategory);
// adapterCategory = new ArrayList<String>(this, android.R.layout.simple_spinner_item, R.id.spnCategSelect, AllCategoryList);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectCategory.setAdapter(adapterCategory);
selectCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onButtonClickButtonListener(){
BtnINewTrans = (Button)findViewById(R.id.btnNewTrans);
BtnINewTrans.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentNewTrans = new Intent ("com.example.ever_ncn.cashflow.NewTransaction");
startActivity(intentNewTrans);
}
}
);
BtnIViewCash = (Button)findViewById(R.id.btnViewCashflow);
BtnIViewCash.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentViewCash = new Intent ("com.example.ever_ncn.cashflow.ViewCashflow");
startActivity(intentViewCash);
}
}
);
BtnIAddCateg = (Button)findViewById(R.id.btnAddCateg);
BtnIAddCateg.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.CategorySetting");
startActivity(intentAddCateg);
}
}
);
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Classes are case sensitive: you have android.support.v7.widget.toolbar with a lowercase 't' when it should be android.support.v7.widget.Toolbar
When I am passing image url to Picasso then it shows me error "Target must not be null". When I fetch image from url using Picasso then it shows me error. Even my id assigned to image is correct. At last I replaced the image url with static image, though I receive error as same.
Here is my code:
public class MainScreen extends AppCompatActivity {
private ImageView user_profile_pic,img2;
private ImageView like_button,dislike_button,location_button,refresh;
private ArrayList<String> al;
private ArrayAdapter<String> arrayAdapter;
private int i;
private SwipeFlingAdapterView flingContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
//Setting
user_profile_pic=(ImageView)findViewById(R.id.profile_image);
img2=(ImageView)findViewById(R.id.img2);
//Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg").into(user_profile_pic);
//Picasso.with(getApplicationContext()).load(android.R.drawable.btn_star).into(img);
Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
.placeholder(android.R.drawable.btn_star)
.error(android.R.drawable.btn_star)
.into(img2);
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
al = new ArrayList<>();
al.add("php");
al.add("c");
al.add("python");
al.add("java");
al.add("html");
al.add("c++");
al.add("css");
al.add("javascript");
arrayAdapter = new ArrayAdapter<>(this, R.layout.custom_user_details, R.id.helloText, al );
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
Log.d("LIST", "removed object!");
al.remove(0);
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onLeftCardExit(Object dataObject) {
Toast.makeText(getApplicationContext(),"Left !",Toast.LENGTH_SHORT).show();
}
#Override
public void onRightCardExit(Object dataObject) {
Toast.makeText(getApplicationContext(),"Right !",Toast.LENGTH_SHORT).show();
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
al.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
#Override
public void onScroll(float scrollProgressPercent) {
try
{
View view = flingContainer.getSelectedView();
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
catch (NullPointerException e) {
Log.e("tag", "NullPointerException" + e);
}
}
});
//flingContainer.getTopCardListener().selectRight(); To remove view from right side while pressing button
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_screen, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is what my xml is:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/img2"
android:src="#android:drawable/btn_star"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:padding="8dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vimal, "
android:id="#+id/helloText"
android:textColor="#android:color/black"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="23"
android:textColor="#android:color/black"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#android:drawable/btn_dialog"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#android:drawable/btn_dialog"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:layout_gravity="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/item_swipe_left_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:background="#A5F" />
<View
android:id="#+id/item_swipe_right_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:layout_gravity="right"
android:background="#5AF" />
</FrameLayout>
img2 is null, and you're passing that into the into function.
Add a null check after img2=(ImageView)findViewById(R.id.img2); to check when it stops being null or whether it stays null while you're testing, or just to confirm that it is null.
Check your layout ID's and make sure they're right for ALL the configurations, and make sure you have #+id/img2 specified .
are you sure load the correct layout ? because you have R.id.profile_image in Java file, but i didn't found that id in your xml file.
You can't download image from https using Picasso library.
you can use this library.
try to change it as..
Picasso.with(MainScreen.this).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
.placeholder(android.R.drawable.btn_star)
.error(android.R.drawable.btn_star)
.into(img2);
also make sure that you are using the activity_main_screen.xml layout and check the id if img2 there.
I keep getting the error in my Android Studio Emulator (Nexus_7_API_21) that my app has stopped working whenever i try to run it. I launched LogCat and I think the error is due to a null pointer reference but i don't know where the problem is.
Here is my .java main file.
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
public class MainActivity extends ActionBarActivity
{
RelativeLayout bg;
RadioButton r1;
RadioButton r2;
RadioButton r3;
RadioButton r4;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bg = (RelativeLayout) findViewById(R.id.back);
r1 = (RadioButton) findViewById(R.id.rButton1);
r2 = (RadioButton) findViewById(R.id.rButton2);
r1 = (RadioButton) findViewById(R.id.rButton3);
r1 = (RadioButton) findViewById(R.id.rButton4);
r1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return;
}
});
r2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return;
}
});
r3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return;
}
});
r4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my XML file (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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/back"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#210d0a0d">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/welcomeString"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="180dp"
android:layout_height="150dp" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rButton1"
android:id="#+id/rButton1"
android:layout_gravity="center_vertical"
android:checked="false" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rButton2"
android:id="#+id/rButton2"
android:layout_gravity="center_vertical" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rButton3"
android:id="#+id/rButton3"
android:layout_gravity="center_vertical" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rButton4"
android:id="#+id/rButton4"
android:layout_gravity="center_vertical" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:textSize="25dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
r1 = (RadioButton) findViewById(R.id.rButton3);
r1 = (RadioButton) findViewById(R.id.rButton4);
is problem. You are using r1 instead of r3 and r4.
r3 = (RadioButton) findViewById(R.id.rButton3);
r4 = (RadioButton) findViewById(R.id.rButton4);