I'm creating a small animation app using Android studio. When a button press images should work according to the particular animation.
There's an error with line viewGroup = (viewGroup)findViewById(R.id.ViewGroup);
It shows "Cannot resolve symbol ViewGroup". I typed Alt+Enter and selected a solution. But nothing happened.
public class MainActivity extends Activity {
private ViewGroup viewGroup;
private ImageView imageView, imageView2, imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9,imageView10,imageView11,imageView12;
private Button button,button1;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
imageView= (ImageView) findViewById(R.id.imageView);
imageView2= (ImageView) findViewById(R.id.imageView2);
imageView3= (ImageView) findViewById(R.id.imageView3);
imageView4= (ImageView) findViewById(R.id.imageView4);
imageView5= (ImageView) findViewById(R.id.imageView5);
imageView6=(ImageView) findViewById(R.id.imageView6);
imageView7=(ImageView) findViewById(R.id.imageView7);
imageView8=(ImageView) findViewById(R.id.imageView8);
imageView9=(ImageView) findViewById(R.id.imageView9);
imageView10=(ImageView) findViewById(R.id.imageView10);
imageView11=(ImageView) findViewById(R.id.imageView11);
imageView12=(ImageView) findViewById(R.id.imageView12);
button = (Button)findViewById(R.id.button);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TransitionManager.beginDelayedTransition(viewGroup, new Fade());
fade(imageView, imageView2, imageView3,imageView4,imageView5,imageView6);
}
});
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
TransitionManager.beginDelayedTransition(viewGroup, new Slide());
toggle(imageView7,imageView8,imageView9,imageView10,imageView11,imageView12);
}
});}
private static void toggle(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
}
private static void fade(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
Here's my 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:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/myAniLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button1"
android:layout_alignBottom="#+id/button"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#mipmap/ic_launcher"
android:layout_alignTop="#+id/imageView2"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView7"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#android:drawable/star_big_on" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView8"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView9"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView10"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView9"
android:layout_alignStart="#+id/button1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView11"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView8"
android:layout_alignEnd="#+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView12"
android:src="#android:drawable/star_big_on"
android:layout_above="#+id/button"
android:layout_alignStart="#+id/imageView4" />
Your logcat shows
"Cannot resolve symbol 'ViewGroup'"
viewGroup=(viewGroup)findViewById(R.id.ViewGroup); // (ViewGroup)
viewGroup Not declare in your XML
(viewGroup) must be (ViewGroup) . (viewGroup)
The ViewGroup class is a subclass of the View class. ViewGroup
instances work as containers for View instances to group View
instances together.
http://developer.android.com/intl/es/reference/android/view/ViewGroup.html
I don't see an element ViewGroup in the xml. You need to add it in the same xml or include it.
As per your xml there is not an any id with ViewGroup in your xml code. please give id to your layout with ViewGroup and cast it as below,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
there was also a spelling mistake in your line,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
As they pointed out, you have no R.id.ViewGroup in your layout.
If you really need the view group try this:
viewGroup = (ViewGroup)findViewById(android.R.id.content)
The problem is that you have,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
and you have to change it for this,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
The V must be a capital letter.
Also watch for the id of your Viewgroup in your xml, check if it is R.id.Viewgroup or R.id.viewgroup
Related
I want to test if the button works on my application.
They are placed inside my first fragment. When I click "All" Button it does not respond at all. My main goal is to load fragment,but in this case I put System.out.println just to test if my button is going to work,but it obviously is not working.
I have tried to make new project and it works,but only inside Main Activity,I want this inside me Exercises_Tab fragment.
I do not see any error logs at all..
public class Exercises_Tab extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View Exercises = inflater.inflate(R.layout.fragment_1,container,false);
Button allbtn = (Button) Exercises.findViewById(R.id.all_button);
allbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Test");
}
});
return Exercises;
}
}
And my XML:
<LinearLayout
android:id="#+id/Linear_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= " Muscles "
android:background="#000000"
android:textColor="#ffffff"
android:textSize="25dp"
android:layout_marginTop="3dp"/>
<HorizontalScrollView
android:id="#+id/scrollViewhorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="-20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"
android:background="#fafafa"
android:text="All"
android:textColor="#696969"
android:id="#+id/all_button"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:id="#+id/second_button"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"
android:background="#fafafa"
android:text="Button 1"
android:textColor="#696969" />
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Button 1"
android:background="#fafafa"
android:textColor="#696969"
android:layout_margin="-20dp"
android:layout_marginRight="2dp"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<FrameLayout
android:id="#+id/Frame_Layout"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimary"
android:layout_below="#+id/Linear_Layout"
>
</FrameLayout>
Perform your entire code inside this function for fragment as it already has a reference to your xml view.
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button allbtn = (Button) view.findViewById(R.id.all_button);
allbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Test");
}
});
}
Simply you can implement the click listener like
public class Exercises_Tab extends Fragment implements OnClickListener {
Button allbtn ;
#Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View Exercises = inflater.inflate(R.layout.fragment_1,container,false);
allbtn = (Button) Exercises.findViewById(R.id.all_button);
allbtn .setOnClickListener(this);
return Exercises ;
}
#Override
public void onClick(View v) {
// implements your things
}
}
I prefer this because it makes your code more readable.
In my app it has a custom ListView'. The ListView contains an Image button. At the moment if the user is using an Android phone or tablet they can select and press this Button.
But if they are using for example a Firestick that uses a remote I can't select it. How can I make sure that I can scroll through my list but also make sure it scrolls through the buttons as well when using a remote?
My custom.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/titleapp"
android:textSize="20dp"
android:textColor="#drawable/button_textcolour"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="12dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/img" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/detail"
android:layout_margin="6dp"
android:textSize="15dp"
android:layout_below="#+id/titleapp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/img"
android:layout_toEndOf="#+id/img"
android:textColor="#drawable/button_textcolour"/>
<ImageView
android:layout_width="45dp"
android:padding="5dp"
android:id="#+id/img"
android:scaleType="fitCenter"
android:cropToPadding="true"
android:adjustViewBounds="true"
android:layout_height="45dp"
android:layout_alignBottom="#+id/detail"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/downloads"
android:id="#+id/downloadcounttext"
android:layout_below="#+id/detail"
android:layout_alignEnd="#+id/detail"
android:layout_marginRight="50dp"
android:textColor="#drawable/button_textcolour"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginLeft="5dp"
android:id="#+id/counterdl"
android:layout_below="#+id/detail"
android:layout_alignEnd="#+id/detail"
android:layout_marginEnd="37dp"
android:textColor="#drawable/button_textcolour"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/date_added"
android:id="#+id/dateaddedtxt"
android:layout_below="#+id/detail"
android:layout_alignStart="#+id/detail"
android:textColor="#drawable/button_textcolour"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="01/01/0000"
android:id="#+id/datetext"
android:layout_below="#+id/detail"
android:layout_toEndOf="#+id/dateaddedtxt"
android:textColor="#drawable/button_textcolour"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Report?"
android:paddingRight="5dp"
android:layout_alignTop="#+id/img"
android:layout_toStartOf="#+id/report"
android:layout_alignParentTop="true"
android:layout_margin="5dp" />
//HERE I WANT THIS BUTTON ON EVERY ITEM TO BE ABLE TO GAIN FOCUS WITH IN THE LIST
<ImageButton
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/report"
android:background="#drawable/ic_sentiment_dissatisfied_black_24dp"
android:layout_alignEnd="#+id/detail" />
My method in my BaseAdapter that allows selection of the button:
public View getView(final int position, View convertView, ViewGroup parent) { final View row;
if (convertView == null) {
row = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom, parent, false);
} else {
row = convertView;
}
report = row.findViewById(R.id.report);
report.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
title.setText(Title[position]);
String getappname = title.getText().toString();
if (getappname!= null){
Log.i("Bad App ", getappname);
Im trying to add an ImageButton to each row in my ListView (which I implemented via CustomAdapter). Each button should open a different WebView. I can't seem to find any tutorials to do this so any advice/tips/links would be highly apreciated.
EDIT 1:
My Listview XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_x="15dp">
<ImageView
android:layout_width="30dp"
app:srcCompat="#drawable/a"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:id="#+id/local"
android:layout_height="30dp"
android:layout_alignBottom="#+id/versus"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/resultsiz"
android:textColor="#android:color/white"
android:textStyle="normal|bold"
android:gravity="center_vertical"
android:elevation="15dp"
android:layout_alignBaseline="#+id/resultsde"
android:layout_alignBottom="#+id/resultsde"
android:layout_alignRight="#+id/resultsde"
android:layout_alignEnd="#+id/resultsde"
android:layout_marginRight="61dp"
android:layout_marginEnd="61dp" />
<ImageView
android:layout_width="15dp"
app:srcCompat="#drawable/a"
android:id="#+id/versus"
android:layout_height="15dp"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_alignBottom="#+id/visit"
android:layout_toRightOf="#+id/local"
android:layout_toEndOf="#+id/local" />
<ImageView
android:layout_width="30dp"
app:srcCompat="#drawable/a"
android:id="#+id/visit"
android:layout_height="30dp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_alignBottom="#+id/resultsiz"
android:layout_toRightOf="#+id/versus"
android:layout_toEndOf="#+id/versus" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/local"
android:layout_alignStart="#+id/local"
android:id="#+id/dia"
android:textColor="#color/white"
android:textStyle="normal|bold"
android:textAllCaps="true" />
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="#+id/resultsde"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp"
android:textColor="#android:color/white"
android:layout_marginTop="19dp"
android:textStyle="normal|bold"
android:textAlignment="viewEnd"
android:gravity="end"
android:layout_width="70dp"
android:layout_below="#+id/dia"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/local"
android:layout_toRightOf="#+id/local"
android:layout_toEndOf="#+id/local"
android:layout_marginTop="155dp"
android:id="#+id/stats" />
</RelativeLayout>
And my CustomAdapter:
protected void onPostExecute(Void aVoid) {
CustomAdapter customAdapter = new CustomAdapter();
lista.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter{
#Override
public int getCount() {
return resultsizq.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
view = getLayoutInflater().inflate(R.layout.customlayout,null);
ImageView versus = (ImageView)view.findViewById(R.id.versus);
ImageView local = (ImageView)view.findViewById(R.id.local);
ImageView visit = (ImageView)view.findViewById(R.id.visit);
TextView dia= (TextView)view.findViewById(R.id.dia);
TextView resultsiz= (TextView)view.findViewById(R.id.resultsiz);
TextView resultsde= (TextView)view.findViewById(R.id.resultsde);
dia.setText(di[position]);
resultsiz.setText(resultsizq[position]);
resultsde.setText(resultsder[position]);
versus.setImageResource(versu[position]);
local.setImageResource(loc[position]);
visit.setImageResource(vis[position]);
if ((position+1)%4==0){
view.setPadding(0,0,0,150);
}
if ((position)%4==0){
view.setPadding(0,150,0,0);
}
return view;
}}
In the getView method, you should assign the onClick function to the Button according to the given position. Probably, you should get the item located at position, generate some url based on the properties of this item, and let this url be opened in the onClick function.
More than that, I have 3 suggestions to improve your code:
Try to use RecyclerView instead of ListView.
Even if you use ListView, have a look at View Holder pattern. This is a good place to start.
Consider using Butterknife, which increases the readability of your code. If I were you, I would keep the item as a property in the view holder object, which has a function annotated with #OnClick(R.id.stats) so that it opens the preferred WebView accordingly.
I hope these help.
I am using viewPager and fragments to develop an application . In one screen I use DialogFragmentto display an alert message .
My DialogFragment code is below,
public class ErrorFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.error_fragment, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setLayout(350,300);
Button button=(Button)rootView.findViewById(R.id.yesButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Print -"," Ok");
if(getActivity()!=null){
Log.d("Print -"," Ok2");
((LoginActivity)getActivity()).setCurrentItem(4);
Log.d("Print -", " Ok4");
}
Log.d("Print -"," Ok3");
dismiss();
}
});
return rootView;
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
Activity a = getActivity();
if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
In my button action , I have program to open another fragment however nothing is happening except DialogFragment getting closed.
Below is my layout,
<?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="#drawable/button_border">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/errorContent"
android:id="#+id/errorTextView1"
android:textSize="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="30dp"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Space
android:layout_width="match_parent"
android:layout_height="36px"
android:layout_below="#id/errorTextView1"
android:id="#+id/space4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_border_2"
android:id="#+id/yesButton"
android:text=" Yes "
android:layout_below="#+id/space4"
android:textColor="#android:color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20dp"
android:layout_marginLeft="46dp"
android:layout_marginStart="46dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_border_2"
android:id="#+id/noButton"
android:text=" No "
android:layout_below="#+id/space4"
android:textColor="#android:color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20dp"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp"
android:layout_alignTop="#+id/yesButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Space
android:layout_width="match_parent"
android:layout_height="36px"
android:layout_below="#id/noButton"/>
</RelativeLayout>
How can I open other fragment by clicking particular button in DialogFragment ?
Even though fragment shifting is not happening the console print inside the button action is getting printed.
I'm working on my first Android project, which means that I'm not that familiar with Android and Java, and I want to move scroll of ScrollView after a user presses the button for next action (IME_ACTION_SEND). My codes are as follows.
activity_add.xml
It basically consists of TextView, EditText, NumberPicker, Spinner, Button. I want to move the scroll after IME_ACTION_SEND on EditText, so that the NumberPicker is centered on the screen.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/addActivity_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<LinearLayout
android:id="#+id/layout_activity_add"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_addItem"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="40sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout"
android:layout_marginTop="50dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine = "true"
android:maxLength="20" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout3"
android:layout_marginTop="50dp">
<TextView
android:id="#+id/period_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_period"
android:layout_centerHorizontal="true" />
<NumberPicker
android:id="#+id/period_number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/period_textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</NumberPicker>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout4"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_category"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_toStartOf="#+id/background_spinner"
android:layout_toLeftOf="#+id/background_spinner" />
<Spinner
android:id="#+id/background_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical">
</Spinner>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_submit"
android:layout_gravity="center_horizontal"
android:text="#string/label_submit" />
</LinearLayout>
</ScrollView>
AddItemActivity.java
I've just copied the part which I think is relevant.
public class AddSinceItemActivity extends ActionBarActivity {
EditText title;
Spinner spinner;
NumberPicker numberPicker;
String title_string;
ViewGroup linearLayout;
ScrollView addActivity_scrollview;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
addActivity_scrollview = (ScrollView) findViewById(R.id.addActivity_scrollview);
linearLayout = (ViewGroup) findViewById(R.id.layout_activity_add);
title = (EditText) findViewById(R.id.title_editText);
numberPicker = (NumberPicker) findViewById(R.id.period_number_picker);
spinner = (Spinner) findViewById(R.id.background_spinner);
/* For title */
title.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
// I think here is the most important part.
addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());
}
return false;
}
});
}
}
For addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());,
I've tried many possible codes, such as
addActivity_scrollview.smoothScrollTo(0, spinner.getTop());
addActivity_scrollview.smoothScrollTo(0, 300); (300 is just a random number)
addActivity_scrollview.smoothScrollBy(0, 300);
but the scroll is always stuck (it moves a little but it's always the same position with above codes) and the screen barely shows the selected number of NumberPicker. How can I achieve the goal to set scroll so that the screen shows the entire NumberPicker?
just add this line to your edittext code in xml :android:imeOptions="actionSend" and then just try this into your activityaddActivity_scrollview.smoothScrollTo(0, 480);
I should've added android:imeOptions="actionSend" to <EditText> in order to properly listen on onEditorAction(). I thought it'd be okay with android:singLine="true" because that option enabled "next" action on keyboard.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine="true"
android:maxLength="20"
android:imeOptions="actionSend" />