AppWidget layout not showing, "widget failed to load" - java

Good day guys, I wonder why when i drag the widget out to my home screen, it only display "widget failed to load" instead of my widget layout. I need help.
Widget.java
package com.fyp.atms;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;
public class Widget extends AppWidgetProvider{
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "Widget deleted!", Toast.LENGTH_SHORT).show();
}
}
widget.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="114dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<TableLayout
android:layout_width="130dp"
android:layout_height="80dp">
<EditText
android:id="#+id/wid_ToTranslate"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Spinner
android:id="#+id/spinner2"
android:layout_width="114dp"
android:layout_height="40dp" />
</TableLayout>
<Button
android:id="#+id/wid_trans"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_weight="50"
android:text="Translate" />
</LinearLayout>
<TextView
android:id="#+id/wid_Translated"
android:layout_width="130dp"
android:layout_height="35dp"
android:ems="10" />
</LinearLayout>
mywidget.xml (AppWidgetProvider)
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="274dp"
android:minHeight="200dp"
android:initialLayout="#layout/widget"
android:updatePeriodMillis="1800000">
</appwidget-provider>

You can't use a Spinner or an EditText in a Widget. That's because every Widget layout must be based on RemoteViews. Following every layout that is supported:
Layout classes
FrameLayout
LinearLayout
RelativeLayout
GridLayout
Widget classes
AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper
Read more about it in the Docs.

The problem is your layout contains an EditText which is not supported by widgets on Android.
For a complete list of views you may use see: http://developer.android.com/guide/topics/appwidgets/

Related

Link CheckBox with Checkable Interface for a ListView

For my Android application I have implemented a ListView that is populated by a Custom Adapter from BaseAdapter and a custom Layout for the rows.
Now i would like to make entries checkable by using the corresponding methods from ListView, like setItemChecked() or getCheckedItemCount().
I created a custom Layout class for the row layout that imlpements Checkable interface and retrieved the Button using findViewById to be able to access it inside the overridden methods from Checkable.
When long clicking on an entry in the list, the methods get invoked, but I get a NullpointerException, saying the CheckBox is referencing null:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CompoundButton.setChecked(boolean)' on a null object reference
What is the appropriate way to link the checkbox of each entry inside a checkable Layout?
The checkable Layout class looks like this:
package com.lastname.name.arcadaobjecttracker;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.LinearLayout;
//extend the Layout for the row view (each element in the listView) to add support for checkable rows (called by the listView methods like setItemChecked() etc.)
public class CheckableRowLayout extends LinearLayout implements Checkable
{
View view;
CheckBox checkBox;
public CheckableRowLayout(Context context)
{
super(context);
init(context);
}
public CheckableRowLayout(Context context, #Nullable AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public CheckableRowLayout(Context context, #Nullable AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context)
{
checkBox = (CheckBox) findViewById(R.id.checkBox);
}
#Override
public void setChecked(boolean checked)
{
checkBox.setChecked(checked);
}
#Override
public boolean isChecked()
{
return checkBox.isChecked();
}
#Override
public void toggle()
{
checkBox.toggle();
}
}
The Layout consists of the following xml file with checkbox:
<?xml version="1.0" encoding="utf-8"?>
<com.lastname.name.arcadaobjecttracker.CheckableRowLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="visible" />
<include
layout="#layout/list_view_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.lastname.name.arcadaobjecttracker.CheckableRowLayout>
which includes the rest of the layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/thumbnailImageView"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_videocam_black_24dp" />
<TextView
android:id="#+id/nameTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:singleLine="true"
android:text="Thisisaverylongnameforavideofilethatforsureexceedsthetextviewsize"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/thumbnailImageView"
app:layout_constraintTop_toTopOf="#+id/thumbnailImageView" />
<TextView
android:id="#+id/dateTextView"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:singleLine="true"
android:text="2018-01-01"
app:layout_constraintBottom_toBottomOf="#+id/thumbnailImageView"
app:layout_constraintStart_toEndOf="#+id/thumbnailImageView" />
<TextView
android:id="#+id/durationTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:singleLine="true"
android:text="Duration"
android:textAlignment="textStart"
app:layout_constraintBottom_toBottomOf="#+id/dateTextView"
app:layout_constraintStart_toEndOf="#+id/dateTextView" />
<TextView
android:id="#+id/hasTrackingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textAlignment="viewEnd"
app:layout_constraintBottom_toBottomOf="#+id/durationTextView"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.42" />
</android.support.constraint.ConstraintLayout>

How to open fragments containing webViews in another activity on button click from fragments of another activity?

I have image-buttons in one of the fragments of an activity and want to open corresponding fragments containing webViews on button clicks in another activity.
I am a beginner so please do give appropriate codes for this.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.administrator.hiha.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar">
</include>
<fragment
android:id="#+id/upperFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.example.administrator.hiha.Upper_main_Fragment"
android:layout_below="#id/toolbar"
>
</fragment>
<fragment
android:layout_below="#id/upperFragment"
android:id="#+id/lowerFragment"
android:layout_width="match_parent"
android:layout_height="140dp"
android:name="com.example.administrator.hiha.Lower_main_Fragment"
>
</fragment>
<fragment
android:layout_below="#id/lowerFragment"
android:id="#+id/Bottom_Most_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.administrator.hiha.BottomMostFragment"
tools:layout="#layout/fragment_bottom_most">
</fragment>
<View
android:id="#+id/hLastRow"
android:layout_centerHorizontal="true"
android:layout_below="#id/Bottom_Most_Fragment"
android:layout_width="300dp"
android:layout_height="2dp"
android:background="#EEEEEE"
/>
<TextView
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/hLastRow"
android:layout_centerHorizontal="true"
android:text="Designed \u0026 Developed by me © 2016."/>
</RelativeLayout>
</ScrollView>
MainActivity.java
package com.example.administrator.hiha;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
//Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar=(Toolbar)findViewById(R.id.toolbar);
}
}
fragment_lower_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/about_board"
android:id="#+id/about_img_btn"
android:onClick="onAboutBoardClick"
/>
<TextView
android:id="#+id/text_about_img_btn"
android:text="About Board"
android:layout_below="#id/about_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/sarasvati"
android:id="#+id/sarasvati_img_btn"
android:layout_toRightOf="#id/about_img_btn"
android:onClick="onSarasvatiClick"
/>
<TextView
android:layout_toRightOf="#id/text_about_img_btn"
android:id="#+id/text_sarasvati_img_btn"
android:text="Sarasvati"
android:layout_below="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/scientific_evidences"
android:id="#+id/scientific_evidences_img_btn"
android:onClick="onScientificEvidencesClick"
/>
<TextView
android:layout_toRightOf="#id/text_sarasvati_img_btn"
android:id="#+id/text_scientific_evidences_img_btn"
android:text="Scientific EVidences"
android:layout_below="#id/scientific_evidences_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/digital_library_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/affltd_organization"
android:id="#+id/affltd_oragnization_img_btn"
android:onClick="onAffltdOrganizationClick"
/>
<TextView
android:layout_toRightOf="#id/text_digital_library_img_btn"
android:id="#+id/text_affltd_oragnization_img_btn"
android:text="Affiliated Organizations"
android:layout_below="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/tender"
android:id="#+id/tender_img_btn"
android:onClick="onTenderClick"
/>
<TextView
android:layout_toRightOf="#id/text_affltd_oragnization_img_btn"
android:id="#+id/text_tender_img_btn"
android:text="Tenders"
android:layout_below="#id/tender_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
Lower_Main_Fragment.java
package com.example.administrator.hiha;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class Lower_main_Fragment extends Fragment {
ImageButton about_img_btn;
ImageButton sarasvati_img_btn;
ImageButton scientific_evidences_img_btn;
ImageButton digital_library_img_btn;
ImageButton affltd_oragnization_img_btn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView=inflater.inflate(R.layout.fragment_lower_main_,container,false);
//cast image buttons
about_img_btn=(ImageButton)getView().findViewById(R.id.about_img_btn);
sarasvati_img_btn=(ImageButton)getView().findViewById(R.id.sarasvati_img_btn);
scientific_evidences_img_btn=(ImageButton)getView().findViewById(R.id.scientific_evidences_img_btn);
digital_library_img_btn=(ImageButton)getView().findViewById(R.id.digital_library_img_btn);
affltd_oragnization_img_btn=(ImageButton)getView().findViewById(R.id.affltd_oragnization_img_btn);
return rootView;
}
//onclick methods of image buttons
public void onAboutBoardClick(View view){
}
}
These are the files in which i want that if i click on about_img_btn then it should open a fragment containing a webView in another activity. The same should be repeated for all the buttons.
Please define the further code that i have to use and mention the files where i have to use it. Thanks!
try this:
in your fragment_lower_main xml:
Give an id to the Framelayout:
<?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"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<FrameLayout
android:id="+#id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<--Rest of the code -->
/>
</LinearLayout>
Now in code open new Fragment during button click using:
about_img_btn=(ImageButton)rootView.findViewById(R.id.about_img_btn);
about_img_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AboutUS fragment = new AboutUS();
Replace_fragment(fragment); //pass the fragment u want to replace
}
});
public void ReplaceFragment(Fragment r_fragment) {
FragmentManager fragmentManager = getActivity.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container, r_fragment).commit();
}

Add button at end Listview android

so , i have a Listview activity , that is populated by values from a SQLITE database.
how can i put a button fixed at top or bottom ?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/coligada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_alignBottom="#+id/editar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"/>
<Button
android:id="#+id/editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantidadesxsx"
android:onClick="editar"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/coligada"
android:layout_toEndOf="#+id/coligada" />
</RelativeLayout>
Here is my ProductAdapter that have custom layout where have the items and a apdater that populate my listview with values from Sqlite database.
package br.exemplosqlite;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;
public class ProdutosAdapter extends BaseAdapter {
private Context context;
private List<Produtos> list;
public ProdutosAdapter(Context context, List<Produtos> list){
this.context = context;
this.list = list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0).getId();
}
#Override
public View getView(int position, View arg1, ViewGroup arg2) {
final int auxPosition = position;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.produtos, null);
TextView tv = (TextView) layout.findViewById(R.id.coligada);
tv.setText(list.get(position).getItem());
Button editarBt = (Button) layout.findViewById(R.id.editar);
editarBt.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NewUserActivity.class);
intent.putExtra("item", list.get(auxPosition).getItem());
intent.putExtra("quantidaderm",list.get(auxPosition).getQuantidaderm());
intent.putExtra("id", list.get(auxPosition).getId());
context.startActivity(intent);
}
});
// *********** BOTÃO DE EXCLUIR ***************** //
//Button deletarBt = (Button) layout.findViewById(R.id.deletar);
//deletarBt.setOnClickListener(new Button.OnClickListener(){
// #Override
// public void onClick(View arg0) {
// BD bd = new BD(context);
// bd.deletar(list.get(auxPosition));
//
// layout.setVisibility(View.GONE);
// }
//}
///);
return layout;
}
}
Try the following xml at the bottom of ListView and centerHorizontal
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Your Button"
/>
</RelativeLayout>
Hope this helps.
Do it like this:
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your text here"/>
</LinearLayout>
Try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:orientation="horizontal" >
<TextView
android:id="#+id/coligada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:text="Large Text" />
<Button
android:id="#+id/editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/coligada"
android:layout_toRightOf="#+id/coligada"
android:onClick="editar"
android:text="Quantidadesxsx" />
</LinearLayout>
</LinearLayout>
Try this.
<Button
android:id="#+id/btn"
layout_width="wrap_content"
layout_height="wrap_content"
android:alignParentTop="true"
/>
<ListView
layout_width="wrap_content"
layout_height="wrap_content"
android:layout_below="#+id/btn"
/>
Current layout is for listview items.
Remove Button from this layout file and paste it in layout file where you have defined ListView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<ListView
.....
</ListView>
<Button>
</Button>
</RelativeLayout>
in one layout and
<TextView>
</TextView>
in another layout
In button add this line of code
<Button
android:id="#+id/editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantidadesxsx"
android:onClick="editar"
android:layout_below="#+id/coligada"/>
You can use android:layout_weight="1" with LinearLayout.
Example.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantidadesxsx"
android:onClick="editar"/>
</LinearLayout>

Android: EditText in CustomView not behaving as expected when touched

I have a CustomView which contains a LinearLayout that holds an EditText & another custom view element. However, when I run my app and touch the EditText it does not behave as expected (doesn't appear to receive focus & the soft keyboard doesn't open). I've tried setting duplicateParentState="true" on both the EditText & the LinearLayout. I also attached an onTouchEventListener to the EditText that calls a simple toast & the toast did show up.
Here's the code for my CustomView.
form_field.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_marginBottom="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/grey_rounded_borders">
<EditText
android:id="#+id/edit_text"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:inputType="text"
android:padding="10dp"
android:textColor="#2d6169"
android:textSize="18sp"
android:background="#color/transparent" />
<RelativeLayout
android:layout_marginStart="5dp"
android:layout_marginEnd="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<com.example.app.ValidationIcon
android:id="#+id/validation_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
tools:ignore="ContentDescription" />
</RelativeLayout>
</LinearLayout>
FormField.java
package com.example.app;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class FormField extends LinearLayout {
private EditText editText;
private ValidationIcon validationIcon;
private Integer fieldInputType;
private String fieldInputHint;
public FormField(Context context)
{
super(context);
}
public FormField(Context context, AttributeSet attributeSet)
{
super(context, attributeSet);
TypedArray attrs = context.obtainStyledAttributes(attributeSet, R.styleable.FormField, 0, 0);
fieldInputType = attrs.getInt(
R.styleable.FormField_fieldInputType,
InputType.TYPE_TEXT_VARIATION_NORMAL
);
fieldInputHint = attrs.getString(
R.styleable.FormField_fieldInputHint
);
attrs.recycle();
inflate(getContext(), R.layout.form_field, this);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
editText = (EditText)findViewById(R.id.edit_text);
editText.setInputType(fieldInputType);
editText.setHint(fieldInputHint);
editText.setFocusableInTouchMode(true);
validationIcon = (ValidationIcon)findViewById(R.id.validation_icon);
validationIcon.setValid(true);
ArrayList<View> touchables = new ArrayList<View>();
touchables.add(this);
touchables.add(editText);
addTouchables(touchables);
}
public FormField(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
}
registration_layout.xml
<RelativeLayout android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<RelativeLayout android:id="#+id/account_details"
android:layout_marginBottom="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.app.FormField
android:id="#+id/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:fieldInputHint="#string/name_field_hint" />
<com.example.app.FormField
android:id="#+id/email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:fieldInputHint="#string/email_field_hint" />
<com.example.app.FormField
android:id="#+id/password_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:fieldInputHint="#string/password_field_hint" />
<com.example.app.FormField
android:id="#+id/re_password_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:fieldInputHint="#string/re_password_field_hint" />
</RelativeLayout>
</RelativeLayout>
Min SDK Version is set at 14, target is set at 20. Any help would be greatly appreciated!
EDIT:
I got this logcat message today while testing long presses on my FormFields.
W/TextView﹕ TextView does not support text selection. Action mode cancelled.
I double checked my code and the EditText element is only ever cast as an EditText. If the EditText is being cast to a TextView that would explain my original issues but now I'm confused as to why or how it was cast as a TextView.
It looks like you are extending LinearLayout but never using it in your layout. From what you've posted, you aren't using the FormField at all and the only custom View I see is the ValidationIcon view. So hooking the touch events into EditText and ValidationIcon aren't occurring because you aren't using FormField in your layout.xml.
Rather than extending LinearLayout, why not just use the predefined methods and attributes to handle the behaviour you are trying to achieve. For example, displaying keyboard, requesting focus, and displaying hint:
<EditText
android:id="#+id/edit_text"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
**android:inputType="text"**
**android:hint="#styleable/FormField_fieldInputHint"**
android:padding="10dp"
android:textColor="#2d6169"
android:textSize="18sp"
android:background="#color/transparent" />
The keyboard should display when the edit text receives focus, if not you could programmatically do this by requesting focus and handling the IME manager call. For example, from the Activity/Fragment:
//call this from onViewCreated to grab focus and begin the flow
editText.requestFocus();
//set focus listener to handle keyboard display
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
} else {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromWindow(myEditText.getWindowToken(), 0);
}
});
Make sure have not been setting inputType:'none' or '0X000000'
use this code for custom Edittext with title
custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:foregroundGravity="right"
app:layout_anchorGravity="right">
<LinearLayout
android:orientation="vertical"
android:id="#+id/search_closed_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|right"
android:background="#color/white"
android:foregroundGravity="center"
android:gravity="right">
<TextView
android:id="#+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_marginStart="16dp"
android:layout_marginTop="14dp"
android:fontFamily="#font/intel"
android:foregroundGravity="center"
android:text="textview"
android:textColor="#color/title_color"
android:textSize="14dp"
android:textStyle="bold" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/lay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:clickable="true"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/edt_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/edit_txtbg"
android:fontFamily="#font/intel"
android:gravity="center|start"
android:paddingStart="24dp"
android:textColor="#color/text_color"
android:textColorHint="#color/txt_hint"
android:textSize="#dimen/text_nev"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/img_add_patient"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</FrameLayout>
CustomEditText.kt
package com.mdppractice.custom
import android.content.Context
import android.text.InputType
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
import com.mdppractice.R
class CustomEditText(
context: Context,
attrs: AttributeSet,
) : FrameLayout(context, attrs) {
private var txt_title: TextView
private var edt_text: EditText
init {
LayoutInflater.from(context)
.inflate(R.layout.custom_view, this, true)
txt_title = findViewById(R.id.txt_title)
edt_text = findViewById(R.id.edt_text)
val a = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText)
txt_title.text = a.getString(R.styleable.CustomEditText_title)
edt_text.hint = a.getString(R.styleable.CustomEditText_hint)
when(a.getString(R.styleable.CustomEditText_inputType)) {
"number" -> edt_text.inputType = InputType.TYPE_CLASS_PHONE
"textCapSentences" -> edt_text.inputType = InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
"text" -> edt_text.inputType = InputType.TYPE_CLASS_TEXT
}
a.recycle()
}
fun setmInputType(type: Int) {
edt_text.setRawInputType(InputType.TYPE_CLASS_TEXT)
setmInputType(type)
}
fun setTitle(title: Int) {
txt_title.setText(title)
}
fun set_Text(title: Int) {
edt_text.setText(title)
}
fun getTitle(): String {
return txt_title.text.toString()
}
fun get_Text(): String {
return edt_text.text.toString()
}
}
main/../attr.xml
<declare-styleable name="CustomEditText">
<attr name="title" format="string"/>
<attr name="onCenter" format="boolean"/>
<attr name="hint" format="string"/>
<attr name="maxLength" format="integer"/>
<attr name="inputType" format="string"/>
</declare-styleable>
#drawable/edit_txtbg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/listview_background_shape">
<stroke android:width="1dp" android:color="#color/button_bg_border" />
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<corners android:radius="6dp"/>
<solid android:color="#color/button_bg" />
</shape>
use in layout
<com.mdppractice.custom.CustomEditText
android:id="#+id/edt_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="Patient ID/Phone"
android:autofillHints="#string/verified"
android:foregroundGravity="center"
android:hint="Please enter name"
app:inputType="textCapSentences"
android:textColor="#color/title_color"
android:textSize="14dp"
android:textStyle="bold" />
thanks you

How to add icon to tabs

I am trying to add an icon to my tabs and it isnt working,
I tried adding
getResources().getDrawable(R.drawable.bodyicon));
and it still isnt working. When I run the application the tab bar just has the text and there is no icon
I outlined where my code is. If someone could figure out why it isnt working that would be great.
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Body extends Activity implements OnTouchListener, OnClickListener {
Button bAbs, bQuad2, bPecs;
TabHost th;
ImageView body;
final Context context = this;
StretchType pop;
// above I am defining all of the buttons and images, etc...
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// here I am saying get rid of the nav bar
setContentView(R.layout.body);
th = (TabHost) findViewById(R.id.thbodyview);
bAbs = (Button) findViewById(R.id.bAbss);
bPecs = (Button) findViewById(R.id.bPecss);
bAbs.setOnClickListener(this);
bPecs.setOnClickListener(this);
//*********************below are my tabs*******************************************
th.setup();
TabSpec specs = th.newTabSpec("Front");
specs.setContent(R.id.Front);
specs.setIndicator("Front",getResources().getDrawable(R.drawable.bodyicon));
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.Back);
specs.setIndicator("Back");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("More...");
th.addTab(specs);
}
//***************************Above are my tabs *******************************
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bAbss:
// below I am creating the alert dialogue
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.abs, null);
// above I am setting the customview of the alert dialogue
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setView(promptsView);
// here I am officially setting the custom layout
// set dialog message
alertDialogBuilder.setTitle("Stretch Type");
// alert dialogue title
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
// above is creating the alert dialogue
final Spinner mSpinner = (Spinner) promptsView
.findViewById(R.id.sSType);
// above is initializing the spinner
/*
* dont need this button final Button mButton = (Button) promptsView
* .findViewById(R.id.bSpinclose);
*/
// reference UI elements from my_dialog_layout in similar fashion
mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());
// show it
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);
break;
}
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
public class OnSpinnerItemClicked implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
switch (pos) {
case (1):
Intent i = new Intent(Body.this, Practice.class);
Body.this.startActivity(i);
break;
}
}
#Override
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
Here is the xml
<TabHost
android:id="#+id/thbodyview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/Front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/llbottomtop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<TextView
android:id="#+id/tvupper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/bAbs"
android:text="Upper"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_centerVertical="true"
android:background="#color/orange" />
<TextView
android:id="#+id/tvlower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:text="Lower"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
</LinearLayout>
<ImageView
android:id="#+id/ivBody"
android:layout_width="210dp"
android:layout_height="430dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/body_front" />
<Button
android:id="#+id/bPecss"
android:layout_width="83dp"
android:layout_height="50dp"
android:layout_above="#+id/bAbss"
android:layout_alignRight="#+id/bAbss"
android:text="Pecs" />
<Button
android:id="#+id/bAbss"
android:layout_width="80dp"
android:layout_height="77dp"
android:layout_alignBottom="#+id/llbottomtop"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:text="Abs" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/Back"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/llbottomtop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<TextView
android:id="#+id/tvupper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/bAbs"
android:text="Upper"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_centerVertical="true"
android:background="#color/orange" />
<TextView
android:id="#+id/tvlower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:text="Lower"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
</LinearLayout>
<ImageView
android:id="#+id/ivBody"
android:layout_width="210dp"
android:layout_height="430dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/body_back" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
My tip is to get access to the tab title textview and set left|top|right|bottom drawable
Example:
//to get first tab title as textView from tabHost
TextView tabTitle = (TextView) tabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.title);
//to set the icon to left of the tab title
tabTitle.setCompoundDrawablesWithIntrinsicBounds(
getActivity().getResources().getDrawable(
R.drawable.ic_tab_icon), null, null, null);
I have two physical devices one 2.2 and the other 4.2.2. on 2.2 there is no issue and the icon appears. on 4.2.2 there is no icon. Somethings changed in how tabs are laid out in Android. If you really required to use a TabHost i saw you can pass in a view as the tabindicator. This code is kinda pseudo code as it still some adjustment in the UI in terms of layout but try this:
define a layout called tabindicator_layout.xml and put it in res/layout. the contents of which look like this:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:scaleType="center" >
</ImageView>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
then in your class code at runtime inflate this view and populate the image and textview with what you desire. do this for each tab:
//*********************below are my tabs*******************************************
th.setup();
TabSpec specs = th.newTabSpec("Front");
specs.setContent(R.id.Front);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tabindicator_layout, th.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.title)).setText("Front");
((ImageView) tabIndicator.findViewById(R.id.image))
.setImageResource(R.drawable.ic_launcher);
specs.setIndicator(tabIndicator);
TabSpec specs2 = th.newTabSpec("tag2");
specs2.setContent(R.id.Back);
View tabIndicator2 = LayoutInflater.from(this).inflate(
R.layout.tabindicator_layout, th.getTabWidget(), false);
((TextView) tabIndicator2.findViewById(R.id.title)).setText("Back");
((ImageView) tabIndicator2.findViewById(R.id.image))
.setImageResource(R.drawable.ic_launcher);
specs2.setIndicator(tabIndicator2);
TabSpec specs3 = th.newTabSpec("tag3");
specs3.setContent(R.id.tab3);
View tabIndicator3 = LayoutInflater.from(this).inflate(
R.layout.tabindicator_layout, th.getTabWidget(), false);
((TextView) tabIndicator3.findViewById(R.id.title)).setText("...More");
((ImageView) tabIndicator3.findViewById(R.id.image))
.setImageResource(R.drawable.ic_launcher);
specs3.setIndicator(tabIndicator3);
th.addTab(specs);
th.addTab(specs2);
th.addTab(specs3);
of course replace R.drawable.ic_launcher with whatever icon you want to show
another interesting thing that might help..i notice if i pass in a blank for the title that the original way works but shows no title. So you could go into photoshop and just add the title to the image and make it even better looking. Try this way:
TabSpec specs = th.newTabSpec("Front");
specs.setContent(R.id.Front);
specs.setIndicator("",getResources().getDrawable(R.drawable.ic_launcher));
th.addTab(specs);

Categories