Relative layout alignment in android - java

In the below posted code, i'm not able to see the text in single line and at a time i want only 6 text to be displayed, but i'm able to see morethan 20 text's. can you have a look once why it is..
i need my view to be like this, full view should be covered by 6 texts only 3 by 3
|afsafasfaf afasfaffasfas|
| |
|cascsdfs asdaddasdada|
| |
Mainactivity.java
package com.example.sampleoporj;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
Button mbutton;
MainActivity activity;
LinearLayout mEventsLayout;
HorizontalScrollView mHorizontalscroll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbutton = (Button)findViewById(R.id.button1);
mEventsLayout = (LinearLayout)findViewById(R.id.eventslayout);
mbutton.setOnClickListener(btnlistener);
activity = this;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
OnClickListener btnlistener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0 ;i <10 ; i++)
{
LayoutInflater inflater = activity.getLayoutInflater();
View view = (View)inflater.inflate(R.layout.samplelayout,null);
mEventsLayout.addView(view);
}
}
};
}
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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/helloworld"
android:text="#string/hello_world" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/helloworld"
android:id="#+id/horizontalscroll">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/eventslayout"></LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:layout_toLeftOf="#+id/helloworld"
android:text="Button" />
</RelativeLayout>
Samplelayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".45"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".10"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".45"
android:orientation="vertical" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
i request the output should look like this, i need a horizontal scroll & at a time i need only 3 + 3 text to display (in image i have showed only 2 + 2 ). As i notice horizontal scroll shrinks if i add linearlayout with "fill_parent" param.

Related

Android app keeps stopping while building using android studio

When I'm trying to transfer data between activities, I can't get my message and the app keeps crashing,
It show me 'app keeps stopping'
Code in MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Intent outIntent;
EditText edtPhone;
EditText edtMessage;
Button btnNext;
String tempText="";
public static final String PHONE = "PHONE";
public static final String MESSAGE = "MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View V) {
outIntent = new Intent(MainActivity.this, ActivityTwo.class);
edtPhone = (EditText) findViewById(R.id.edtPhone);
edtMessage = (EditText) findViewById(R.id.edtMessage);
tempText = edtPhone.getText().toString();
outIntent.putExtra(PHONE,tempText);
tempText = edtMessage.getText().toString();
outIntent.putExtra(MESSAGE,tempText);
startActivity(outIntent);
}
});
}
public void closeMethod(View view) {
finish();
}
}
ActivityTwo.java
package com.example.fir;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
Intent incomingIntent;
TextView txtPhone;
TextView txtMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
incomingIntent = getIntent();
txtPhone = (TextView) findViewById(R.id.txtPhone);
txtMessage = (TextView) findViewById(R.id.txtMessage);
txtPhone.setText(incomingIntent.getStringExtra(MainActivity.PHONE));
txtMessage.setText(incomingIntent.getStringExtra(MainActivity.MESSAGE));
}
}
Activitymain.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" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
Activitytwo.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" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
The issue is when I don't put any value into the firstEditText or secondEditText or both of them and click on any button then the app crashes and a pop up shows "myapp keeps stopping".
I cannot get the text and am unsure of why the app keeps crashing.
you have defined your Edit texts in the first activity with id's named
edtPhone
edtMessage
but there was no edtPhone or edtMessage in your xml.
this issue also repeats in your second activity
you have defined txtPhone and txtMessage
but again there are no such ids in your second activity XML.
keep in mind that these might not be the whole problem as you didn't post any log for the errors. but if there were anything else, ask and we'll help you

Android Studio Code in Java to take user input and display output

Here I am trying to take user input and display output in a Text View, but checkgen and checkuser is returning -1 despite of selecting them and also both the radio buttons are getting selected not any one of them, I want to display all the details entered in the text view. I have pasted the activity_reg.xml code below too
reg.java
package com.example.bloodbank;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class reg extends AppCompatActivity {
//login button
Button btn_login;
//firstname
EditText fn;
//lastname
EditText ln;
//phone number
EditText pn;
//output
TextView op;
//registration button
Button btn_reg;
//radio group for gender
RadioGroup rggen;
//radio group for user(donor/in search of blood)
RadioGroup rguser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
btn_login=findViewById(R.id.btn_login);
fn=findViewById(R.id.txtfirstname);
ln=findViewById(R.id.txtlastname);
pn=findViewById(R.id.txtmobile);
op=findViewById(R.id.outputreg);
rggen=findViewById(R.id.rggender);
rguser=findViewById(R.id.rgreg);
btn_reg=findViewById(R.id.main_btn_reg);
btn_reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String fname=fn.getText().toString();
String lname=ln.getText().toString();
int pno= new Integer(pn.getText().toString());
int checkgen=rggen.getCheckedRadioButtonId();
//Toast.makeText(reg.this, "checkgen= "+checkgen, Toast.LENGTH_SHORT).show();
int checkuser=rguser.getCheckedRadioButtonId();
//Toast.makeText(reg.this, "checkuser= "+checkuser, Toast.LENGTH_SHORT).show();
if(((checkgen == -1)||(checkuser == -1))||((checkgen == -1)&&(checkuser == -1)))
{
//no radio button selected
Toast.makeText(reg.this, "Please select an option", Toast.LENGTH_SHORT).show();
}
else {
//radio button is checked
findRadioButton1(checkgen);
findRadioButton2(checkuser);
op.setText("Welcome " + fname + " " + lname + "Phone Number: " +pno );
}
}
});
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(reg.this,login.class);
startActivity(i);
}
});
}
private void findRadioButton2(int checkuser) {
switch(checkuser){
case R.id.rbdonor:
op.setText("\nUser: Donor\n");
break;
case R.id.rbneedy:
op.setText("\nUser: In search of blood\n");
break;
}
}
private void findRadioButton1(int checkgen) {
switch(checkgen){
case R.id.rbmale:
op.setText("\nGender: Male\n");
break;
case R.id.rbfemale:
op.setText("\nGender: Female\n");
break;
}
}
}
activity_reg.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="#+id/rbmale"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".reg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="23dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txtregister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Registration"
android:textColor="#color/colorPrimaryDark"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText
android:id="#+id/txtfirstname"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="First Name*"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText
android:id="#+id/txtlastname"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Last Name*"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
>
<TextView
android:id="#+id/textView"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_weight="1"
android:text=" Gender*:"
android:textColor="#A31D13" />
<RadioGroup
android:id="#+id/rggender"
android:layout_width="309dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rbmale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male"
android:textColor="#BE202F" />
<RadioButton
android:id="#+id/rbfemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female"
android:textColor="#BE202F" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText
android:id="#+id/txtmobile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Mobile Number*"
android:inputType="number"
android:maxLength="10"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<EditText
android:id="#+id/txtpassword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:hint="Password*"
android:inputType="textPassword"
android:maxLength="10"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="111dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:text=" Register as*:"
android:textColor="#BE202F" />
<RadioGroup
android:id="#+id/rgreg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rbdonor"
android:layout_width="171dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="I am a donor"
android:textColor="#BE202F" />
<RadioButton
android:id="#+id/rbneedy"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="I am in search of blood"
android:textColor="#BE202F" />
</LinearLayout>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="1dp"
android:orientation="horizontal">
</LinearLayout>
<Button
android:id="#+id/main_btn_reg"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="20dp"
android:background="#color/colorPrimaryDark"
android:text="REGISTER"
android:textColor="#color/white"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0.6dp"
android:layout_marginTop="16dp"
android:background="#color/colorPrimaryDark"></View>
<Button
android:id="#+id/btn_login"
android:layout_width="86dp"
android:layout_height="44dp"
android:layout_marginTop="7dp"
android:gravity="center"
android:text="LOGIN"
android:textColor="#color/colorPrimaryDark"
android:textSize="14dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/outputreg"
android:layout_width="match_parent"
android:layout_height="60dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> ```

ScrollView isn`t working. What should I do?

How can I scroll in my Android app?
I already tried Scrollview but it doesn`t work.
So this is my MainActivity:
package com.example.myapplication2;
import android.content.DialogInterface;
import android.content.Intent;
import android.icu.text.IDNA;
import android.nfc.Tag;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import static com.example.myapplication2.R.id.*;
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private boolean loadFragment(Fragment fragment){
if(fragment!=null){
getSupportFragmentManager()
.beginTransaction()
.replace(fragment_container, fragment)
.commit();
return true;
}
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = findViewById(R.id.bottomnav);
navigation.setOnNavigationItemSelectedListener(this);
getSupportFragmentManager().beginTransaction().replace(fragment_container, new Home_Screen()).commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment fragment = null;
switch(menuItem.getItemId()){
case kontakt:
break;
case Termine:
fragment = new TermineFragment();
break;
case wilkommen:
fragment = new Home_Screen();
break;
}
return loadFragment(fragment);
}
}
And this is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/darkgrey">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Termine"
android:textSize="30sp"
android:textColor="#color/colorAccent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
></TextView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="80dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
>
<Button
android:text="Mehr infos"
android:layout_width="120dp"
android:layout_height="35dp"
android:id="#+id/button1"
android:layout_weight="1"
android:layout_marginTop="130dp"
android:layout_marginRight="170dp"
/>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/maincolor"
>
</LinearLayout>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="HPI - 29.06.19"
android:textSize="24dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Sitzung: H-E.51"
android:textSize="16dp"
android:layout_marginTop="40dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Juni22, 2019"
android:textSize="16dp"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="270dp"
/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="270dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/colorPrimaryDark"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="HPI - 13.07.19"
android:textSize="24dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Sitzung: H-E.51"
android:textSize="16dp"
android:layout_marginTop="40dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="July13, 2019"
android:textSize="16dp"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="270dp"
/>
</android.support.v7.widget.CardView>
</RelativeLayout>
What should I do? I want these Cardviews in my Scrollview.
Replace your XML layout code with the below. Also, be notified, there seems to be an awkward amount of topMargin between your two cards, not sure if that's intended or not but as of now, there is huge gap between your two cards :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/darkgrey">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Termine"
android:textSize="30sp"
android:textColor="#color/colorAccent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="80dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
>
<Button
android:text="Mehr infos"
android:layout_width="120dp"
android:layout_height="35dp"
android:id="#+id/button1"
android:layout_weight="1"
android:layout_marginTop="130dp"
android:layout_marginRight="170dp" />
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/maincolor">
</LinearLayout>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="HPI - 29.06.19"
android:textSize="24dp" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Sitzung: H-E.51"
android:textSize="16dp"
android:layout_marginTop="40dp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Juni22, 2019"
android:textSize="16dp"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="270dp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="270dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/colorPrimaryDark"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="HPI - 13.07.19"
android:textSize="24dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Sitzung: H-E.51"
android:textSize="16dp"
android:layout_marginTop="40dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="July13, 2019"
android:textSize="16dp"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="270dp"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Use ScrollView in place of RelativeLayout. Add a LinearLayout with vertical orientation before the first TextView and close the LinearLayout after the CardView.

I am having problems programming java on aide app

The app is supposed to call one screen from another and use the other screen's layout components. Whenever I try to call a component, such as ImageButton, it says that member id is unknown, I told my teacher and he says that it shouldn't happen. I have tried renaming the components and even made a new project.
How can I fix it?
the error is in R.id
package com.movil.ejemploventanas;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
public class DatosPersonales extends Activity {
EditText vEdtNom,vEdtEdad;
ImageButton vBtnOrigen,vBtnSantuario;
Intent intnEnvio;
Bundle bndContenedorEnvio;
String strNom;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.datospersonales);
vEdtNom=(EditText)findViewById(R.id.edtNom);
vEdtEdad=(EditText)findViewById(R.id.edtEdad);
vBtnOrigen=(ImageButton)findViewById(R.id.btnOrigen);
vBtnSantuario=(ImageButton)findViewById(R.id.btnSantuario);
intnEnvio=new Intent(DatosPersonales.this,Caracteristicas.class);
bndContenedorEnvio=new Bundle();
vBtnOrigen.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0){
strNom=vEdtNom.getText().toString();
bndContenedorEnvio.putString("nom",strNom);
bndContenedorEnvio.putString("edad", vEdtEdad.getText().toString());
bndContenedorEnvio.putString("opc","1");
intnEnvio.putExtras(bndContenedorEnvio);
startActivity(intnEnvio);
}
});
vBtnSantuario.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0){
strNom=vEdtNom.getText().toString();
bndContenedorEnvio.putString("nom",strNom);
bndContenedorEnvio.putString("edad",vEdtEdad.getText().toString());
bndContenedorEnvio.putString("opc","2");
intnEnvio.putExtras(bndContenedorEnvio);
startActivity(intnEnvio);
}
});
}
}
<?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:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtLetrero"
style="style/titulo"
android:layout_centerHorizontal="true"
android:text="#string/letMariposa"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtLetrero"
android:id="#+id/linearlayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp">
<TextView
android:id="#+id/txtNom"
style="style/letreros"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="string/nom"/>
<EditText
android:id="#+id/edtNom"
style="style/datos"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1"
android:id="#+id/linearlayout2"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/txtEdad"
style="style/letreros"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="#string/edad"
android:textAlignment="gravity"/>
<EditText
android:id="#+id/edtEdad"
style="style/datos"
android:layout_width="220dp"
android:ems="10"
android:layout_height="wrap_content"
android:inputType="number"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/origen"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/linearLayout2"
android:layout_marginLeft="30dp"
android:layout_marginTop="200dp"
android:id="#+id/textView4"
style="style/letreros"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="#id/textView4"
android:layout_marginLeft="80dp"
android:id="#+id/btnOrigen"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sant"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/btnOrigen"
android:layout_marginLeft="120dp"
android:id="#+id/textView5"
style="style/letreros"/>
<ImageButton
android:id="#+id/btnSantuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_marginLeft="180dp"/>
</RelativeLayout>

Android ActionBar with custom layout does not occupy whole width of parent

This is my xml fil
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="fill_horizontal"
android:gravity="fill_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:background="#fff556ff"
android:id="#+id/actionbar_relativelayout"
>
<ImageView
android:clickable="true"
android:id="#+id/imgMenu"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:paddingLeft="10dp"
android:src="#drawable/menu_dashboard" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="22dp"
android:layout_toEndOf="#+id/imgMenu"
android:layout_toRightOf="#+id/imgMenu"
android:text="#string/app_name"
android:textColor="#ffffff"
android:textSize="14sp" />
<ImageView
android:id="#+id/imgUser"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:src="#drawable/user" />
<ImageView
android:id="#+id/imgMessage"
android:layout_width="40dp"
android:clickable="true"
android:layout_height="40dp"
android:layout_marginTop="15dp"
android:layout_toLeftOf="#+id/imgUser"
android:src="#drawable/message_dashboard"
/>
<ImageView
android:id="#+id/imgInfo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginRight="80dp"
android:layout_marginTop="15dp"
android:src="#drawable/ic_action_search"
android:clickable="true"/>
</RelativeLayout>
</LinearLayout>
This is my Activity
RelativeLayout custom_RelativeLayout = (RelativeLayout)findViewById(R.id.actionbar_relativelayout);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM );
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setBackgroundDrawable(
getApplicationContext().getResources().getDrawable(
R.drawable.drawable_actionbar_back));
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
I want to its occupy whole space of action bar but it does not occupy i also read many answers on stackoverflow but didn't work.
1) I want match parent space in action bar
Edit This is my main Avtivity
package com.example.qasim.cashmanagement;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class DashboardActivity extends ActionBarActivity implements View.OnClickListener {
View viewAction;
ImageView imgUserbutton;// Actionbar
ImageView imgMessagebutton;// Actionbar
ImageView imgInfobutton;// Action bar
ImageView imgMenubutton;// Actionbar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard_two);
android.support.v7.app.ActionBar actionbar = getSupportActionBar();
GetActionBarDetails();// Method for inflating ActionBar
/*
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);*/
/* ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
View actionBarView = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(actionBarView, lp);*/
// actionBar.setCustomView(R.layout.custom_actionbar);
/* getSupportActionBar().setCustomView(R.layout.custom_actionbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setBackgroundDrawable(
getApplicationContext().getResources().getDrawable(
R.drawable.drawable_actionbar_back));
viewAction = getSupportActionBar().getCustomView();*/
}
#Override
protected void onResume() {
super.onResume();
}
private void GetActionBarDetails()
{
RelativeLayout custom_RelativeLayout = (RelativeLayout)findViewById(R.id.actionbar_relativelayout);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM );
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setBackgroundDrawable(
getApplicationContext().getResources().getDrawable(
R.drawable.drawable_actionbar_back));
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
imgUserbutton = (ImageView) findViewById(R.id.imgUser);
imgInfobutton = (ImageView) findViewById(R.id.imgInfo);
imgMessagebutton = (ImageView)findViewById(R.id.imgMessage);
imgMenubutton = (ImageView)findViewById(R.id.imgMenu);
imgMenubutton.setOnClickListener(this);
imgMessagebutton.setOnClickListener(this);
imgInfobutton.setOnClickListener(this);
imgUserbutton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
}
If you want to get the height of native action bar then use ?android:attr/actionBarSize in XML.
Now your XML will look like:
<?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="?android:attr/actionBarSize"
android:background="#fff556ff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="#+id/imgMenu"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.4"
android:text="#string/app_name"
android:textColor="#ffffff"
android:textSize="20sp" />
<ImageView
android:id="#+id/imgUser"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imgMessage"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imgInfo"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.15"
android:clickable="true"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</RelativeLayout>

Categories

Resources