I'm trying to make an Activity which allows the user to press a button and when he presses it, it generates an EditText and a spinner. The problem is that the scroll view does not scroll. The page stays as is.
XML:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_order_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.nir.nestleapp.OrderItemsActivity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true">
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_y="58dp"
android:inputType="phone"
android:ems="10"
android:id="#+id/Quantity"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="quantity"
android:layout_x="11dp" />
<Spinner
android:layout_width="174dp"
android:layout_height="48dp"
android:layout_x="148dp"
android:layout_y="58dp"
android:id="#+id/ItemSpinner" />
</AbsoluteLayout>
</ScrollView>
<Button
android:text="add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="133dp"
android:layout_y="431dp"
android:id="#+id/Generate" />
</AbsoluteLayout>
Java:
package com.example.nir.nestleapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class OrderItemsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_items);
final AbsoluteLayout layout = (AbsoluteLayout) findViewById(R.id.activity_order_items);
final Spinner Items=(Spinner)findViewById(R.id.ItemSpinner);
final Button Generate=(Button)findViewById(R.id.Generate);
final EditText Quantity=(EditText)findViewById(R.id.Quantity);
final float[] yPlacement = {280};
Generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Spinner GeneratedSpinner=new Spinner(OrderItemsActivity.this);
EditText GeneratedQuantity=new EditText(OrderItemsActivity.this);
layout.addView(GeneratedSpinner);
GeneratedSpinner.setX(Items.getX());
yPlacement[0] =yPlacement[0]+200;
GeneratedSpinner.setY(yPlacement[0]);
layout.addView(GeneratedQuantity);
GeneratedQuantity.setX(Quantity.getX());
GeneratedQuantity.setY(yPlacement[0]);
GeneratedQuantity.setHint("quantity");
GeneratedQuantity.setWidth(300);
}
});
}
You can change your Absolute layout, use as child LinearLayout
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_y="58dp"
android:inputType="phone"
android:ems="10"
android:id="#+id/Quantity"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="quantity"
android:layout_x="11dp" />
<Spinner
android:layout_width="174dp"
android:layout_height="48dp"
android:layout_x="148dp"
android:layout_y="58dp"
android:id="#+id/ItemSpinner" />
</LinearLayout>
</ScrollView>
Changing AbsoluteLayout into a LinearLayout is the best way to get rid from this issue.
Related
I am really new to java and I came across the following 2 errors error: illegal start of expression public void registerClick(View v) and error: class, interface, or enum expected }
I don't understand what I am doing wrong. I have watched extensive tutorials on Youtube and gone to the Android Studio docs but I can not find out what is wrong with my program. I am trying to go from a signup screen to a register screen if a certain text view is clicked. My code is down below.
Main Activity.java:
package com.example.gooddeed;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Config Stuff
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//end of config stuff
public void registerClick(View v)
{
TextView tv= (TextView) findViewById(R.id.register);
}
}
}
Activity Main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="235dp"
android:background="#drawable/blue_bg" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="36.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_border_rounded_cornwe">
<!-- INPUT -->
<EditText
android:id="#+id/email_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/username_icon"
android:background="#android:color/transparent"
android:fontFamily="#font/poppins_regular"
android:hint="Email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textCursorDrawable="#drawable/cursor_color"
android:textSize="12sp" />
<!-- ICON -->
<ImageView
android:id="#+id/username_icon"
android:layout_width="14.7dp"
android:layout_height="10.3dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="#drawable/email" />
</RelativeLayout>
<!-- Spacer Button -->
<!-- Spacer Button -->
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="14dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="12.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_border_rounded_cornwe">
<!-- INPUT -->
<EditText
android:id="#+id/password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:background="#android:color/transparent"
android:fontFamily="#font/poppins_regular"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="10"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textCursorDrawable="#drawable/cursor_color"
android:textSize="12sp" />
<!-- ICON -->
<ImageView
android:id="#+id/icon"
android:layout_width="14.7dp"
android:layout_height="10.3dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="#drawable/password" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="62dp"
android:layout_marginTop="18.7dp"
android:layout_marginRight="62dp"
android:background="#drawable/blue_fill__rounded_color"
android:fontFamily="#font/poppins_medium"
android:gravity="center"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Login"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/forgotpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:fontFamily="#font/poppins_medium"
android:text="FORGOT PASSWORD?"
android:textColor="#1566e0"
android:textSize="10.5sp" />
<TextView
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:fontFamily="#font/poppins_medium"
android:text="Don't have an account? Make one for free!"
android:textColor="#1566e0"
android:textSize="13sp"
android:clickable="true"
android:onClick="registerClick"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="13.7dp"
android:gravity="center"
android:orientation="horizontal"/>
</LinearLayout>
</RelativeLayout>
Thank you to everyone that helps!
You should have registerClick as a separate method inside the activity because Java does not support directly nested methods
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Config Stuff
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//end of config stuff
}
public void registerClick(View v) {
TextView tv= (TextView) findViewById(R.id.register);
}
}
Android Studio is giving me errors in the Main Activity Java over unresolved symbols or id's. As far as I can see the id's refrenced in the java code exist.
In another app which used these same principles, it worked, I cross refrenced the two apps and could not find anything that stood out.
Here is my java code:
package com.example.android.quizapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class QuizMainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_main);
}
public void display(int value) {
TextView quantityTextView = findViewById(R.id.value_text);
quantityTextView.setText(value);
}
public void slider(View view){
SeekBar seekBar = (SeekBar)findViewById(R.id.i);
int Value = seekBar.getProgress();
EditText text = (EditText)findViewById(R.id.valueText);
String name = text.getText().toString();
text.setText(Value);
}
}
Here is my XML: (The id's are there)
<?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="match_parent"
tools:context="com.example.android.quizapplication.QuizMainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Quiz"
android:textAlignment="center"/>
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="What is you favorite color?"
android:textAlignment="center"
android:paddingTop="20dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_below="#+id/title"
/>
<TextView
android:id="#+id/value_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test"
android:textAlignment="center"
android:layout_alignParentBottom="true"
android:layout_marginBottom="300dp"/>
<SeekBar
android:id="#+id/i"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="146dp"
android:onClick="slider"/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
Try adding this:
import com.example.android.quizapplication.R;
Every time i try to type in the edittext everything goes up and the header disappears. How do i make a linearlayaout linear1 stay put when i try to type in the edittext. i also tried adding "android:windowSoftInputMode="adjustPan">" to the manifest and it is still not working.
This is my layout
<?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="wrap_content"
android:weightSum="100"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear1"
android:weightSum="4"
android:orientation="horizontal"
android:layout_width="0dp"
android:background="#5D4037"
android:layout_height="wrap_content"
android:layout_weight="10"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher"
android:isScrollContainer="false"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="51dp"
android:text="TextView"
android:textColor="#f0f0f0"
android:textSize="24sp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
android:layout_toEndOf="#+id/imageView1"
android:layout_marginStart="51dp" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout1"
android:background="#f0f0f0"
android:layout_weight="80"
android:layout_below="#+id/linear1">
</ListView>
<LinearLayout
android:id="#+id/relativeLayout1"
android:weightSum="4"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#f0f0f0" >
<EditText
android:id="#+id/mess"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:maxHeight="300dp"
android:clickable="true"
android:layout_weight="3"
android:background="#ffffff"
android:focusableInTouchMode="true"
android:scrollbarSize="40dp" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_send"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
This is me java class
import com.obi.thinker.logins.R;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class Chatting extends ListActivity {
EditText mess;
ImageButton send;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.yon);
mess = (EditText) findViewById(R.id.mess);
mess.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
}
}
});
}
}
I don't want the soft keyboard to push the header . it should push only the list view up.
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();
}
I want the text to appear on the button and not under it.And How can I give more weightage to the TextView rather than the Button background Colour.I was building an App TicTacToe and I came across this Issue.I'm not able to solve it.Can someone please guide me?
Activity(MainActivity.java):
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends
AppCompatActivity implements View.OnClickListener {
TextView textone;
Button button,buttonPanel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textone = (TextView)findViewById(R.id.textone);
button = (Button)findViewById(R.id.button);
buttonPanel = (Button)findViewById(R.id.buttonPanel);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button){
textone.setText("This is not Fair");
}
}
}
Layout(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">
<Button
android:id="#+id/buttonPanel"
android:text="#string/button"
android:textSize="40sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f4f18a"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/textone"
android:id="#+id/textone"
android:textSize="45sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button2"
android:id="#+id/button"
android:layout_marginBottom="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
If you have <LinearLayout ... as a parent of your layout, You need to add your <TextView .. and <Button .. like as below
<LinearLayout layout_height=".." layout_width=".." orientation="vertical">
<TextView layout_height=".." layout_width=".." id="#+id/textone"/>
<Button layout_height=".." layout_width=".." id="#+id/button" />
<!-- your other layout view -->
</LinearLayout>
2.If you have <RelativeLayout ... as a parent of your layout, You need to add your <TextView .. and <Button .. as below
<RelativeLayout layout_height=".." layout_width=".." orientation="vertical">
<TextView layout_height=".." layout_width=".." id="#+id/textone"/>
<Button layout_height=".." layout_width=".." id="#+id/button" android:layout_below="#id/textone" />
<!-- your other layout view -->
</RelativeLayout>